Reading file help

This is a discussion on Reading file help within the C++ Programming forums, part of the General Programming Boards category; I have a file that has three players names and how many touchdowns scored and how many catches they have ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    6

    Reading file help

    I have a file that has three players names and how many touchdowns scored and how many catches they have caught. I know the code for opening up the file and reading it but I dont understand how to add each category. For example the file is like this.

    John
    2: 5
    Micheal
    1: 4
    Patrick
    2: 7

    So for the output I want it to read:
    Touchdowns: 5
    Catches: 16

    I know the output code is
    cout<<"Touchdowns: "<<touchdown<<endl;
    cout<<"Catches: "<<catches<<endl;

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do you know how to add up a group of numbers one at a time?

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    No I do not. How do you do that? Is that the basis for adding numbers in a file?

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Variables are your friend.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    for the scores I used to the variable "scored"
    my cin statement is cin>>player>>scored>>caught;
    when I add I know Im going to be adding the scores to another variable which I named "touchdown"

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by bananasplitkids View Post
    No I do not. How do you do that? Is that the basis for adding numbers in a file?
    Yes. Imagine that you and a friend are supposed to add up some numbers, but he has the list of numbers, you have the calculator, and you're talking on the phone so you're stuck. He's going to read off the numbers and you're going to add them up -- how do you do it?

    Once you do that, you should be able to write a C program that does the same thing -- the file is the list of numbers, and the scanf/getline/whatever input functions will read them off to you and you have to add them.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    I believe I have found a way. Would you do:


    touchdown=0;
    while (inputFile<scored){
    touchdown=touchdown+scored;
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by bananasplitkids View Post
    I believe I have found a way. Would you do:

    Code:
    touchdown=0;
    while (inputFile<scored){
    touchdown=touchdown+scored;
    }
    Not really. The idea of "start at 0 and add in new numbers is right", but you need to (1) read data from your file properly and (2) fix your loop condition of the while loop.

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    I thought while (inFile>>scored) would read the scores file and store them in the variable scored.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by bananasplitkids View Post
    I thought while (inFile>>scored) would read the scores file and store them in the variable scored.
    It would. Do you want things like "John" and "Patrick" to be read into an integer variable? You have to read the data that's there and know what it is; read in
    *a name
    *touchdowns
    *a colon ( : )
    *catches
    each time through.

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    Im not totally understanding. So are you saying that I cant use that because the first item is not "scored" but a name.
    So if I used that it would read in Patrick and John as the variable scored.
    So how do I tell it to read in the just the score portion.

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by bananasplitkids View Post
    So how do I tell it to read in the just the score portion.
    You can't, so you don't.

    Generally, you read everything in a text file, discarding what you don't need.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

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