you have some lines out-of-order.
1) move line 28 to line 13. You have to open the file before testing if the open failed.
2) exit the program if the open failed. There is no point continuing. So code exit(1) after the error message.
3) Here is how to code that input loop. Delete that do loop and code a while loop like this
while( infile >> ants >> yards)
{
// do something with the data
}
4) delete lines 59 and 63 -- they not needed. There is no point clearing the flag before closing the file unless you are going to use the same stream object again for something else.
5) lines 47 and 48: What are those two loops supposed to be doing? All they do is destroy the value of the variables that were just read in. And those two loops do nothing because the counter is initialized to 0 then you ask the question if the counter is <= 0. Well, duuuuh! The loop counter is already 0, so the loop will do nothing at all.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
delete line 30 -- it serves no purpose and will cause infinite loop that that trailing semicolon. Its probably just a left-over from your previous version of the program.
>>but still would not loop till ants = zero
Of course not unless the data file has the value 0 in it. If it does contain a 0, then you can code the while loop like this:
while(inFile >> ants >> yards && ants != 0)
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
From the writeup that I have it looks as if the 1st colum of numers are the number of ants and the second is the yards travled. My issue is that trying to subtract the number of ants killed by the band from the intial value of 50,000 until it reaches zero then the program goes back in and runs the second simulation. There must be a way to get to that point because the example output thta was given shows this being done. Would it help if you saw the out put for the problem?
I think I know what you are trying to do, but perhaps sample output and the corresponding input file would make things clearer. Where does the number 50,000 come from? I don't see it in the program itself.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
you don't need that while statement in the program. while(inFile >> ants >> yards) will read the file until eof().
I'm completly confused about those simulations. What do you mean by first simulation and second simulation ? Is each line in that data file a different simulation ?
The first line has 50000 ants and 1 yard. What does that mean? 500000 ants moved 1 yard before something kills all the ants? What are you supposed to do with that information ?
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
the 50000 comes from the data file. the program should read the data that looks like this
50000 1
200000 25
80000 25
90000 25
50000 1
60000 1
60000 10
60000 20
0 1
100000 10
100000 5
during the second simulation i have to tell if the band can stomp the ants before they consume the rest of the stadium which is why i left the
while(inFile.eof())
statement in the program. Is this something you think you understand?
I understand a little better, but not quite. I think I need some output to go with this input file. Was that furnished to you? So the band can stomp 10,000 ants per yard or something? So if there's 30,000 ants and they have to travel 4 yards, the band can stomp them all out since 30,000 divided by 4 is less than 10,000, but if there are 50,000 ants and they travel four yards, the band can't smash them all since 50,000 divided by 4 is more than 10,000 and hence the stadium is devoured?
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
That clears up a lot -- except the part about the professor sprayhing some ants.
int main()
{
const int AntsStompedPerMinute = 10000;
int ants, yards;
ifstream in("datafile.txt");
if(!in.is_open())
{
cout << "Can't open input file\n";
return 1;
}
while( in >> ants >> yards)
{
int minutes = 0;
for(int minutes = 0; ants >= 0; minutes++)
{
cout << minutes << '\t' << minutes * yards << '\t' << ants << "\n";
ants -= AntsStompedPerMinute;
}
cout << "\n\n";
cin.get();
}
cin.get();
return 0;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
with a little work I got it to do both simulations except it won't subtract the additional 50000 once the ants hit 50 yards. Also I want to make it where after five minutes the band can only stomp 5000 instead of 10000.
int main()
{
const int band = 10000;
const int professor = 50000;
int minutes = 0;
int ants;
int yards;
int count = 1;
ifstream in("antsData.txt");
if(!in.is_open())
{
cout << "Can't open input file\n";
}
while( in >> ants >> yards)
{
cout << " *** *** *** *** *** *** *** *** *** *** *** *** *** ";
cout << " *** *** " << endl;
cout << " * * " << endl;
cout << " * SIMULATION " << count << " * " << endl;
cout << " * * " << endl;
cout << " *** *** *** *** *** *** *** *** *** *** *** *** *** ";
cout << " *** *** " << endl << endl;
cout << " EMERGENCY! " << endl << endl;
cout << " The dastardly entomologist from UF have inundated the FSU " << endl;
cout << " stadium with " << ants << " ravening killer ants. Latest ";
cout << " reports estimate that " << endl;
cout << " the stadium is being consumed at the horrific pace of " << yards;
cout << " yard per minute! " << endl << endl;
cout << " From CNN's raw feed, here are the latest statistics : " << endl << endl;
cout << " Time Elapsed Stadium Devoured Surviving Ants " << endl;
cout << " in min. in yds. " << endl;
cout << " ========================================================================== " << endl;
for(minutes = 0; ants >= 0; minutes++)
{
if((minutes * yards) <= 100)
cout <<setw(5) << minutes <<'\t' << setw(15) << minutes * yards << '\t' << setw(15) << ants << endl;
ants -= band;
}
count++;
cin.get();
}
if( yards = 50)
ants -= band + professor;
cin.get();
return 0;
}
Line 47 - don't confuse = with ==.
Line 3 - if the number of ants that the band can stamp changes from 10,000 to 5,000 at some point and that number is stored in the variable band, you need to change the variable band's value at some point from 10,000 to 5,000. If band needs to be able to change, you don't want the const modifier in this line. Have an if statement that compares minutes to 5 and if minutes >= 5 , change band to 5000.
Lines 47 - 50 - these lines are outside of your while loop and your for loop. Is that intentional?
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
so what your saying is that I need to do an
if (minutes >= 5)
band == 5000;
and this will or should run corretly in the program
== is the comparison operator. = is the assignment operator. You would want this:
if (minutes >= 5)
band = 5000; // assignment
You would want something like this in your code from before.
if( yards == 50) // comparison
ants -= (band + professor);
While the parentheses aren't needed above, I put them in to make it more clear.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711