having trouble with functions

Hello I'm trying to teach my self c++ and I have run into a problem i have two functions the main program in main and another function called option that reads a character. if y it goes back to main if n it closes and if anything else goes back to the begining of the function option. I got it to compile and build but when i ran it; it didn't read my second option. here is my 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
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
	int a,b,c;
	a=(rand()%5)+1;
cout<< a;
cout<< "\n";
cout<< "please type a number.";
cin>> b; 
 c=a+b;
 if (c>5) 
 {cout<< "you win";}
 else if (c<5)
 { cout<< "you loose";}
 else if (c==5)
 {cout << "you win";}}
 int option()
 {
	 char choice;
 cout<< "do you want to play again? y/n";
 cin>> choice;
 if (choice=='y')
 {return main();}
 else if  (choice=='n')
 { return 0;}
 else 
 {cout<< "not a valid character";}
 return option();
 
}
Two function? All I see is your main.
indentation
<nolyc> indentation is optional whitespace. see also: !vowel
vowel
<nolyc> vwls r bt s sfl s whtspc, spclly n vrbl nms. s ls: !ndnttn
ndnttn
<nolyc> indentation is optional whitespace. see also: !vowel
@ Austin I have two brakets closing my if function and then my main and then i introduse my next function.
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
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
int a,b,c;
a=(rand()%5)+1;
cout<< a;
cout<< "\n";
cout<< "please type a number.";
cin>> b; 
 c=a+b;
 if (c>5) 
{
      cout<< "you win";
}
      else if (c<5)
 {
       cout<< "you loose";
}
        else if (c==5)
{
cout << "you win";
}                                                              //*the end of main
}
                 int option()
 {
	 char choice;
 cout<< "do you want to play again? y/n";
 cin>> choice;
 if (choice=='y')
            {return main();                           //*goes to main
}
                else if  (choice=='n')
{
                return 0;
}
                 else 
{
                 cout<< "not a valid character";
}
 return option();                                        // *loops to function option
 }

@ne555
can you explain?
Last edited on
¡¿did you understand ` vwls r bt s sfl s whtspc, spclly n vrbl nms. s ls: !ndnttn'?!
Indent your code properly.
You're calling option(); but you didn't make sure that the compiler would now what you're doing when it got called. Either move your second function above main or prototype it 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
27
28
29
30
31
32
33
34
#include <iostream>
#include <cstdlib>
using namespace std;
int option(); // Here we are prototyping the function
int main()
{
	int a,b,c;
	a=(rand()%5)+1;
cout<< a;
cout<< "\n";
cout<< "please type a number.";
cin>> b; 
 c=a+b;
 if (c>5) 
 {cout<< "you win";}
 else if (c<5)
 { cout<< "you loose";}
 else if (c==5)
 {cout << "you win";}
}
 int option()
 {
	 char choice;
 cout<< "do you want to play again? y/n";
 cin>> choice;
 if (choice=='y')
 {return main();}
 else if  (choice=='n')
 { return 0;}
 else 
 {cout<< "not a valid character";}
 return option();
 
}
I have attempted to indent it properly for you like I said i'm teaching my self.i have no proper training in a format to indent.
thank Austin I'll do that!
prototyping did not do it it still behaves the same way...
At line 27, you're calling main directly. Don't do that, ever. It's not legal.

If there's some code that you want to execute from main, and also from option, then write a third function and call it from each place that you need to call it.

Edit: it looks like what you're trying to achieve would be best done using a do... while loop.
Last edited on
@OP: http://en.wikipedia.org/wiki/Indent_style
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
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
	int a,b,c;
	a=(rand()%5)+1;
	cout<< a;
	cout<< "\n";
	cout<< "please type a number.";
	cin>> b; 
	c=a+b;
	if (c>5) 
	{
		cout<< "you win";
	}
	else if (c<5)
	{
		cout<< "you loose";
	}
	else if (c==5)
	{
		cout << "you win";
	}
}

int option()
{
	char choice;
	cout<< "do you want to play again? y/n";
	cin>> choice;
	if (choice=='y')
	{
		return main(); //*goes to main
	}
	else if  (choice=='n')
	{
		return 0;
	}
	else 
	{
		cout<< "not a valid character";
	}
	return option(); //*loops to function option
}

As you can see, you are not calling `option()' from `main()'
thanks for your help i got with a friend and this is what we came up with
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
#include <iostream>
#include <cstdlib>
using namespace std;
void option(int);

int main()
{
 int b;
 char choice;
 cout<< "please type a number.";
 cin>> b;
 option(b);
 cout<< "do you want to play again? y/n";
 cin>> choice;
 if(choice=='y')
	{
			while (choice== 'y')
			{
			cout << "Enter another number: ";
			cin >> b;
			option(b);
			cout << "do you wanna play again bro? y/n";
			cin >> choice;
		    }
	}
			
 else if(choice=='n')
		return 0;
 else while(choice != 'y' && choice != 'n') //* makes it ask again
	{
	cout<< "not a valid character";
	cout << "Enter either y or n!";
	cin >> choice;
	}
}
 
 void option(int b)
 {
 int a,c;
	a=(rand()%5)+1;
cout<< a;
cout<< "\n"; 
 c=a+b;
 if (c>=5) 
 {cout<< "you win\n";}
 else if (c<5)
 { cout<< "you loose\n";}
}

it works fine but i tryed imputing another character instead of Y or n and it just terminated
Topic archived. No new replies allowed.