I'm trying to read a text file into a do while loop. For some reason I can't get it to countinue until ants = 0 . there are two different simulations, once ants go to o the counter needs to reset and do another simulation. Can someone help me?
the text file looks like this

50000  1
100000 25
90000   25

Code:

int main ()

{

      int ants;                             // gets the integer on the data line
      ifstream inFile;                      // get the text file for decryption
      int count = 0; 
      int yards = 0;
      int inFileLoop = 0;
      int band = 10000;


      if(!inFile)

      {



      std::cout<<"Could not open file"<<std::endl;



      }



      inFile.open("antsData.txt");



      if(inFile)       

      inFile >> ants;

      inFile >> yards;
      do 

      {


      cout << " Time Elapsed   Stadium Devoured   Surviving Ants " << endl;

      cout << "   in min.          in yds. " << endl;


        for(count = 0; count <= 0; count++)   
    for(yards = 0; yards <= 0; yards++)


     //prints out the time elapsed, yards, and antsber of ants

     cout << "      " << count << "                " << yards   << "              " << ants  << endl;

     cout << "      " << count << "                " << yards   << "              " << ants - band << 
             " Band stomps 10000 ants. " << endl;

      }
     while(inFile.eof()); 



      inFile.clear();
      inFile.close();



    // print a closing message and exit program 

    cout << endl << "-=- Execution Terminated -=-" << endl << endl;

    cout << "==================================================" << endl 

         << endl;


    return 0;

}

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.

After doing what you suggested what I ended up with was the program just reading the text file and echo what the file says. I tried a few different variationsof what you gave me and one nearly worked but still would not loop till ants = zero. IO found thta if i didn't use the for statement to intialize the count to zero it doesn't start at 0.

int main ()
{
	int ants; // gets the integer on the data line
	ifstream inFile; // get the text file for decryption
	int count = 0; 
	int yards = 0;
	int inFileLoop = 0;
	int band = 10000;
	inFile.open("antsData.txt");
	if(!inFile)
	{
	
	std::cout<<"Could not open file"<<std::endl;
	}
	
	if(inFile) 
	{	
	// prints out a simulations statement and the data table
		cout << " Time Elapsed        Stadium Devoured          Surviving Ants " << endl;
	cout << "   in min.               in yds. " << endl;
	cout << " ========================================================================== " << endl;
	while(inFile >> ants >> yards)
	{
	
	//prints out the time elapsed, yards, and antsber of ants
	cout << "      " << count << "                      " << yards << "            " << ants << endl;
	cout << "      " << count << "                      " << yards << "            " << ants - band ;
	cout << " Band stomps 10000 ants. " << endl;
	}}
	while(inFile.eof()); 
	inFile.clear();
	inFile.close();
	// print a closing message and exit program 
	cout << endl << "-=- Execution Terminated -=-" << endl << endl;
	cout << "==================================================" << endl 
	<< endl;
	return 0;
}

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)

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?

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.

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?

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 ?

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?

the band can stomp 10000 ants per min which was my "count" the ants start at the e1 yrd line which is set to zero because they have not started eatinf yet and there are 50000 ants lined up at the one yard line
the output should look like this

simulation 1
time elapsed stadium devoured surviving ants
in min in yds
0 0 50000
1 1 40000
2 2 30000
3 3 20000
4 4 10000
5 5 0

simulations 2

time elapsed stadium devoured surviving ants
in min in yds
0 0 100000 band stomps 10000
1 25 90000 band stomps 10000
2 50 30000 band stomps 10000
professor sprays 50000
3 75 20000 band stomps 10000
4 100 10000 band stomps 10000

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;
}

when the ants reach the 50 yard line the professor sprays the ants with bug spray that can kill an additional 50000 ant instantly.

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;
}

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?

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.