Help with redo please ,...

Pages: 12
Hi im trying to make some kinda stupid story im just fooling around with c++ and now i try to do this : I want to ask do you want to start over ? Y or N and this is my script.


#include <iostream>
using namespace std;
int main(){

string name;
string amount;
string object;

char redo;

Again:

cout << "Enter your name : " << endl;
cin >> name;

cout << "Enter a number : " << endl;
cin >> amount;

cout << "Enter the name of an object : " << endl;
cin >> object;

system("CLS");

cout << "Once upon a time there was a hero called " << name << endl;
cout << "That hero had a village with " << amount << " people ..." << endl;
cout << "Together they all want to defeat the monstrous and enormous " << object << " !! " << endl;
cout << "They all took their weapons and went on their way to " << object << endl;
cout << "BOOM BAM KLETSKABBAM !!!! They defeated " << object << endl;
cout << "And they all lived happily ever after !!" << endl;
cout << " ~~ The End ~~ " << endl;
cout << " " << endl;
cout << "Do you want to start over ? type Y or N" << endl;
cin >> redo;

if(redo == 'Y' || 'y'){
system("CLS");
goto Again;
}
if(redo == 'N' || 'n'){
system("CLS");
}

system("pause");
return 0;
}

Everything works fine but as soon as it asks do you want to start over ? you can fill in any answer and ik will start over .. help please ??
The syntax of your if clause is wrong. It must be

1
2
3
4
5
6
7
if(redo == 'Y' || redo == 'y'){ // Note the second 'redo == '
system("CLS");
goto Again;
}
if(redo == 'N' || redo == 'n'){
system("CLS");
}


This if(redo == 'Y' || 'y'){ asks basically if('y' != 0) which is always true
Thank you man but when i tryed that it actually didnt work either .. i gound out what the problem was ;) look i made redo a int variable and it should be a char variable and i changed some stuff and now it works perfect :D look this is how it looks now..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <cmath>

