5 Replies - 6314 Views - Last Post: 26 January 2012 - 07:25 AM Rate Topic: -----

#1 kwiatm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-January 12

Do/While loop not working properly

Posted 26 January 2012 - 02:19 AM

Hi, thank you to everybody who helped me out yesterday...new problem today unfortunately. I read a few forums about Loops and looked it up in a book as well, but it still is not working the way it should

int binary;
    
    do 
    {
    
        for(int i =0; i < 50; i++ ) 
   { 
   
   ar[i].arData(i); 
   
   
   
   cout<<"Is there more info to input? 1 for yes, 0 for no ";
   cin>> binary;
   cin.ignore(1, '\n');
   
   };
   
   
    } while (binary == 1);
   


the problem I am having is even if the user inputs 0, the line
ar[i].arData(i); 
still runs

Is This A Good Question/Topic? 0
  • +

Replies To: Do/While loop not working properly

#2 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Do/While loop not working properly

Posted 26 January 2012 - 03:46 AM

it's the cin.ignore('1', '\n'); line, it's AFTER your cin. And your array line is going to run because it is in the do-while loop. so it will run at least once before the program is exited.
Was This Post Helpful? 0
  • +
  • -

#3 kwiatm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-January 12

Re: Do/While loop not working properly

Posted 26 January 2012 - 03:53 AM

View PostIngeniousHax, on 26 January 2012 - 03:46 AM, said:

it's the cin.ignore('1', '\n'); line, it's AFTER your cin. And your array line is going to run because it is in the do-while loop. so it will run at least once before the program is exited.


I need the cin.ignore('1', '\n'); line to prevent the complier from skipping over the first input the second time around.
How can I make a loop which terminates immediately- after the user enters 0, I do not want the loop running again. I was under the impression that the do-while loop was the best way to do this
Was This Post Helpful? 0
  • +
  • -

#4 Armie  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 79
  • Joined: 09-August 08

Re: Do/While loop not working properly

Posted 26 January 2012 - 05:13 AM

First off all, try to improve on indentation. { and } should be indented properly, i.e
for()
{
    for
    {
    }
}



Second of all, line 17, }; is wrong. Remove the ;.

This post has been edited by Armie: 26 January 2012 - 05:14 AM

Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,751
  • Joined: 25-December 09

Re: Do/While loop not working properly

Posted 26 January 2012 - 07:09 AM

View Postkwiatm, on 26 January 2012 - 04:53 AM, said:

I need the cin.ignore('1', '\n'); line to prevent the complier from skipping over the first input the second time around.
How can I make a loop which terminates immediately- after the user enters 0, I do not want the loop running again. I was under the impression that the do-while loop was the best way to do this

First in your code you do not need the cin.ignore(1,'\n');. By default when getting numeric input leading white space is skipped, and since you are only ignoring 1 character it is not needed. The ignore() is usually only needed when switching from numeric input to character input.

As to the problem of the program not terminating after the user enters 0, this problem is being caused because you are asking this question in the for loop. This for loop is not being controlled by your user input, it will only stop execution when it's condition is met. So to get the desired results you will need to test the value of binary and break out of your for loop when the user enters zero.


Jim

This post has been edited by jimblumberg: 26 January 2012 - 07:10 AM

Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Do/While loop not working properly

Posted 26 January 2012 - 07:25 AM

As jimblumberg explained, you don't need the ignore in this situation.

You also don't need the do-while loop. Everything you want can be achieved just by adding an additional condition to the for loop:
int binary = 0;
    
for(int i = 0; i < 50 && binary == 0; i++ ) 
{ 
    ar[i].arData(i); 
   
    cout<<"Is there more info to input? 1 for yes, 0 for no ";
    cin>> binary;
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1