#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Smile Stopping program from closing at end


    Ok, so back already!

    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 :)");
    	puts ("What's your name?");
    	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("Your guess was too high!");
    		puts(lose);
    	}
    	else if (guess < x)
    	{
    		puts("Your guess was too low!");
    		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


    Thank you in advance dudes!!

    Peace and love

    ZD
  2. #2
  3. I <3 ASCII
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Aug 2003
    Posts
    2,400
    Rep Power
    1233
    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.

    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

    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Location
    WA
    Posts
    26
    Rep Power
    0

    string to integer


    What are some ways to convert a string to an integer? I once got an error telling me the value returned was not integral. I was convinced it was definitely integral to my program. After much searching I realised I needed to return an integer, and so I did. Being able to convert strings of letters, symbols, or other things like colors or ideas to integers intrigues me.
    If this sounds strange, a neighbor of mine wrote a program which, among other things, asked what to do next. If you said "shoot the horse", you were told, "Now you must walk". Any help would be greatly appreciated.

IMN logo majestic logo threadwatch logo seochat tools logo