Can you fix my code please? WARNING: There are ALOT of problems

#include<iostream>
using namespace std;
int main()
{
string a;
void apple(void);
void banana (void);
void orange (void);
char c;


cout << "Yo what's your name?" << endl;
cin >> c;
cout << "Cool, btw " << c << "wanna play a game?" << endl;
getline (cin, a);
if ( a == 'yes' || a == 'Yes' || a == 'YES' );
{
cout << "Ok." << endl;


cout << "Guess what's my favourite food is\n" << endl;
cout << "1) Apple 2) Banana 3) Orange" << endl;
switch(c)
{
case 1: apple;
break;
case 2: banana;
break;
case 3: orange;
break;
cout << "Wanna try again? (y/n)" << endl;
cin >> c;
while ( c == 'y' || c == 'Y');
return 0;
}
else
{
cout << "Well too bad then." << endl;
cin.get();
cin.get();
return 0;
}

void apple (void)
{
cout << "Hmm...close...but NO" << endl;
}
void banana (void)
{
cout << "WTH" << endl;
}
void orange (void)
{
cout << "YEP" << endl;
}

I know, I'm a BIG newbie =)
Please use [code][/code] tags http://www.cplusplus.com/articles/firedraco1/ and post the error messages
Some thing I see are:
if ( a == 'yes' || a == 'Yes' || a == 'YES' ); You should be using double quotes and you have an extra semicolon

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
break;
cout << "Wanna try again? (y/n)" << endl;
cin >> c;
while ( c == 'y' || c == 'Y');
return 0;
}
else
{
cout << "Well too bad then." << endl;
cin.get();
cin.get();
return 0;
}

void apple (void)
from the next line nothing would be executed as the switch doesn't jump here

reads c
if c is equal to 'y' or 'Y' get an infinite loop

close switch brace
else before closing the if brace





close else brace

define a function inside the unclosed if block and inside unclosed main
you should put your code in tag code.
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
#include<iostream>
using namespace std;

//without main function
void apple(void);
void banana (void);
void orange (void);

int main()
{
    string a, name;
    char test;
    int n;

    cout << "Yo what's your name?" << endl;
    getline(cin,name);
    cout << "Cool, btw " << name << " wanna play a game (yes/no)?" << endl;
    getline (cin, a);
    if ( a == "yes" || a == "Yes" || a == "YES" ) //  ;
        {
            cout << "Ok." << endl;

            do {
                cout << "Guess what's my favourite food is\n" << endl;
                cout << "1) Apple \n2) Banana \n3) Orange" << endl;
                cout << "your choose : "; cin >> n;
                switch(n)
                    {
                        case 1: apple();
                                        break;
                        case 2: banana();
                                        break;
                        case 3: orange();
                                        break;
                        default: cout << "to 1->3";
                    }
                cout << "Wanna try again (y/n) ?";
                cin >> test;
            } while ( test == 'y' || test == 'Y');
        }
    else
        cout << "Well too bad then." << endl;

    return 0;
}
void apple (void)
{
    cout << "Hmm...close...but NO" << endl;
}
void banana (void)
{
    cout << "WTH" << endl;
}
void orange (void)
{
    cout << "YEP" << endl;
}
Jack. I redid some of the code towards what I am currently learning, but tried to keep it as on target as much as possible (except for a few couts, my stab at humor,,,:D) any ways, take a look through it and let me know what you think,,, it seems to be working ok, i think i worked out the bugs. Look Ma! no goto's!!! ;)

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
    

#include<iostream>
#include <cctype> 
#include <cstring>
#include <cstdlib>
using namespace std;


void fav_fruit();// fav_fruit prototype
void guess();// guess prototype
int main()
{
char c[80]; char a[80];char b[80];char e[80];
int i,f;

     cout << "Yo what's your name?\n" ;
          cin >> c;


     cout <<"Cool, btw " << c << " wanna play a game?\n" ;
          cin >> a;
for (i=0;a[i]; i++){if(isupper(a[i]))  a[i] = tolower(a[i]);} 
  
if(!strcmp(a, "yes")){cout << "Ok.\n"; fav_fruit();}






return 0;
} 

void fav_fruit()

{

int i;
char d[80];char e[80];


     cout << "Guess what's my favorite food is\n" ;
     cout << "Apple, Banana, Orange\n\n";
          
          cin >> d;


for (i=0;d[i]; i++)if(isupper(d[i]))  d[i] = tolower(d[i]); 


     if(!strcmp(d, "apple" ))  cout << "Apple,,, close,,, but no tangerine!!! \n\n" ;
     if(!strcmp(d, "banana")) cout << "well,, I am talking about my favorite to eat,,\n";
     if(!strcmp(d, "banana")) cout << "so,,, grudgingly,, NO!!(making 'L' sign on forehead)\n\n";
     if(!strcmp(d, "orange")) cout << "Yez!!! now lets do ze bumpitty bump!!!\n";
     if(!strcmp(d, "orange"))return; 
          
          guess();

}

void guess()

{

int i;
char d[80];char e[80];
cout << "guess again?";
cin >>e;

for (i=0;e[i]; i++){if(isupper(e[i]))  e[i] = tolower(e[i]);} 


     if(!strcmp(e, "yes")) fav_fruit();
          else cout  << "Well too bad then.\n\n"; 

return;

}
Last edited on
How do u put code in tag? Srry Im new here
Topic archived. No new replies allowed.