Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
June 12th, 2013, 04:27 PM
 Zer0D0wn
Registered User

Join Date: Jun 2013
Posts: 3
Time spent in forums: 22 m 28 sec
Reputation Power: 0
Stopping program from closing at end

This is the second program I'm attempting - just messing around with the various (basic) statements and functions I've learned so far.

This one actually works, but I've just got a couple of questions on how to make it a bit better. here it is:

Code:
```
# include <stdio.h>
# include <stdlib.h>

int main ()
{
int x;
srand(time(NULL));
x = rand() % 20;   //generates a random number 0-20 and stores it as x

printf ("%i\n\n", x); //prints x for testing purposes, to be removed from final version

char lose[50]= "You Lose!";
int guess = 0;
char name[50];

/* below section takes input for name,  displays a welcome message and
then requests keypress of the enter button*/
puts("Hey you, welcome to Guess The Number :)");
gets (name);

printf ("So, your name is %s? Pretty cool! On with the game \n\n", name);
printf("Press Enter\n");
while( getchar() != '\n' );

printf ("Ok, Guess a number between 0 - 20\n ");
scanf ("%i", &guess);  //requests a number 0-20 and stores it as 'guess'

// below section tests for various values of guess relative to the value of x
if (guess > 20)
{
printf("Like, duh! That number is too high!\n");
printf ("%s", lose);
}
else if (guess > x)
{
puts(lose);
}
else if (guess < x)
{
puts(lose);
}
else if (guess == x)
{
puts ("You got it right - good job!");
puts ("YOU WIN!!!!!!!");
}
else { puts ("That wasn't even a number bro!"); puts(lose);} //catch-all if user does not input a number
return 0;
}
```

so, the questions

1. When I run the .exe by double clicking it, the program loads in a black console like box. At the end of the program it dissapears. How can I get it to remain open?

2. my 'else' clause doesn't work - I'm guessing it has something to do with 'guess' being formatted as a number, so if characters are input they are interpreted as being their number equivalent? Any ideas on how I can get the else clause working correctly?

3, and lastly. The program only works for a single guess. I'm thinking that If I wanted the program to run continuously (i.e, allow a set number of guesses or automatically restart after the end of each game) then this would require a total redesign? any pointers on how to do this would be much appreciated

Peace and love

ZD

#2
June 14th, 2013, 11:20 AM
 MBirchmeier
I <3 ASCII

Join Date: Aug 2003
Posts: 2,399
Time spent in forums: 1 Month 2 Weeks 2 Days 21 h 23 m 18 sec
Reputation Power: 1232
Quote:
 1. When I run the .exe by double clicking it, the program loads in a black console like box. At the end of the program it dissapears. How can I get it to remain open?

9 times out of 10 I print "Program complete... press enter to close" and throw one last scanf at the end.

Quote:
 2. my 'else' clause doesn't work - I'm guessing it has something to do with 'guess' being formatted as a number, so if characters are input they are interpreted as being their number equivalent? Any ideas on how I can get the else clause working correctly?

it's been a while since I've done raw C/C++ but the most safest method I'm familiar with is to input a string instead of a integer in your scanf. Then see if it can be converted to an integer

Quote:
 3, and lastly. The program only works for a single guess. I'm thinking that If I wanted the program to run continuously (i.e, allow a set number of guesses or automatically restart after the end of each game) then this would require a total redesign? any pointers on how to do this would be much appreciated

Research for, or while loops.

-MBirchmeier
__________________
My fiancee's transition from accountant to writer
0x4279 7465 204D 6521

 Viewing: Dev Shed Forums > Other > Beginner Programming > Stopping program from closing at end