Miata Turbo Forum - Boost cars, acquire cats.

Miata Turbo Forum - Boost cars, acquire cats. (https://www.miataturbo.net/)
-   Insert BS here (https://www.miataturbo.net/insert-bs-here-4/)
-   -   I need help with C++ (https://www.miataturbo.net/insert-bs-here-4/i-need-help-c-11794/)

miataspeed1point6 08-11-2007 03:08 PM

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.

magnamx-5 08-11-2007 03:34 PM

Is it missing a restart end code?

neogenesis2004 08-11-2007 03:46 PM

do you have choice5 declared as a variable outside of the loop?

miataspeed1point6 08-11-2007 03:46 PM

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")}

magnamx-5 08-11-2007 03:51 PM


Originally Posted by miataspeed1point6 (Post 139222)
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?

miataspeed1point6 08-11-2007 04:02 PM

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?

magnamx-5 08-11-2007 04:14 PM

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.

92MX5 08-11-2007 04:27 PM

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

neogenesis2004 08-11-2007 04:43 PM

looks good to me there, thats why i asked. I !<3 debugging.

hustler 08-11-2007 04:50 PM

the day I left computer science was a great one.

miataspeed1point6 08-11-2007 05:12 PM


Originally Posted by 92MX5 (Post 139237)
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.

magnamx-5 08-11-2007 05:17 PM

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?

92MX5 08-11-2007 05:59 PM

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

neogenesis2004 08-11-2007 06:02 PM

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.

messiahx 08-11-2007 06:15 PM

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).

miataspeed1point6 08-11-2007 07:13 PM

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.

wildfire0310 08-11-2007 07:33 PM


Originally Posted by miataspeed1point6 (Post 139300)
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..

neogenesis2004 08-11-2007 08:01 PM

post your entire code for me to look at in a quote box.

miataspeed1point6 08-11-2007 08:08 PM


#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.

neogenesis2004 08-11-2007 09:13 PM

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


All times are GMT -4. The time now is 05:32 AM.


© 2024 MH Sub I, LLC dba Internet Brands