A noob has a problem

Write your question here.

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
  #include <iostream>
#include <limits>
using namespace std;

int main()
{


   cout << "Guess the number!Its bigger than 1 and smaller than 50"<<endl;
   int a;
   cin >>a;
   if(a==6)
   {
       cout << "You guessed it!"<<endl;
       return 0;
   }
   else if(a!=6)
   {
       cout << "Wrong,try again!" <<endl;
       return a;
   }


}


I can't see why isn't this working.I want it to let me choose another number if I didn't guess,but it only works once.
If you want to allow the user to enter another number, you're going to need a loop.

Line 20: This is going to cause you to exit the program immediately and return an indication to the operating system that the program failed (assuming a is non-zero).
first off like to say love your "noob" reference for the title
second if you want to repeat it more then once use a do while loop that way it will read it once and continue with it ex)


do
{
//////
place here ur code

//////

/// would you like to try again////////

}while(ans == 'n')


I got lost. in while(ans == 'n') shall i replace them with something or just copy paste in code?I forgot to mention its my second year of c++ at school,take me easier :D
@imstuux

sure so the do while loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
do
{
      /// what ever is between these scopes repeat once no matter what
       // the while loop is waiting for your input so
       // create another variable char and ask for the user to enter there answer y for yes again
       // or n for no

cout << "would you like to try again" << endl;
cin >> ans;

}while(ans == 'y')




I think I got this,let me adjust.
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
#include <iostream>
#include <limits>
using namespace std;

int main()
{
int ans;
do
{


   cout << "Guess the number!Its bigger than 1 and smaller than 50"<<endl;

   int a;

   cin >>a;
   if(a==6)
   {
       cout << "You guessed it!"<<endl;

   }
   else if(a!=6)
   {
       cout << "Wrong,try again!" <<endl;
   }

cout<<"Would you like to try again?"<<endl;
}
while(ans == 'y');

}


it works until I type the second number,the program closes because of that stupid "Press any key to continue".How to remove it?
Line 7: ans is an int If you want to test it equal to 'y', ans should be a char

Line 27: You don't do a cin to get ans.

I'm feeling so stupid because I can't figure 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <limits>
using namespace std;

int main()
{
char ans;
int Clue;



   cout << "Guess the number!Its bigger than 1 and smaller than 50"<<endl;


   int a;

   cin >>a;
   if(a==6)
   {
       cout << "You guessed it!"<<endl;

   }
   else if(a!=6)
   {
       cout << "Wrong,try again!" <<endl;
       cout << "Do you need a clue?Type 'Clue'"<<endl;
       if(cin>>Clue)

       {
           cout <<"It's smaller than 25"<<endl;
       }
   }

cout<<"Would you like to try again?Type 'y' for yes and 'n' for no."<<endl;
cin>>ans;

if(ans == 'n')
{
    return 0;
}
else if(ans == 'y')
    return a;


When I type Clue,<<Would you like to try again?Type 'y' for yes and 'n' for no.>> pops out.Why?I also want to add the possibility to choose if you want to continue guessing or stop.
But you haven't used a while loop anywhere in that code?
If you want the program to repeat, you need to use a loop.


http://www.cplusplus.com/forum/beginner/202643/#msg964169
Topic archived. No new replies allowed.