trouble with while statements...what am i missing?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: trouble with while statements...what am i missing?

  1. #1
    Join Date
    Jan 2013
    Posts
    71

    trouble with while statements...what am i missing?

    Im trying to create a program that has the user input a 5 digit number. If it's between 10000 & 99999, it will do one thing..(just saying 'yes' for now. Outside those numbers will prompt the user to input again. However, if the user inputs the exact digits 76087, it should display 'term'.

    I think my error is the braces. If someone could help me find my error without 'doing it for me'.

    This current code is displaying 'term' whenever the user inputs the 5 digits.

    Code:
    //Author: Kenneth Watkins
    
    #include <iostream>
    
    using namespace std;
    int main()
    {
    int pin;
    
         cout << "Welcome to Movie Food\nEnter your 5-digit pin code: " ;
         cin >> pin;
         cout << endl;
         
         while (pin != 76087)
         {
               if (pin >= 10000 && pin <= 76086 || pin >= 76088 && pin <= 99999)
               {
               cout << "yes" << endl;
               break;
               }
               else
               { 
               cout << "Welcome to Movie Food\nEnter your 5-digit pin code: " ;
               cin >> pin;
               cout << endl;
                   break;
                   }
    }
               if (pin = 76087)
               cout << "term" << endl;
    
                       
                       
    system ("pause");
           return 0;
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,986

    Re: trouble with while statements...what am i missing?

    Do you know the difference between = and ==?

    Not sure what this is trying to accomplish?
    if (pin >= 10000 && pin <= 76086 || pin >= 76088 && pin <= 99999)
    Seems like that could be simplified. You may want to use parens to group your conditions too.
    Last edited by GCDEF; March 9th, 2013 at 06:14 PM.

  3. #3
    Join Date
    Jan 2013
    Posts
    71

    Re: trouble with while statements...what am i missing?

    UGH, i didnt realize i had a = instead of ==. but yes, == is equal to. tbh, im not sure when i would use = instead

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,986

    Re: trouble with while statements...what am i missing?

    Quote Originally Posted by psfign View Post
    UGH, i didnt realize i had a = instead of ==. but yes, == is equal to. tbh, im not sure when i would use = instead
    = assigns a value to a variable.

  5. #5
    Join Date
    Jan 2013
    Posts
    71

    Re: trouble with while statements...what am i missing?

    so it seems like iif im trying to do if statements within a while statement, it only works if i have two ifs.

    for example on this code, i have 5 letter choices the user can input, but if i add more than two if statements than the program doesnt function properly.

    Code:
               while (letter != Q)
               {
                     if (letter = B)
                     {
                     cout << "You selected Beverage\n";
                     cout << "Input 5.00\n";
                     cin >> amount;
                     change = amount - BEVERAGE_COST;
                     cout << fixed << showpoint << setprecision(2);
                     cout << "Change returned: " << change << "\n" << endl;
                     break;
                     }
                     else if (letter = C)
                     {
                     cout << "You selected Candy\n";
                     cout << "Input 2.25\n";
                     cin >> amount;
                     change = amount - CANDY_COST;
                     cout << fixed << showpoint << setprecision(2);
                     cout << "Change returned: " << change << "\n" << endl;
                     break;
                     }
    That code functions fine, but if i add
    Code:
               while (letter != Q)
               {
                     if (letter = B)
                     {
                     cout << "You selected Beverage\n";
                     cout << "Input 5.00\n";
                     cin >> amount;
                     change = amount - BEVERAGE_COST;
                     cout << fixed << showpoint << setprecision(2);
                     cout << "Change returned: " << change << "\n" << endl;
                     break;
                     }
                     else if (letter = C)
                     {
                     cout << "You selected Candy\n";
                     cout << "Input 2.25\n";
                     cin >> amount;
                     change = amount - CANDY_COST;
                     cout << fixed << showpoint << setprecision(2);
                     cout << "Change returned: " << change << "\n" << endl;
                     break;
                     }
                     if (letter = H)
                     {
                     cout << "You selected Hot Dog\n";
                     cout << "Input 7.00\n";
                     cin >> amount;
                     change = amount - HOTDOG_COST;
                     cout << fixed << showpoint << setprecision(2);
                     cout << "Change returned: " << change << "\n" << endl;
                     break;
                     }
                     }
    it no longer works right
    Last edited by psfign; March 9th, 2013 at 08:18 PM.

  6. #6
    Join Date
    Jan 2013
    Posts
    71

    Re: trouble with while statements...what am i missing?

    ok, i figured it out. I have to do cases...

  7. #7
    Join Date
    Feb 2013
    Location
    United States
    Posts
    56

    Re: trouble with while statements...what am i missing?

    Quote Originally Posted by psfign View Post
    Code:
    ...
    	while (pin != 76087)
    	{
    		if (pin >= 10000 && pin <= 76086 || pin >= 76088 && pin <= 99999)
    		{
    			cout << "yes" << endl;
    			break;
    		}
    		else
    		{ 
    			cout << "Welcome to Movie Food\nEnter your 5-digit pin code: " ;
    			cin >> pin;
    			cout << endl;
    			break;
    		}
         }
     ...
    Your while loop will be executed only one time at most, since you are breaking out of it under both conditions of the if statement. Breaking out of loops is also considered to be sloppy coding because it makes the code harder to understand.

    Code:
    if (pin = 76087)
    Code:
    if (letter = B)
    Code:
    else if (letter = C)
    Code:
    if (letter = H)
    You keep using the assignment operator = instead of the comparison operator == which is why your code is not working properly. Now I would agree that allowing assignments inside of an if statement is probably one of the dumbest things ever put into a programming language, but it is something we have to live with unless they ever change the language to disallow it.

    Also, unless Q, B, C, and H are defined as chars and set to appropriate values, your code will not work properly either (or even compile). Character constants must be enclosed in single quotes.
    Last edited by Coder Dave; March 9th, 2013 at 10:39 PM.

  8. #8
    Join Date
    Jan 2013
    Posts
    71

    Re: trouble with while statements...what am i missing?

    Thanks Dave, I did have them set to char. But yea, i keep using = wrong, i switched them to == and it worked.

  9. #9
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,018

    Re: trouble with while statements...what am i missing?

    Looking at your pin input, this would usually be coded using a do loop rather than a while so that the output/input statements need only be stated once.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    int pin;
    
           do {
    		cout << "Welcome to Movie Food\nEnter your 5-digit pin code: " ;
    		cin >> pin;
    		cin.ignore(1000, '\n');
    		cout << endl;
    	} while (pin < 10000 || pin > 99999);
    
            if (pin == 76087)
    	     cout << "term" << endl;
    
    	system ("pause");
    	return 0;
    }
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  10. #10
    Join Date
    Jan 2013
    Posts
    71

    Re: trouble with while statements...what am i missing?

    that makes sense. Can I also mix while statements with for statements? Im doing a number classifying project now that is requiring me to display:
    1. A count of the number of integers entered
    2. The sum of the integers
    3. The average of the integers
    4. The number of odd integers
    5. The number of even integers
    6. The number of zeros.

    I have the number of integers and average done, but when I try to do the evens ( that's as far as ive gotten) it always displays there's zero evens.

  11. #11
    Join Date
    Jan 2013
    Posts
    71

    Re: trouble with while statements...what am i missing?

    well, i think i found the conflict, but i cant figure out how to get the while statement and the for statement to co-exist. if i remove the while statement, the program does count the evens. I am required to do the SENTINEL statement, so would i leave the while statement out?


    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    const int SENTINEL = -999;
    const int N = 5;
    int main ()
    {
        int number;
        int sum = 0;
        int count = 0;
        int odds = 0;
        int evens = 0;
        int zeros = 0;
        int counter;
        int num;
        cout << "Enter integers ending with " << SENTINEL << endl;
        cin >> number;
        
        while (number != SENTINEL)
        {
              sum = sum +  number;
              count++;
              cin >> number;
              }
    
                      
        cout << "The number of integers is: " << count << endl;
        
        if(count !=0)
        {
                 cout << "The average is " << sum / count << endl;
    }
            
             for (counter = 1; counter <= number; counter++)
             {
                 cin >> number;
                 cout << number << " ";
                 
                 switch (number % 2)
                 {
                 case 0:
                 evens++;
                 if (number == 0)
                 zeros++;
                 break;
                 case 1:
                 case -1:
                      odds++;
                      }
                      }
                      cout << endl;
                      cout << "There are " << evens << " evens, " << endl;
    
             
             system ("pause");
           return 0;
    }

  12. #12
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,018

    Re: trouble with while statements...what am i missing?

    Can I also mix while statements with for statements?
    Not sure I understand what you mean. A for loop can contain a while loop and a while loop can contain a for loop. Post your code and we'll see what you are trying to do.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  13. #13
    Join Date
    Jan 2013
    Posts
    71

    Re: trouble with while statements...what am i missing?

    i just posted it right as you were replying

  14. #14
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,018

    Re: trouble with while statements...what am i missing?

    You need to do the odd, even and zero tests within the while loop. You don't need a seperate for loop. So your switch will go in the while loop.

    Code:
    while (number != SENTINEL)
        {
              sum = sum +  number;
    
    //YOU NEED TO DO THE TEST FOR ODD, EVEN AND ZERO HERE
              count++;
              cin >> number;
              }
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  15. #15
    Join Date
    Jan 2013
    Posts
    71

    Re: trouble with while statements...what am i missing?

    boom!

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    const int SENTINEL = -999;
    int main ()
    {
        int number;
        int sum = 0;
        int count = 0;
        int odds = 0;
        int evens = 0;
        int zeros = 0;
        int counter;
        int num;
        cout << "Enter integers ending with " << SENTINEL << endl;
        cin >> number;
        
        while (number != SENTINEL)
        {
              sum = sum +  number;
                       {
              switch (number % 2)
                 {
                 case 0:
                 evens++;
                 if (number == 0)
                 zeros++;
                 break;
                 case 1:
                 case -1:
                      odds++;
                      }
                      }
              count++;
              cin >> number;
        }
    
        system("cls");              
        cout << "Count: " << count << endl;
        cout << "Sum: " << sum << endl;
        if(count !=0)
        {
                 cout << "Average " << sum / count << endl;
                 cout << "Even: " << evens << endl;
                 cout << "Odds: " << odds << endl;
                 cout << "Zeros: " << zeros << endl;
                 }
    
             
             system ("pause");
           return 0;
    }
    Last edited by psfign; March 10th, 2013 at 06:06 PM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center