Hi peoples! im sort of new to programming, been at it for maybe a week. Nevertheless, i know enough functions to make some cool stuff. I need help with a weird error.... Well first of all im making sort of an operating enviormnet, which i call mini-os. There is like a kernel application that has a bunch of if statements that let you call on this app or that game. The problem is is that if i use the kernel to run, say, my Randoguess, the game doesnt run properly. Here is the piece of code around it. (there is more code, but it is irrelevent):
if(x == 3)
{
printf("- Which game do you want to play? -\n");
printf("A list of games:\n11 - Noah's Number Guess\n21 - Owen's Menu game\n31 - Bryan's R-P-S game\n");
//printf("- Press 9 for a list of games.\n");
printf("./root/Games> ");
scanf_s("%i", &y);
//if(y == 9)
//{
//printf("A list of games:\n11 - Noah's Number Guess\n21 - Owen's Menu game\n31 - Bryan's Maze game (not finished)\n");
//printf("./root/Games> ");
//}
if(y == 11)
{
result = system("start ./system/guessgame.exe");
}
if(y == 21)
{
result = system("start ./games/menugame.exe");
}
if(y == 31)
{
result = system("start ./games/rps.exe");
}
cin.get();
}
The games execute, but in the case of the number game, it wont work properly. Here is its code:
{
int z = 0;
int x;
//int y;
srand ( time(NULL) );
int y = rand() % 1000 + 1;
printf("=-------------------------------=\n=- Randoguess: The Number Game -=\n=-------------------------------=\n");
printf("=-The object of this game is to guess the right number.\n=-To start, guess a number between 1 and 1000.\n=-The rest is simple. Have fun!\n");
gets the newline that's left in the stream. Next, you try entering 1000 again but the program is actually at this cin.get:
1 2 3
cin.get(); //now the program's at this one
z = z + 1;
} while (x != y);
This cin.get grabs the 1 at the beginning of your input (1000). Then, the scanf_s call at the beginning of the loop takes the rest of the input (000) and sets it to x. x will now be zero so it will be too low. Solution: get rid of all your calls to cin.get();.
Howevver, I assume you want to wait for the user to click enter before the program terminates, so after you print "You got it!..." and the number of tries, you should call cin.ignore(); to remove the newline from the stream and then call cin.get(); to wait for the user to hit enter.
Thanks!! that helped a ton, but the real problem i found was in the result = system("myprogram.exe) . When i used that to call on my number game, the game was still running in the first program. And since the first program didnt have the #include <time.h> on top. So it wasnt generating a random number....
But it works now! i did find a problem with cin.get(), and it was just amissing semicolon.