Help with input errors on c++!

Discussion in 'Mac Programming' started by WinkeyFaced, Sep 19, 2011.

  1. WinkeyFaced, Sep 19, 2011
    Last edited by a moderator: Sep 19, 2011

    WinkeyFaced macrumors newbie

    Sep 19, 2011
    My goal here is to read a file (word.txt) and then output them added. the challenge is to skip over the lines that have words in them and just add the numbers. I really need help with this part and where to put it in my program

    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <sstream>
    using namespace std;
    int main() 
       int n;
       int total;
       int count;
       double average;
       string filename;
        cout << "Please enter the filename: ";
        getline(cin, filename); //getting possibly more than one word for filename
        cout << endl;
        cout << "Opening " << filename << "." << endl;
        ifstream file(filename.c_str());
        if (!file.good()) //if the file isn't there
            cout << "Sorry, I can't find that file." << endl;
        return 0;
        else //if the file is there
         while (!file.eof()) //while the file hasn't ended
             file >> n;
             total += n; //sums all numbers
             count++; //counts the numbers
         average = total / count;
             cout << "The sum is: " << total << endl;
             cout << "The average is: " << average  << endl;
             cout << "The total amount of numbers is: " << count << endl;
  2. lloyddean macrumors 6502a

    May 10, 2009
    Des Moines, WA
    This may give you an idea of how you might accomplish your stated goal -

    void getInput(const std::string& strPrompt, const std::string& strReprompt, int& t)
        using std::cin;
        using std::cout;
        using std::flush;
        using std::endl;
        do {
            cout << strPrompt << ": " << flush;
            if ( cin >> t )
            cout << strReprompt << endl;
            // clear error condition and flush input buffer
            cin.ignore(INT_MAX, '\n');
        } while ( true );
  3. jiminaus macrumors 65816


    Dec 16, 2010
    You'll get nothing from the challenge if you just ask someone for the answer.
  4. chown33 macrumors 604

    Aug 9, 2009
    This is the same "challenge" as your other thread:

    The posted code is the same, too, with one difference: the code posted in this thread calculates an average value.

    Other than calculating and printing the average, there are no significant differences. The code here even has the same bugs as the code first posted.

    Posting the same code multiple times doesn't change the code or fix any bugs in it. Posting the same problem multiple times doesn't give you an answer any sooner.

    If you have no idea how to solve the problem, then think about how to read in a single word, and what type of variable you'd need for that. Then think about how you'd convert a word already read into a number. If you don't know what any of that means, talk to your instructor.

Share This Page