-
March 24th, 2009, 06:54 PM
#1
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!
-
March 24th, 2009, 07:22 PM
#2
Re: do-while
First, fix your indenting.
Second, "passing out zero" just means that should be the return value from main.
-
March 24th, 2009, 08:33 PM
#3
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.
-
March 24th, 2009, 08:45 PM
#4
Re: do-while
Originally Posted by XodoX
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.
-
March 24th, 2009, 08:52 PM
#5
Re: do-while
Well, you mean some { } are missing? I followed the instructions - right now I don't see any other possibile error.
-
March 24th, 2009, 09:31 PM
#6
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.
-
March 25th, 2009, 08:14 PM
#7
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 ?
-
March 25th, 2009, 08:17 PM
#8
Re: do-while
You can either make use of the tolower() (or toupper()) function, or else you have to logically OR the two possibilities.
-
March 26th, 2009, 07:02 AM
#9
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;
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|