CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: do-while

  1. #1
    Join Date
    Mar 2009
    Posts
    78

    do-while

    Hello guys,

    we have the following pseudocode and need to create the C++ code accordingly.

    Code:
    Function main
      Pass In: nothing
    
      Do
        Display a message asking user thier age
        Get the users age from the keyboard
        Display a message asking user for thier friend's age
        Get the users friends age from the keyboard
        If age_user equal to age_friend
          Display "You and your friend are the same age."
          Display "You should celebrate together."
        Else
          Display "You and your friend are not the same age."
          Display "You should party together anyway."
        Endif
        Display a message asking user if they want to do it again
        Get the response for loop_control from the keyboard
      While loop_control equals'Y' or loop_control equals 'y'
    
      Pass Out: zero to the OS
    Endfunction

    Here's what I got so far:

    Code:
    int main(void)
      {
      
                         do
               {
                cout << “\nWhat is your age? “;
                cin >> age_user;
                cout << “\nWhat is your friend’s age? “;
                cin >> age_friend;
    
    if (age_use == age_friend)
          cout << “\nYou and your friend are the same age. “;
           cout << “\nYou should celebrate together. “;
    
    else
    
           cout << “\nYou and your friend are not the same age. “;
           cout << “\nYou should party together anyway. “;
           end if;
              
              cout << “\nDo you want to do it again? y or n “;
              
              cin >> loop_control;
              }
    
              while (loop_control == ‘y’);
                }
          
        pause();
    
      return 0;
      }
    It says something is not ok with the caracter_loophole. And I don't really know what is meant by Pass Out: zero to the OS !
    I hope somebody can help me.

    Thank you!

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: do-while

    First, fix your indenting.

    Second, "passing out zero" just means that should be the return value from main.

  3. #3
    Join Date
    Mar 2009
    Posts
    78

    Re: do-while

    Well, the indenting is currently not one of my major concerns. It dosen't even work yet.

    Opps, nevermind. Char, int... Still - dosen't quite work.
    Last edited by XodoX; March 24th, 2009 at 08:39 PM.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: do-while

    Quote Originally Posted by XodoX View Post
    Well, the indenting is currently not one of my major concerns. It dosen't even work yet.
    Getting the indenting right helps make it work. It makes the structure of the program a lot more obvious, so you can more easily see what you're doing wrong.

    You can't rely on the compiler to tell you all of the problems, after all. A perfectly compiling program can still be wrong.

    Trust me, no programmer worth a **** would be caught dead without at least a semblance of proper indenting for more than a few minutes of the development process. Most IDEs can auto-indent for you. Even that function can be enormously helpful for finding missing or mismatched brackets.

    Now, as to actual problems......there's no such thing as "end if". If, else if, and else blocks are denoted by brackets (or else only 1 statement long), which you are also missing.
    Last edited by Lindley; March 24th, 2009 at 08:49 PM.

  5. #5
    Join Date
    Mar 2009
    Posts
    78

    Re: do-while

    Well, you mean some { } are missing? I followed the instructions - right now I don't see any other possibile error.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: do-while

    Yes. The only other issue I see is that you aren't checking if loop_control is either capital *or* lowercase y.

  7. #7
    Join Date
    Mar 2009
    Posts
    78

    Re: do-while

    Wouldn't is be sufficient if I do it like this....


    while (loop_control == ‘Y’);

    So just add this line that has the Y ?

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: do-while

    You can either make use of the tolower() (or toupper()) function, or else you have to logically OR the two possibilities.

  9. #9
    Join Date
    Feb 2009
    Location
    USA
    Posts
    68

    Re: do-while

    Here is your code with proper indenting.

    Code:
    int main(void)
    {
        do
        {
            cout << “\nWhat is your age? “;
            cin >> age_user;
            cout << “\nWhat is your friend’s age? “;
            cin >> age_friend;
    
            if (age_use == age_friend)
                cout << “\nYou and your friend are the same age. “;
                cout << “\nYou should celebrate together. “;
    
            else
                cout << “\nYou and your friend are not the same age. “;
                cout << “\nYou should party together anyway. “;
    
            end if;
           
            cout << “\nDo you want to do it again? y or n “;
            cin >> loop_control;
        }
        while (loop_control == ‘y’);
    }
        pause();
        return 0;
    }
    Like others suggested brackets are causing you a big problem. You need brackets around your if statement and your else statement.
    Code:
    if( " this is true" )
    {
        do this;
    }
    
    else
    {
        do this;
    }
    It also looks like you are closing your main function earlier than you want. There should be some variable declarations also.

    int user_age;
    int friend_age;
    Google is your friend.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured