I need help with C++ - Miata Turbo Forum - Boost cars, acquire cats.

Welcome to Miataturbo.net   Members
 


Insert BS here A place to discuss anything you want

Reply
 
 
 
LinkBack Thread Tools Search this Thread
Old 08-11-2007, 04:08 PM   #1
Senior Member
Thread Starter
iTrader: (1)
 
miataspeed1point6's Avatar
 
Join Date: Feb 2006
Location: Detroit
Posts: 1,235
Total Cats: 0
Default I need help with C++

Does anyone here know much about it? I just started learning it in school. I'm trying to create a do while loop, and for the life of me can't figure out why it keeps telling me my choice (choice5) is undeclaired. Here is what I have so far,


do{

((BULK OF CODE))

int choice5;
cout<<"Do you want to play again (Y=Yes/ N= No)?"<<endl;
cout<<"1-Yes"<<endl;
cout<<"2-No"<<endl;
cin>>choice5;
}while (choice5; != '1')

I'm trying to make a game, and I need this to be the replay? Yes/No loop. So if anyone can help I'd really appreciate it, thanks.
miataspeed1point6 is offline   Reply With Quote
Old 08-11-2007, 04:34 PM   #2
:(
iTrader: (7)
 
Join Date: Jul 2006
Location: nowhere
Posts: 8,281
Total Cats: 2
Default

Is it missing a restart end code?
magnamx-5 is offline   Reply With Quote
Old 08-11-2007, 04:46 PM   #3
Elite Member
iTrader: (12)
 
Join Date: Aug 2006
Location: Central-ish VA
Posts: 4,449
Total Cats: 16
Default

do you have choice5 declared as a variable outside of the loop?
neogenesis2004 is offline   Reply With Quote
Old 08-11-2007, 04:46 PM   #4
Senior Member
Thread Starter
iTrader: (1)
 
miataspeed1point6's Avatar
 
Join Date: Feb 2006
Location: Detroit
Posts: 1,235
Total Cats: 0
Default

I don't even know what that is. This is my 4th class of C++, so I'm in the dark.

choice5 is all declared inside the loop. I start the loop after the main() function and then the code I posted above is the very last code before the system("pause")}
miataspeed1point6 is offline   Reply With Quote
Old 08-11-2007, 04:51 PM   #5
:(
iTrader: (7)
 
Join Date: Jul 2006
Location: nowhere
Posts: 8,281
Total Cats: 2
Default

Quote:
Originally Posted by miataspeed1point6 View Post
I don't even know what that is. This is my 4th class of C++, so I'm in the dark.

choice5 is all declared inside the loop. I start the loop after the main() function and then the code I posted above is the very last code before the system("pause")}
SO the pause is most likely the problem you need something to initiate the chosen action given no the system should go into standbye or shutdown right. And given yes the system will restart from the begining right?
magnamx-5 is offline   Reply With Quote
Old 08-11-2007, 05:02 PM   #6
Senior Member
Thread Starter
iTrader: (1)
 
miataspeed1point6's Avatar
 
Join Date: Feb 2006
Location: Detroit
Posts: 1,235
Total Cats: 0
Default

Yeah. When the player puts in 1 I want the game to restart, and when they put 2 close the window or standbye or whatever. What should I use instead of pause?
miataspeed1point6 is offline   Reply With Quote
Old 08-11-2007, 05:14 PM   #7
:(
iTrader: (7)
 
Join Date: Jul 2006
Location: nowhere
Posts: 8,281
Total Cats: 2
Default

I dunno i never took C+ but it seems you need the choice to initiate a action, instead of pause so, i would have the system ask for input then initiate on the new loop via the input of the user, the system should stop untilt he input is entered anyway right as the line is not complete without the info of the user. Agian i am not a C+ student but this makes logical sense to me.
magnamx-5 is offline   Reply With Quote
Old 08-11-2007, 05:27 PM   #8
Junior Member
iTrader: (1)
 
Join Date: Oct 2004
Location: Toronto, ON
Posts: 83
Total Cats: 0
Default

choice5 is declared within the scope of the do - while loop. Define it outside, like this:

int choice5 = 0;
do {
...
} while( choice5 != '1' )

Might want to pre-declare it, as above. Also: you had (choice5; != 1) - that should be (choice5 != '1'). Hope this helps.

Cheers!

Jeff
92MX5 is offline   Reply With Quote
Old 08-11-2007, 05:43 PM   #9
Elite Member
iTrader: (12)
 
Join Date: Aug 2006
Location: Central-ish VA
Posts: 4,449
Total Cats: 16
Default

looks good to me there, thats why i asked. I !<3 debugging.
neogenesis2004 is offline   Reply With Quote
Old 08-11-2007, 05:50 PM   #10
Tour de Franzia
iTrader: (6)
 
hustler's Avatar
 
Join Date: Jun 2006
Location: Republic of Dallas
Posts: 29,114
Total Cats: 351
Default

the day I left computer science was a great one.
hustler is online now   Reply With Quote
Old 08-11-2007, 06:12 PM   #11
Senior Member
Thread Starter
iTrader: (1)
 
miataspeed1point6's Avatar
 
Join Date: Feb 2006
Location: Detroit
Posts: 1,235
Total Cats: 0
Default

Quote:
Originally Posted by 92MX5 View Post
choice5 is declared within the scope of the do - while loop. Define it outside, like this:

int choice5 = 0;
do {
...
} while( choice5 != '1' )

Might want to pre-declare it, as above. Also: you had (choice5; != 1) - that should be (choice5 != '1'). Hope this helps.

Cheers!

Jeff
Thanks! It worked, but the problem now is even if you put in 2 (no) it will still loop the game.
miataspeed1point6 is offline   Reply With Quote
Old 08-11-2007, 06:17 PM   #12
:(
iTrader: (7)
 
Join Date: Jul 2006
Location: nowhere
Posts: 8,281
Total Cats: 2
Default

you need to differentiate the choice 1=restart 2=end, i see that you have 1 sorted but not 2 maybe this means that since 2 is undifined the answer will always be 1. Am i right?
magnamx-5 is offline   Reply With Quote
Old 08-11-2007, 06:59 PM   #13
Junior Member
iTrader: (1)
 
Join Date: Oct 2004
Location: Toronto, ON
Posts: 83
Total Cats: 0
Default

Did you remove the declaration of choice5 from within the do - while loop? If not, then you have it defined in two scopes - inside the loop, and outside. It'll set the value of choice5 in the loop, but NOT the value of choice5 outside the loop - that's the scope that the variable is being checked.

Cheers!

Jeff
92MX5 is offline   Reply With Quote
Old 08-11-2007, 07:02 PM   #14
Elite Member
iTrader: (12)
 
Join Date: Aug 2006
Location: Central-ish VA
Posts: 4,449
Total Cats: 16
Default

the while statement is conditional. It means that as long as the input is not '1' is will continue to loop. I personally never use do{} while{}, I just use while{}.

int choice5 0;
while(choice5 != '1') {

((BULK OF CODE))

cout<<"Do you want to play again (Y=Yes/ N= No)?"<<endl;
cout<<"1-Yes"<<endl;
cout<<"2-No"<<endl;
cin>>choice5;
}

That should work just as well.
neogenesis2004 is offline   Reply With Quote
Old 08-11-2007, 07:15 PM   #15
Senior Member
iTrader: (2)
 
Join Date: Jun 2007
Location: Shalimar, FL
Posts: 965
Total Cats: 7
Default

Listen to neogen, he is right. I'm liking this forum more and more (computers were -- and still are -- the only other interest besides cars I have, and the one I'm far more qualified to talk about :gay. I've got a 2 year degree in programming and going on to be a software engineer with school starting in 2 weeks. I once wrote tic tac toe in c++ that was text based. Also did a little OpenGL stuff but that is another field in itself (and a few years ago).
messiahx is offline   Reply With Quote
Old 08-11-2007, 08:13 PM   #16
Senior Member
Thread Starter
iTrader: (1)
 
miataspeed1point6's Avatar
 
Join Date: Feb 2006
Location: Detroit
Posts: 1,235
Total Cats: 0
Default

I ran it how you said and it needed a primary expression before int. the program runs when I add an equal sign,

int choice5=0;

The problem is when I do that it freaks out and just keeps looping. Do I need to set a value somewhere to only let it loop once?

Thanks alot for all the help.
miataspeed1point6 is offline   Reply With Quote
Old 08-11-2007, 08:33 PM   #17
Senior Member
iTrader: (1)
 
Join Date: Jun 2006
Location: Outside of the Loop-ATL
Posts: 754
Total Cats: 0
Default

Quote:
Originally Posted by miataspeed1point6 View Post
I ran it how you said and it needed a primary expression before int. the program runs when I add an equal sign,

int choice5=0;

The problem is when I do that it freaks out and just keeps looping. Do I need to set a value somewhere to only let it loop once?

Thanks alot for all the help.


If I remember from Qbasic days you have to define a value for choice 5 and it sounds as if your value is wrong.

some like

choice5= X

If X=1 then::: if not blah blah blah


or


If X=1 then....
If X=2 the..blah

now its been 4 years since those days and never used C++

Good Luck, glad I went into Animation over programing..
wildfire0310 is offline   Reply With Quote
Old 08-11-2007, 09:01 PM   #18
Elite Member
iTrader: (12)
 
Join Date: Aug 2006
Location: Central-ish VA
Posts: 4,449
Total Cats: 16
Default

post your entire code for me to look at in a quote box.
neogenesis2004 is offline   Reply With Quote
Old 08-11-2007, 09:08 PM   #19
Senior Member
Thread Starter
iTrader: (1)
 
miataspeed1point6's Avatar
 
Join Date: Feb 2006
Location: Detroit
Posts: 1,235
Total Cats: 0
Default

Quote:
#include<iostream>
#include<string>
using namespace std;

main ()
{
int choice5=1;
while(choice5 != '1') {

int soldiers, killed, survivors;
string leader;

//This is the introduction screen and asks the player to input important
//information.
cout<<"******************************************* *************"<<endl;
cout<<"** Welcome to the game **"<<endl;
cout<<"******************************************* *************"<<endl;
cout<<"\t\nPlease enter the following information"<<endl;
//Here the player will input the number of adventurers, survivors as well as the
//players name. These will all be used further along in the story.

cout<<"\nEnter a number: ";
cin>>soldiers;
do{
cout<<"\nEnter a number smaller than the first: ";
cin>>killed;
if (killed>soldiers)
cout<<"Invalid number.";
}while (killed>soldiers);

survivors=soldiers-killed;

cout<<"\nEnter your name: ";
cin>>leader;
//This is the story segment. The above numbers that the player put in are
//put into context down below.
cout<<"******************************************* ******************************"<<endl;
cout<<"******************************************* ******************************"<<endl;
cout<<"\nA group of explorers set out to map their land. Along the way they were"<<endl;
cout<<"attacked by a rouge group from another village. In retaliation the"<<endl;
cout<<"King sent the foolish General Tivian to avenge the murdered explorers."<<endl;
cout<<"The General and all soldiers were lost. The King was outraged."<<endl;
cout<<"\nA brave group of "<<soldiers<<" soldiers were sent out to march across"<<endl;
cout<<"the land. This group was led by the legendary General, "<<leader<<".\n"<<endl;
cout<<"This group was also attacked, and "<< killed <<" of the men were killed."<<endl;
cout<<"The soldiers had fought hard, and most of their weapons and armor had"<<endl;
cout<<"been heavily damaged."<<endl;
cout<<"\nThe surviving members all want to move on, but they have no equipment."<<endl;
cout<<"\nWhat should the group do?"<<endl;
cout<<"******************************************* *****************************"<<endl;
int choice;
cout<<"* Your choices are: *"<<endl;
cout<<"* 1-Press on *"<<endl;
cout<<"* 2-Run home *"<<endl;
cout<<"* Enter your selection: *"<<endl;
cout<<"******************************************* *****************************"<<endl;
cin>>choice;

switch(choice)
{
case 1:
cout<<"\nYou rally the troops and press on!"<<endl;
break;
case 2:
cout<<"\nThe troops laugh at you and force you to keep going!"<<endl;
break;
default:
cout<<"\nYou must make a valid choice!"<<endl;
}
cout<<"The soldiers and their leader "<<leader<<" continued on through the night."<<endl;
cout<<"The next morning they started their long journey to the offending village."<<endl;
cout<<"Just before settling down for lunch the group came across a massive"<<endl;
cout<<"cave."<<endl;
cout<<endl;
cout<<"******************************************* *****************************"<<endl;
int choice2;
cout<<"* Will you explore the cave?: *"<<endl;
cout<<"* 1-Lets check it out! *"<<endl;
cout<<"* 2-We have no equipment, we are in no shape to explore. *"<<endl;
cout<<"* Enter your selection *"<<endl;
cout<<"******************************************* *****************************"<<endl;
cin>>choice2;

switch(choice2)
{
case 1:
cout<<"\nThe cave has a hidden army surplus, you have";
cout<<"plenty of new equipment"<<endl;
cout<<"for everyone!"<<endl;
cout<<"Newly fitted with equipment the soldiers spirits are"<<endl;
cout<<"high. They proudly march on and come across the "<<endl;
cout<<"village they had been looking for."<<endl;
cout<<endl;
break;
case 2:
cout<<"\nThe group sits down for lunch. This uses up the"<<endl;
cout<<"last of the food they have. Tired, they all march on."<<endl;
cout<<"Over the next hill, they can see the village."<<endl;
break;
default:
cout<<"\nYou must make a valid choice!"<<endl;
}

cout<<"The troops want to fight right away, but being the brilliant general "<<endl;
cout<<leader<<" is,the group is brought together to decide what to do next."<<endl;
cout<<endl;
cout<<"******************************************* *****************************"<<endl;
int choice3;
cout<<"* What will you and your troops do?: *"<<endl;
cout<<"* 1-Charge in! *"<<endl;
cout<<"* 2-Wait for reinforcements *"<<endl;
cout<<"* Enter your selection *"<<endl;
cout<<"******************************************* *****************************"<<endl;
cin>>choice3;

switch(choice3)
{
case 1:
cout<<"\nYou rush in and kill most of the offenders,"<<endl;
cout<<"but they end up killing all but 2 of the men. "<<endl;
cout<<"You return home and the family members of the "<<endl;
cout<<"dead men are furious. You are cast out of your "<<endl;
cout<<"village and become a beggar."<<endl;
cout<<endl;



break;
case 2:
cout<<"You wait for reinforcements and you take the base"<<endl;
cout<<"with little trouble. You return home a hero, and given"<<endl;
cout<<" a large palace."<<endl;
break;
default:
cout<<"You must make a valid choice!";
}

cout<<"******************************************* *****************************"<<endl;
cout<<"* *"<<endl;
cout<<"* Do you want to play again (Y=Yes/ N= No)? *"<<endl;
cout<<"* y-Yes *"<<endl;
cout<<"* n-No *"<<endl;
cout<<"* *"<<endl;
cout<<"******************************************* *****************************"<<endl;
cin>>choice5;
}

system("pause");
}
It's cheesy, but I'm just learning. Anyway, in this form it just keeps looping, it won't stop.
miataspeed1point6 is offline   Reply With Quote
Old 08-11-2007, 10:13 PM   #20
Elite Member
iTrader: (12)
 
Join Date: Aug 2006
Location: Central-ish VA
Posts: 4,449
Total Cats: 16
Default

you started the program with choice 5 already being 1 so the loop repeats infinitely. I'm taking a look at the rest of it in VS now.

In its present state the program wont even compile for me
neogenesis2004 is offline   Reply With Quote
 
 
Reply

Related Topics
Thread Thread Starter Forum Replies Last Post
Software/IT types/Joe P, step inside please z31maniac Insert BS here 13 09-22-2015 03:59 PM
Help me choose a MICROCONTROLLER board JasonC SBB ECUs and Tuning 2 09-21-2010 12:39 PM
Hey all, new guy from Va noth1ngmuch Meet and Greet 8 10-27-2009 03:22 PM
MegaSquirt programming libraries? UrbanSoot MEGAsquirt 9 01-14-2008 06:06 PM
Geek trap arga Insert BS here 27 05-08-2007 01:44 PM


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 -4. The time now is 09:02 PM.