Help with a program reading integers from a .txt file

Hello everybody, I'm new with c++ and I need some help with some stuff, thanks a lot if you are able to!

I need help to write a program that processes the data a .txt file. It needs to
produce a report to an output file that would contain the total number of voter applications brought in by each team(teams 1-5) and the total number of voter applications brought in by all the teams combined.

this is my assignment and what I need help with is if in the .txt file the numbers are arranged like this
1 5
3 8
2 3
1 3
5 6
5 2
4 2
1 1
4 3
0 0


with the numbers in the left column are the teams 1-5 and the numbers on the right are the number of voter registrations they brought in. and 0 0 being the sentinal. I seem to be forgetting how to get the information from the .txt AND how to differentiate between the team and the number of votes that they brought in. Any help is very much appreciated as I am a complete noob.
Use an array to store the 5 teams and there totals.
I'm not to keen on using zero as a flag. Using something like -1 or -99 is a better idea because zero is used commonly as an initializer.

This might work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
ifstream inFile;
int team[5] = {0};  // I'm not sure if this syntax is correct
int teamNum = -1;
int teamVal = -1;

inFile >> teamNum >> teamVal;

while (inFile && (teamNum != 0 && teamVal != 0))
{
     if (1 >= teamNum <= 5)
        team[teamNum-1] += teamVal;  // -1 because arrays start at 0

     inFile >> teamNum >> teamVal;
}






Last edited on
Just a few notes on IceThatJaw's example for clarification because you said you were a beginner:

In line 1, ifstream is probably an std::ifstream. You also need to open the file that you want to read from. You can do that on the same line e.g. std::ifstream inFile("file_name.txt");, or you can do it after initialization e.g. inFile.open("file_name.txt");.

Line 2 is correct, but int team[5] = {}; is enough.

Line 10 should be if (1 <= teamNum <= 5).

You will also want to do inFile.close(); when you are finished with the file stream. Output to a file works in a similar way. http://www.cplusplus.com/reference/iostream/ofstream/
IceThatJaw wrote:
int team[5] = {0}; //I'm not sure if this syntax is correct


It is. This initialized the entire array to 0.

monad wrote:
Line 10 should be if (1 <= teamNum <= 5)


I think you mean Line 10 should be: if (teamNum >= 1 && teamNum <= 5).
Why? I admit that I've never used the former over the latter, but it works does it not?

Off topic, but what is the syntax for quotes within posts?
Last edited on
monad wrote:
Why? I admit that I've never used the former over the latter, but it works does it not?

Off topic, but what is the syntax for quotes within posts?


I actually tested it before posting that just to confirm that it didn't (wasn't positive before that). Results ended up being that it loops infinitely, going up or down. Tested something like this:

1
2
3
4
5
6
int i = 5;
while (1 <= i <= 10)
{
	cout << "i: " << i << endl;
	++i; // Tried --i; as well after confirming that this looped infinitely; same result.
}


As for how quotes work: [quote=PersonName]Thing they said goes here[/quote]
Oh yeah, derp, I realized this after shutting down my computer for the night. I had to come back to announce my shame.

Hm. I thought (after I realized I was wrong) that while (1 <= i <= 10) would be the same as
while ( (1 <= i) <= 10 ) in which case 10 would be compared against a boolean, but based on your results that wouldn't be correct either.

Moeljbcp wrote:
As for how quotes work: [quote=PersonName]Thing they said goes here[ /quote]

Thanks. =]

Edit:
Ugh, logic breaking down, I should just stop posting for this forum's sake. Your results don't necessarily disprove my reasoning because 0 and 1 are both less than or equal to 10. true might not be guaranteed to be 1 though.

This loop exits:
1
2
3
4
5
int i = 5;
while (1 <= i > 0) {
    std::cout << "i: " << i << std::endl;
    --i;
}


My apologies to uhhhjonas for the tangent.
Last edited on
Isn't this (1 <= i <= 10) interpreted like this ((1 <= i) <= 10)?

In turn, the first expression will be either true or false, thus giving either
(1 <= 10) or ( 0 <= 10).

Both of these are true, regardless of the value of i.
Topic archived. No new replies allowed.