Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
74 views
in Technique[技术] by (71.8m points)

c++ - Program wont print out amount of characters that are in a file

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(); 
    }
}   
question from:https://stackoverflow.com/questions/65943921/program-wont-print-out-amount-of-characters-that-are-in-a-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...