Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

while loop repeating itself

loop

  • Please log in to reply
5 replies to this topic

#1 lva28

lva28

    CC Lurker

  • Just Joined
  • Pip
  • 6 posts

Posted 30 November 2011 - 01:14 PM

Hi guys I have a program that compiles but after I enter the while loops which asks the user if they want more information on an item and I enter 'Y' (yes) it keeps asking over and over again for the same thing.Thanks
 
  while(choice=='Y')    {
      cout<<"\nPlease enter the amount of the item";//outputs price
      cin>>amount;//reads price input
      cout<<"\nPlease enter the weight of the item";
      cin>>weight;//reads the weight from user
      bTax+=amount;//calculates price to total
      totalWt+=weight;//calculates total weight
      cout<<"\nEnter information on an item? Y or N";
    }
  return;
}


Edited by lva28, 30 November 2011 - 03:58 PM.

  • 0

#2 fread

fread

    Programming God

  • Senior Member
  • PipPipPipPipPipPip
  • 897 posts
  • Location:Trinidad and Tobago
  • Learning:C, Java, C++, C#, PHP, Python, PL/SQL

Posted 30 November 2011 - 01:40 PM

Choice is always Y; the while condition evaluates as truth, so the loop repeats. You need the while condition to evaluate as false(in this case). Change the value of choice inside of the loop, so that the while condition evaluates as false next time around and terminates.
  • 0

#3 lva28

lva28

    CC Lurker

  • Just Joined
  • Pip
  • 6 posts

Posted 30 November 2011 - 01:48 PM

would I do it like this?
while(choice=='Y')//while user enters yes then get the price and weight
    {
      cout<<"\nPlease enter the price of the item";//outputs price
      cin>>price;//reads price input
      cout<<"\nPlease enter the weight of the item";//outputs weight
      cin>>weight;//reads the weight from user
      bTax+=price;//calculates price to total
      totalWt+=weight;//calculates total weight
       cout<<"\nEnter information on an item? Y or N";

while(choice=='N')
return 0;

  • 0

#4 fread

fread

    Programming God

  • Senior Member
  • PipPipPipPipPipPip
  • 897 posts
  • Location:Trinidad and Tobago
  • Learning:C, Java, C++, C#, PHP, Python, PL/SQL

Posted 30 November 2011 - 02:08 PM

while(choice=='Y')//while user enters yes then get the price and weight

    {

      cout<<"\nPlease enter the price of the item";//outputs price

      cin>>price;//reads price input

      cout<<"\nPlease enter the weight of the item";//outputs weight

      cin>>weight;//reads the weight from user

      bTax+=price;//calculates price to total

      totalWt+=weight;//calculates total weight

      cout<<"\nEnter information on an item? Y or N";



    /* change the value of choice whilst you are in the while loop; any where within these curly brackets will be ok */

     choice = 'N';

    }

  • -1

#5 lva28

lva28

    CC Lurker

  • Just Joined
  • Pip
  • 6 posts

Posted 30 November 2011 - 02:20 PM

Thanks problem solved
  • 0

#6 agnl666

agnl666

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 172 posts
  • Programming Language:C, Java, C++
  • Learning:Python, Assembly

Posted 30 November 2011 - 07:16 PM

while(choice=='Y') // while user enters yes then get the price and weight 
{      
      cout << "\nPlease enter the price of the item"; // outputs price
       cin >> price; // reads price input
       cout << "\nPlease enter the weight of the item"; // outputs weight
       cin>>weight; // reads the weight from user
       bTax+=price; // calculates price to total
       totalWt+=weight; // calculates total weight
       cout << "\nEnter information on an item? Y or N"; 

      cin >> choice;  // this actually gives the user the choice
}

I know that problem was considered solved though the posted code did not actually give the user the choice. The code posted above will.
  • 0





Also tagged with one or more of these keywords: loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download