using namespace std;
int main(){
   
   string name;
   string amount;
   string object;
  
   char redo;
   
   Again:

   cout << "Enter your name : " << endl;
   cin >> name;
   
   cout << "Enter a number : " << endl;
   cin >> amount;
   
   cout << "Enter the name of an object : " << endl;
   cin >> object;
   
   system("CLS");
   
   cout << "Once upon a time there was a hero called " << name << endl;
   cout << "That hero had a village with " << amount << " people ..." << endl;
   cout << "Together they all want to defeat the monstrous and enormous " << object << " !! " << endl;
   cout << "They all took their weapons and went on their way to " << object << endl;
   cout << "BOOM BAM KLETSKABBAM !!!! They defeated " << object << endl;
   cout << "And they all lived happily ever after !!" << endl;
   cout << " ~~ The End ~~ " << endl;
   cout << "      " << endl;
   cout << "Do you want to start over ? type Y or N" << endl;
   cin >> redo; 
   switch (tolower(redo))
   {
          case 'y':
               system("CLS");
               goto Again;
          
          case 'n':
               system("CLS");
               cout << "You can close this program now.." << endl;
   }
    
   system("pause");
   return 0;
}
And now you want to get rid of that goto and use a loop instead.
Is that possible ? Im new Haha show me how.
Above the Again: label, put
1
2
3
bool cont = true;
while(cont)
{

Then remove the :Again label.

You don't need to do anything in the 'y' case, just do
cont = false;
in the 'n' case.
Oh, and add the end of a switch statement there HAS to be a break statement like that:
1
2
3
4
5
6
7
8
9
10
11
switch (tolower(redo))
   {
          case 'y':
               system("CLS");
               goto Again;
               break;
          case 'n':
               system("CLS");
               cout << "You can close this program now.." << endl;
               break;
   }


Only use goto when you absolutely have to. And believe me when I say in any situation you are imagining right now you don't. Reason: The code you produce that way is a nightmare to read.
Oh and don't forget the closing } at the end.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <cmath>

using namespace std;
int main()
{
   
   string name = "";
   string amount = "";
   string object = "";
  
   char redo = '';

   bool bAgain = true;

   while( bAgain )
   {
      cout << "Enter your name : " << endl;
      cin >> name;
   
      cout << "Enter a number : " << endl;
      cin >> amount;
   
      cout << "Enter the name of an object : " << endl;
      cin >> object;
   
      system("CLS");
   
      cout << "Once upon a time there was a hero called " << name << endl;
      cout << "That hero had a village with " << amount << " people ..." << endl;
      cout << "Together they all want to defeat the monstrous and enormous " << object << " !! " << endl;
      cout << "They all took their weapons and went on their way to " << object << endl;
      cout << "BOOM BAM KLETSKABBAM !!!! They defeated " << object << endl;
      cout << "And they all lived happily ever after !!" << endl;
      cout << " ~~ The End ~~ " << endl;
      cout << "      " << endl;
      cout << "Do you want to start over ? type Y or N" << endl;

      cin >> redo; 
      if( tolower(redo) != 'y' )
      {
           bAgain = false;
           cout << "You can close this program now.." << endl;
      }
  }
    
   system("pause");
   return 0;
}


use bools... they are neat!
Okay so i have it fixed like ceruleus showed me and it works perfect now but i dont understand this whole bool thing.. can someone explain it a bit more to me >>??
Just make a call back to main(), and then return.
1
2
3
4
5
6
7
8
9
10
11
12
if(redo == 'Y' || redo == 'y'){
  //system("CLS");
  //goto Again; //get rid of the label Again!
  return main(); //call main again (start over), then return its exit value
}
//if(redo == 'N' || redo == 'n'){
//  system("CLS");
//}

//if anything other then 'Y' or 'y', end program anyway
system("pause");
return 0;


PS: If you omit "return", i.e. /*return*/ main(), it will recurse, but because each call gets added to the stack, you will get n calls to system("pause"), where n is the amount of time you run the program. Try it, you'll see what I mean... :)

PS: PS: Loops work too; but for small simple programs I don't want to be that indented ;)
Last edited on
@kwb13
a "bool" is true/false statement.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool bAgain = true;

int main()
{
    //This code will execute if bAgain = true...
    if (bAgain)
        {
           .....
           //This code will execute if bAgain = false...
         } else {
           ......
         }
return 0;
}


So yea, basically bool is a true/false statement and you can execute your code based on whether it is true or false. Furthermore you can also make a bool function that returns a true/false value, like this...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

bool CheckTruth(int, int);

int main()
{
     bool isTrue;
     int numOne, numTwo;
     cout <<  "Type two numbers to see if they are divisible";

     cin >> numOne >> numTwo;
     isTrue = CheckTruth(numOne, numTwo;

     if (isTrue)
        cout << "The two numbers are divisible!" << endl;
     cout << "The two numbers are NOT divisible!" << endl;
return 0;
}

bool CheckTruth(int numOne, int numTwo)
{
     if (numOne % numTwo)
        return (true);
     return(false);
}


NOTE: I just wrote up the above code in this post, so I'm not 100% sure if it contains any errors or not.. but I'm sure you understand the point I was trying to make.
Last edited on
@kbw13 a bool is a variable that's either true or false. You can also think of it like a flag (if you have ever heard of that). Basically in this case you can use it to keep track of whether or not you should continue.

In my code I should have included
 
while( bAgain == true )


rather than
 
while( bAgain )



You should check out http://www.cplusplus.com/doc/tutorial/operators/ and scroll down to Relational and equality operators ( ==, !=, >, <, >=, <= ) and Logical operators ( !, &&, || ) these are really important things to know!!!

and also http://www.cplusplus.com/doc/tutorial/variables/ has a section on bools

also, the oposite of the example i gave you would be

 
while( bAgain == false )


is the same thing as
1
2

while( bAgain != true )


is the same thing as

 
while( !bAgain )



they are the EXACT same thing... it's just that the first one is easier to understand whereas once you understand what's going on the second is easier to read. It is called a bool because it refers to a boolean value, which is like a binary value. In reality, true really is the same thing as 1 and false is the same thing as 0. You can think of a bool like an int that can only be equal to 1 or 0. I hope that wasn't confusing... did I answer your question?
Last edited on
@Ceruleus
That's a good definition of a bool, except one thing. If you want to get technical about it then false is the same thing as 0, while true is really ANY number other than 0. For example, bAgain = 1500 is the same as bAgain = true. This is what I have read and my understanding anyway, I have never actually tested it because it's bad programming.
Well you all deffinatly helped me alot i go check out the tutorial section on cplusplus and thanks for all i will now mark this topic as solved :)
Noobie, technically false is just false and true is just true, and you can convert 0 to false and any other number to true respectively. It's generally more useful to think about it that way.
return main(); //call main again (start over), then return its exit value

You shouldn't do this ever. It's sloppy, and compilers don't necessarily have to implement this kind of recursion so you could end up running out of stack space anyway. Really, it's basically just a different way to write goto top_of_main;
1
2
3
4
5
6
7
8
if ( true == 1 && false == 0 )
{
   cout << "ceruleus ftw";
}
else
{
   cout << "hanst99 was right";
}
Last edited on
You apparently completely missed the point.
Yes, true and false are not the same as 1 and 0. That's like saying 1.1 == 1 because "I can convert the double to an int".
Well I'm citing this website which says I'm right and you guys are wrong.
http://tinyurl.com/2g9fuon
^That link goes to youtube's main page...I don't get it.
Pages: 12