Guess My Number problem!!

Apr 20, 2013 at 6:39am
Alright Here Is The Problem
Write A Program That Ask A user To Choose A Number Between 1 And 8 In His/Her Mind
Then With 3 Question Guess The Number

Here what I Write So far
but There are too much if and I'm getting confuse


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>
using namespace std;
int main()
{
    cout<<"Please Choose A Number Between 1 And 8 In Your Mind"
    int a,b,c,d;
    char e,f,j;
    Cout<<"Is Your Number Odd Or Even (O/E)?";
    Cin>>e;
    if (a=="E" || a=="e")
       cout<<"Is Your Number Lower Or Higher Than 4 (L/H)?"
       Cin>>f;
       if (f=="l" || f=="L")
              cout<<"Is Your Number Lower Or Higher Than 3 (L/H)?"
              Cin>>j;
         if (j=="l" || f=="L")
                cout<<"Your Number Is 2"
         else if (j=="h" || f=="H")
                cout<<"Your Number Is 4"    
         else
             cout<<"error";
       else if (f=="h" || f=="H")
              cout<<"Is Your Number Lower Or Higher Than 7 (L/H)?"
              Cin>>j;
Apr 20, 2013 at 7:11am
My first advise will be: use comments! it's very helpfull, then you won't get so confused. Second i think you can divide your program into some functions. Third try using switch - case, it might help as well.. good luck!
Apr 20, 2013 at 9:02am
Consider using round braces {} to group the if/else blocks of statements that are meant to belong together. Your code looks as if you are under the impression that indentation matters in c++. It doesn't, unlike that other language named after a type of snake starting with P. But braces do, as well as semicolons. The first cout statement you've written needs to be separated (using a semicolon) from the declarations of integer variables a,b,c and d that follow it. I'm guessing you mean to have semicolons after all the cout statements in there and braces around all the cout and cin statements on neighbouring lines.

Also capitalisation matters too, ie. whether your type names, variable names and keywords are in lower or upper case. So cout is a standard type name but Cout is not.

Also C++ has no built-in string comparison operator. So if you mean to check that the variable f has the character "L" in it you need to compare it to the character 'L' not a string "L". So for instance, line 13 should look like this:
if (f=='l' || f=='L')
Apr 20, 2013 at 3:20pm

Thanks Man
I Have To Say This Is My First Time With C++
and This is my first Code

Now Here Is My New Problem

I Have To Use Binary Search To Write This Code
But I Don't Know How ??? Or What Binary Search Is For That Matter !!!

By The Way Can Anyone Compile The Code And See If There Is Any Problem With It ???? ( I Mean The Solution and Every Thing)
By The Way
How Can I Make The Program To Exit After It Guessed the Number ???

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
#include <iostream>
using namespace std;
int main()
{
    char a,b,c;
    cout<<"\nPlease Choose A Number Between 1 And 8 In Your Mind\n\n\n";
    cout<<"Question No.1 : Is Your Number Odd Or Even (O/E)?\n\n";
    cin>>a;
    if (a=='E' || a=='e')
    {
    cout<<"Question No.2 : Is Your Number Lower Or Higher Than 5 (L/H)?\n\n";
    cin>>b;
              if (b=='l' || b=='L') 
                  {
                   cout<<"Question No.3 : Is Your Number Lower Or Higher Than 3 (L/H)?\n\n";
                   cin>>c;
                                if (c=='l' || c=='L')
                                cout<<"Your Number Is 2\n";
                                else if (c=='h' || c=='H')
                                cout<<"Your Number Is 4\n";
                                else
                                cout<<"Error\n";
                                }
              else if (b=='h' || b=='H') 
                   {
                   cout<<"Question No.3 : Is Your Number Lower Or Higher Than 7 (L/H)?\n\n";
                   cin>>c;
                                if (c=='l' || c=='L')
                                cout<<"Your Number Is 6\n";
                                else if (c=='h' || c=='H')
                                cout<<"Your Number Is 8\n";
                                else
                                cout<<"Error\n";
                                }
              else
              
              cout<<"Error\n";
                 }
    else if (a=='o' || a=='O')
    cout<<"Question No.2 : Is Your Number Lower Or Higher Than 4 (L/H)?\n\n";
    cin>>b;
                  if (b=='l' || b=='L') 
                  {
                   cout<<"Question No.3 : Is Your Number Lower Or Higher Than 2 (L/H)?\n\n";
                   cin>>c;
                                if (c=='l' || c=='L')
                                cout<<"Your Number Is 1\n";
                                else if (c=='h' || c=='H')
                                cout<<"Your Number Is 3\n";
                                else
                                cout<<"Error\n";
                                }
                  else if (b=='h' || b=='H') 
                   {
                   cout<<"Question No.3 : Is Your Number Lower Or Higher Than 6 (L/H)?\n\n";
                   cin>>c;
                                if (c=='l' || c=='L')
                                cout<<"Your Number Is 5\n";
                                else if (c=='h' || c=='H')
                                cout<<"Your Number Is 7\n";
                                else
                                cout<<"Error\n";
                                }
return 0 ;
}

    
 
Last edited on Apr 20, 2013 at 3:57pm
Topic archived. No new replies allowed.