Repeating a Program
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 81 82
|
#include <iostream>
#include <time.h>
#include <string>
using namespace std;
void retry();
void retry()
{
int y;
cout << "Hit the Y key then ENTER to close the program, otherwise, press just press ENTER.";
cin >> y;
while(y==y)
{
}
}
void mainfunc();
void mainfunc()
{ int x;
int a;
string name;
srand((unsigned int)time(NULL));
a = rand()%10+1;
cout << "Enter your name, player! \n";
cin >> name;
cout << "\n Welcome, ";
cout << name;
cout << " \n";
cout << " \n";
cout << "Guess the number between 1 and 10! It changes every time!: ";
cin >> x;
if (x>a){ cout << "The number is lower \n"; cout << "YOU FAIL!, "; cout << name;
cout << " \n";
cout << " \n";}
if (x<a){ cout << "The number is higher \n";
cout << "YOU FAIL!, "; cout << name;
cout << " \n";
cout << " \n";}
if (x==a){ cout << "WINNER IS YOU \n";}
cout << " \n";
}
int main ()
{
mainfunc();
system("PAUSE"); // ---Deal with it---
return 0;
}
|
There is my code, I want to repeat the function 'mainfunc()'. I don't know how. I think I have to use a while loop or if function but i'm not sure.
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 81 82 83 84 85 86 87 88 89 90 91
|
#include <iostream>
#include <time.h>
#include <string>
using namespace std;
void retry();
void retry()
{
int y;
cout << "Hit the Y key then ENTER to close the program, otherwise, press just press ENTER.";
cin >> y;
while(y==y);
{mainfunc();}
else() // Line 16
{break;}
}
void mainfunc();
void mainfunc()
{ int x;
int a;
string name;
srand((unsigned int)time(NULL));
a = rand()%10+1;
cout << "Enter your name, player! \n";
cin >> name;
cout << "\n Welcome, ";
cout << name;
cout << " \n";
cout << " \n";
cout << "Guess the number between 1 and 10! It changes every time!: ";
cin >> x;
if (x>a){ cout << "The number is lower \n"; cout << "YOU FAIL!, "; cout << name;
cout << " \n";
cout << " \n";}
if (x<a){ cout << "The number is higher \n";
cout << "YOU FAIL!, "; cout << name;
cout << " \n";
cout << " \n";}
if (x==a){ cout << "WINNER IS YOU \n";}
cout << " \n";
}
int main ()
{
mainfunc();
retry();
system("PAUSE"); // ---Deal with it---
return 0;
}
|
Look at line 16. How do I get these else things to work. I tried looking on this site, but I find it hard to find the tutorial.
Topic archived. No new replies allowed.