QJ.NET | Videos | Forums | iPhone | MMORPG | Nintendo DS | Wii | PlayStation 3 | PSP | Xbox 360 | PC | Downloads | Contact Us
Forums | Gaming News | Videos | Downloads | Today's Posts | Mark Forums Read | Chat | FAQ | Members List | Contact

QJ.net Game Discussion - PSP, Xbox, Wii, PS3, PSP Homebrew, and PSP Guides

Go Back   QJ.net Game Discussion - PSP, Xbox, Wii, PS3, PSP Homebrew, and PSP Guides > Consumer > General PC Forums > Everything Windows
The above video goes away if you are a member and logged in, so log in now!

Noob C++ Question.

This is a discussion on Noob C++ Question. within the Everything Windows forums, part of the General PC Forums category; I was wondering what the code would be for a number selection menu, such as: Spoiler for Example : 1. ...

Reply
 
LinkBack Thread Tools
Old 01-12-2008, 05:51 AM   #1
QJ's Nice-guy :)
 
Skyline34's Avatar
 
My Mood: Cheerful
Join Date: Mar 2006
Real First Name: Randy Andy
Location: UK
Just Played: Nazi Zombies
Posts: 4,191
Trader Feedback: 0
Default Noob C++ Question.

I was wondering what the code would be for a number selection menu, such as:
Spoiler for Example:
1. Red
2. Green
3. Blue
4.Yellow

Please choose a number:
Then once a particular number is selected, it should display the chosen option as something like "You chose the colour Red" printed underneath.

Sorry for the noob question, I've literally only just started learning C++ about 3 days ago and I'm just learning the basics at the moment.

Much obliged!

__________________
On Facebook?

JOIN MY MAFIA!
Skyline34 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 01-12-2008, 06:10 AM   #2
 
FranzH's Avatar
 
Join Date: Dec 2006
Location: United Kingdom
Posts: 1,659
Trader Feedback: 0
Default

Well I only know very basic C++ in a command prompt window but I would get it to print the options, then ask for an input and then use an if to check the answer. My syntax might not be correct because I haven't used C++ alot bu here it goes:
Code:
string answer;

cout << "Please choose an option" << endl << " 1. Red" << endl << "2. Green" << endl << "3. Blue" << endl << "4. Yellow" << endl << "Choice:";

cin >> answer;

if (answer == 1)
    cout << "You chose Red";
elseif (answer == 2)
    cout << "You chose Green";
elseif (answer == 3)
    cout << "You chose Blue";
elseif (answer == 4)
    cout << "You chose Yellow";
else
    cout << "Please enter a valid choice";
Then at the end you could have a loop to loop back to the list if they enter a wrong number for validation ^_^

Hope that helps!

Edit, if I remember correctly the program will close instantly after the user input number and it has checked it so you may need to get it to wait for another input. So like make it ask if the user wants to exit and if they press Y then do
Code:
return 0;
and if not then loop back to the beginning of the program.
__________________
[CENTER][IMG]http://img232.imageshack.us/img232/7619/gggggggzq2.png[/IMG][/CENTER][CENTER][B]PSP[/B]: Black (JP) 1.50 + White (UK) 1.50 | [B]PSN[/B]: FranzH - Feel free to add [/CENTER]
[CENTER][SIZE=1]Currently Playing:[B] PC[/B]: WoW + GHIII | [B]PS3[250GB][/B]: TimeCrisis 4 + MGO Beta + COD4 [/SIZE][/CENTER]
FranzH is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 01-12-2008, 06:13 AM   #3
QJ's Nice-guy :)
 
Skyline34's Avatar
 
My Mood: Cheerful
Join Date: Mar 2006
Real First Name: Randy Andy
Location: UK
Just Played: Nazi Zombies
Posts: 4,191
Trader Feedback: 0
Default

That's great, I'll give it a shot now, thanks very much!
__________________
On Facebook?

JOIN MY MAFIA!
Skyline34 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 01-12-2008, 06:18 AM   #4
 
FranzH's Avatar
 
Join Date: Dec 2006
Location: United Kingdom
Posts: 1,659
Trader Feedback: 0
Default

Tell me how it goes
__________________
[CENTER][IMG]http://img232.imageshack.us/img232/7619/gggggggzq2.png[/IMG][/CENTER][CENTER][B]PSP[/B]: Black (JP) 1.50 + White (UK) 1.50 | [B]PSN[/B]: FranzH - Feel free to add [/CENTER]
[CENTER][SIZE=1]Currently Playing:[B] PC[/B]: WoW + GHIII | [B]PS3[250GB][/B]: TimeCrisis 4 + MGO Beta + COD4 [/SIZE][/CENTER]
FranzH is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 01-12-2008, 06:33 AM   #5
No longer a community member.
 
