if-else statement modifier(help!)

the code below runs but i dont know how to modify the else-if statement..
the statement..
look at the underlined text below..
the program just proceeds..
if you enter "y" the program returns to main then executes the else statement(error!only enter letters 'y' or 'n'!the program will now end)..
help please?
can anyone?
thanks


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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
int diff, a;
cout<<"memory game"<<endl;
cout<<""<<endl;
cout<<"Choose Difficulty Level:"<<endl;
cout<<"1 =EASY MODE      <0-10>"<<endl;
cout<<"2= MODERATE MODE  <0-60>"<<endl;
cout<<"3= HARD MODE      <0-100>"<<endl;
cout<<"4= QUIT"<<endl;
cin>>diff;
     
     {
     if(diff==1)
    {
                system("cls");
           srand(time(0));
            int a=rand()%11;
     for(int x=1;x<=5;x++)
             {
             
             float guess;
              cout<<"Input Number:"<<endl;
              cin>>guess;
                      if(guess<a)
                          {
                          cout<<"NUMBER OF TRIES: "<<x<<"/5:"<<endl;
                          cout<<"HIGHER!"<<endl;
                          }
                     else if(guess>a)
                     {
                          cout<<"NUMBER OF TRIES: "<<x<<"/5:"<<endl;
                          cout<<"LOWER!"<<endl;
                     }
                     
                      else if(guess==a)
                       {
                       cout<<"The Random Number Is:"<<a <<endl;
                       cout<<"Congratulations!!!YOU WIN!!! "<<endl;
                       cout<<"do you still want to continue?y/n";
                       cin>>a;
                       {
                       if (a=='y' or 'Y')
                          {
                       main ();
                           }
                       if (a=='n' or 'N')
                          {
                       system("cls");
                       cout<<"thank you for playing your favorite memory game!";
                       system("pause");
                       return 0;
                           }
                       else
                         {
                       system("cls");
                       cout<<"error!only enter letters 'y' or 'n'!the program will now end"<<endl;
                       system("pause");
                       return 0;
                          }
                       }
                       }
                       else if(x==5)
                       {
                       
                 cout<<"YOU LOSE!!!,Please Try Again!!!"<<endl;
              }
        }
    }    
  /*  else if(diff==2)
    {
    moderate();
    }
    else if(diff==3)
    {
    hard();
    }
    else if(diff==4)
    {
    return 0;
    }*/
    else
    {
        cout<<"ERROR!number entered has no data";
    }
    system ("pause");
    return 0;
}
}
line 46 and 50:

'or' in C++ is written '||'. Also, it's if(statement || statement), not if (a == 'y' || 'Y'). The latter will always be true, because it basically says if('Y') which is always true since 'Y' isn't 0.

Rewrite it as such: "if (a == 'y' || a == 'Y')"
Also:
System is Evil: http://www.cplusplus.com/articles/j3wTURfi/

Don't call main(), recursively, as the memory will not be removed from the stack, and cause the memory to be accumulated causing a stack overflow!
@nisheeth
so what do you suggest i should put in there?
thanks
You could put a goto label (whatever they're called) right after int main() like this:

1
2
3
4
5
6
7
8
9
10
int main()
{
beginning:
     // Code goes here.
     if("user wants to quit" == false)
     {
            goto beginning;
     }
     return 0;
}


EDIT: Don't take the if statement seriously; it's pseudocode.
Last edited on
closed account (zb0S216C)
Pyrius wrote:
You could put a goto label (sic)

No! No! No, no, no, no! Never suggest the use of Label: ... goto. Use a loop instead. For example:

1
2
3
4
5
6
7
8
9
int main( )
{
    while( /* Condition */ )
    {
        // Treat this loop as main( )
    }

    return 0;
}


Wazzak
Last edited on
@pyrius goto labal doesnt work..
@framwork cna u help me do the while loop?
need help man..im cramming..
i dont know where to begin now..
closed account (zb0S216C)
Well, as the comment within the loop of my code says, treat the body of the loop as if it was main( ). Such code may appear as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main( )
{
    while( true )
    {
        std::cout << "Memory game" << std::endl;

        int input( 0 );

        std::cout << "Enter a number. Any number, except zero: ";
        std::cin >> input;

        if( input == 0 )
        {
            std::cout << "That's not an acceptable number" << std::endl;

            // Go back to the start of this loop.
            continue;
        }

        // Continue with the game...
    }

    return 0;
}


Wazzak
Topic archived. No new replies allowed.