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.
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
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
#include <iostream>
#include <cstdlib>
usingnamespace 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";}
elseif (c<5)
{ cout<< "you loose";}
elseif (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();}
elseif (choice=='n')
{ return 0;}
else
{cout<< "not a valid character";}
return option();
}
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.