Join Date: Dec 2005
Posts: 22
Trader Feedback: 0
Default

Make sure you #include <string> in that example. However, I wouldn't suggest using a string, because that code won't work with just a string. Change the line that says "string answer" to "int answer".

Also, I would suggest doing a while loop around it, since it's a menu. Dunno, that's what it sounded like you wanted to do
Hardrive is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 01-12-2008, 06:36 AM   #6
 
FranzH's Avatar
 
Join Date: Dec 2006
Location: United Kingdom
Posts: 1,659
Trader Feedback: 0
Default

Oh yeah >.< I have no idea why I used string...
__________________
[CENTER][IMG]http://img232.imageshack.us/img232/7619/gggggggzq2.png[/IMG][/CENTER][CENTER][B]PSP[/B]: Black (JP) 1.50 + White (UK) 1.50 | [B]PSN[/B]: FranzH - Feel free to add [/CENTER]
[CENTER][SIZE=1]Currently Playing:[B] PC[/B]: WoW + GHIII | [B]PS3[250GB][/B]: TimeCrisis 4 + MGO Beta + COD4 [/SIZE][/CENTER]
FranzH is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 01-12-2008, 06:41 AM   #7
QJ's Nice-guy :)
 
Skyline34's Avatar
 
My Mood: Cheerful
Join Date: Mar 2006
Real First Name: Randy Andy
Location: UK
Just Played: Nazi Zombies
Posts: 4,191
Trader Feedback: 0
Default

Quote:
Originally Posted by Hardrive
Also, I would suggest doing a while loop around it, since it's a menu. Dunno, that's what it sounded like you wanted to do
Yes, that's right.

OK, I got it all sorted except for a single error:
Code:
error C2143: syntax error : missing ')' before 'constant'
Error executing cl.exe.
__________________
On Facebook?

JOIN MY MAFIA!
Skyline34 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 01-12-2008, 06:44 AM   #8
 
FranzH's Avatar
 
Join Date: Dec 2006
Location: United Kingdom
Posts: 1,659
Trader Feedback: 0
Default

You need to put a ' ) ' somewhere.. look for the line number :l
__________________
[CENTER][IMG]http://img232.imageshack.us/img232/7619/gggggggzq2.png[/IMG][/CENTER][CENTER][B]PSP[/B]: Black (JP) 1.50 + White (UK) 1.50 | [B]PSN[/B]: FranzH - Feel free to add [/CENTER]
[CENTER][SIZE=1]Currently Playing:[B] PC[/B]: WoW + GHIII | [B]PS3[250GB][/B]: TimeCrisis 4 + MGO Beta + COD4 [/SIZE][/CENTER]
FranzH is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 01-12-2008, 06:45 AM   #9
QJ's Nice-guy :)
 
Skyline34's Avatar
 
My Mood: Cheerful
Join Date: Mar 2006
Real First Name: Randy Andy
Location: UK
Just Played: Nazi Zombies
Posts: 4,191
Trader Feedback: 0
Default

OK cheers.

EDIT: Brilliant! Thank-you so much FranzH and Hardrive!
__________________
On Facebook?

JOIN MY MAFIA!

Last edited by Skyline34; 01-12-2008 at 07:40 AM..
Skyline34 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 01-12-2008, 08:24 AM   #10
No longer a community member.
 
Join Date: Dec 2005
Posts: 22
Trader Feedback: 0
Default

No problem, glad we could help
Hardrive is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 01-12-2008, 11:33 AM   #11
QJ's Nice-guy :)
 
Skyline34's Avatar
 
My Mood: Cheerful
Join Date: Mar 2006
Real First Name: Randy Andy
Location: UK
Just Played: Nazi Zombies
Posts: 4,191
Trader Feedback: 0
Default

Actually, one more little question.

How can I get it to loop back to the user prompt after an option has already been chosen?

Thanks
__________________
On Facebook?

JOIN MY MAFIA!
Skyline34 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
noob , question

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



All times are GMT -8. The time now is 02:42 PM.



Use of this Web site constitutes acceptance of the TERMS & CONDITIONS and PRIVACY POLICY
Copyright © 2009, QJ.NET. All Rights Reserved.
Contact Us