Adding my values together

This is a discussion on Adding my values together within the C++ Programming forums, part of the General Programming Boards category; Hi all, I've made some code that reads a text file for a first name, a surname and a rating. ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    14

    Adding my values together

    Hi all,

    I've made some code that reads a text file for a first name, a surname and a rating. The text file has multiple entries in, so here's where I bump into my problem. I want to add all the ratings together, but don't know how as I only seem to know how to get the rating for one entry.

    Here's my code:

    Code:
     
    #include <iostream.h> 
    #include <fstream.h>
    int main()
    {
        char first[80], second[80], rating[2];
        ifstream inFile("junk.txt", ios::in);
            while (inFile >> first >> second >> rating)
            cout << first << " " << second << endl;
        inFile.close();
        cin.get();
        return 0;
    }
    Could anyone please suggest a way I could add all of the ratings together?

    Thanks very much,
    Mike

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
       char first[80], second[80];
       int rating, sum = 0;
       ifstream inFile("junk.txt", ios::in);
    
       while (inFile >> first >> second >> rating) {
          cout << first << " " << second << endl;
          sum += rating;
       }
       cout << "sum = " << sum << endl;
    
       inFile.close();
       cin.get();
       return 0;
    }

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    14
    Easy as that, eh? Heh, thanks very much Speedy5

    Works perfect!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. putting values into an array
    By zdream8 in forum C Programming
    Replies: 15
    Last Post: 05-21-2008, 11:18 PM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. can't assign proper values to an array of string
    By Duo in forum C Programming
    Replies: 1
    Last Post: 04-04-2005, 06:30 AM
  4. Replies: 1
    Last Post: 02-03-2005, 02:33 AM
  5. adding ASCII values
    By watshamacalit in forum C Programming
    Replies: 1
    Last Post: 12-26-2002, 06:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21