please help.

This is a discussion on please help. within the C Programming forums, part of the General Programming Boards category; hello guys. im in need of some urgent help over here. the case so far is like this: the code ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    9

    please help.

    hello guys. im in need of some urgent help over here. the case so far is like this:
    the code below already checks if the user inputs a letter, it will be discarded. only numbers allowed.

    now i need to enter another checking solution such as if (guess[0]>6)
    in other words. i would like the invalid entry to pop up if a number aboe 6 is entered.

    could someone please help me implement this into my code below. ive tried many solutions but all not working fine..

    Code:
    valid1 = 0;
    while (!valid1) {	
    	printf("ENTER GUESS 1: ");
    	fflush (stdout);
    	if (scanf("%d",&guess[0]) == 1)
    	valid1 = 1;
    	else
    	printf("INVALID ENTRY, PLEASE TRY AGAIN\n\n");
    	while (getchar() != '\n');
    }
    many thnx to anyone who helps.
    greatly appreciated.

    PS. sorry about the heading i was going to add more details but clicked on post too quickly.
    Last edited by oli3; 01-15-2008 at 02:53 PM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    Indenting properly is a good idea.
    Code:
    valid1 = 0;
    while (!valid1) {	
    	printf("ENTER GUESS 1: ");
    	fflush (stdout);
    	if (scanf("%d",&guess[0]) == 1)
    		valid1 = 1;
    	else
    		printf("INVALID ENTRY, PLEASE TRY AGAIN\n\n");
    	while (getchar() != '\n');
    }
    But how can it NOT work? What did you try?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    9
    Quote Originally Posted by Elysia View Post
    Indenting properly is a good idea.
    But how can it NOT work? What did you try?
    the above code works 100%!!!

    its just that i would like it to print out the invalid entry message to a number greater than 6 aswell in addition.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    9
    ITS SORTED!!
    solved it.!!!!!!!!!!!!!!!!!!

    for future references to anyone.
    i just added this line below the existing if statement:
    Code:
    if (guess[0] <= 6) //if loop checks if input is smaller or equal to 6

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    You can use if/else if.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21