Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

WinkeyFaced

macrumors newbie
Original poster
Sep 19, 2011
2
0
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

Code:
#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;
     
     }

    }
 
Last edited by a moderator:
This may give you an idea of how you might accomplish your stated goal -

Code:
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 )
        {
            return;
        }
        
        cout << strReprompt << endl;
        
        // clear error condition and flush input buffer
        cin.clear();
        cin.ignore(INT_MAX, '\n');
    } while ( true );
}
 
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.

This is the same "challenge" as your other thread:
https://forums.macrumors.com/threads/1231451/

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.