Hello all I have to make a program that counts all the characters that are in a given text file I have no errors when I compile it. But when I run it it prompts and takes the name of my file but then ends. Here is my code.
#include <string> #include <iostream> #include"counterType.h" #include <fstream> #define MAX 1000 using namespace std; void countchar(counterType intchar, ifstream& indata); int main() { ifstream indata; counterType intchar; cout << "What file would you like to read?" << endl; string filename = "empty"; cin >> filename; indata.open(filename); return 0; countchar(intchar, indata); intchar.displayCounter(); } void countchar(counterType intchar, ifstream& indata) { string x; indata >> x; while(!indata.eof()) { indata >> x; intchar.incrementCounter(); } }
The line
return 0;
makes it return from the function and prevents executing lines after that. You should remove that to execute countchar(intchar, indata); and intchar.displayCounter();.
countchar(intchar, indata);
intchar.displayCounter();
2.1m questions
2.1m answers
60 comments
57.0k users