boolean operator ||

This is a discussion on boolean operator || within the C Programming forums, part of the General Programming Boards category; I know similar Q's have ben posted with this problem, and found the answers insightful. But when I enter, for ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    51

    boolean operator ||

    I know similar Q's have ben posted with this problem, and found the answers insightful.
    But when I enter, for eg. 4, I get a response of "Invalid Input"
    when the code reads
    Code:
     if (cInput < 1 || cInput > 10) { //if2
    			printf("Invalid Input!\n");
    		}//if 2
    I take this as
    if 4 is less than 1 (false)
    or
    if 4 is greater than 10(false)

    both are false, so I thought it should move on to the else statement, but I think the program reads (true) therefore "Invalid Input"

    The reason might be becasue I have too many if/else statements, but i dont know how else to do it???
    Anyway, whats wrong with my boolean?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <ctype.h>
    
    int main(){
    	char cInput = '\0';	//place to store input
    	int iRandomNum = 0;	//place to stroe random num
    	
    	srand(time(NULL)); // makes sure num is really random, don't forget
    						//Null. time() has to take a paramter inside the ()
    						// use NULL otherwise it holding garbage
    	
    	iRandomNum = (rand() %10) + 1; // find a rand num btween 1 -10
    									// add 1 becase starts at 0, so really 0-9
    	
    	
    	printf("Please enter a number between 1 - 10: ");
    	scanf("%c", &cInput);
    	
    	if (isdigit(cInput)) { //if1  test if cInput is true
    		if (cInput < 1 || cInput > 10) { //if2
    			printf("Invalid Input!\n");
    		}//if 2
    		else { //else2
    			if (cInput == iRandomNum) { // if3
    				printf("\nSuccess, you guessed right!\n");
    			}	//if3
    			else { //else3
    				printf("\nSorry, the correct answer was %d.\n", iRandomNum);
    			} //else3
    
    		} //else2
    
    	}//if1
    	else {	//else 1
    		printf("You did not enter a digit!\n");
    	}	//else 1
    	
    	return 0;
    }	//main
    thanks

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    cInput is a character not an integer. When you are doing the comparison it's comparing the ASCII code of cInput rather than it's integer value.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    51
    ofcoarse, thank you

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    51
    So I need to store the value in an int aswell.
    I have to keep the idigit check because its part of the assignment.
    So I need to store the int as %d somehow.
    Is there a way to store the input in both %d char and %i int using scanf, without entering it twice and without using pointers.
    Or changing the Ascii char back to a value and putting it in an in iInput to use in my if/else statements?

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Google atoi()

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Take note that atoi is retarded about 0. So if your input can be 0, maybe strtol() is a better choice.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You can also just use int and %d, are there any reason why you want a character? Your dealing only with numbers here after all. Scanf has a return value that you can check if you want to make sure that the input was read correctly.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    51
    int iInput;
    //after scanf("%c", &cInput);
    iInput = atoi (cInput);
    I get a warning "passing argument 1 of 'atoi' makes pointer from integer without cast"

    I tried to typecast
    iInput = atoi ((int)cInput); but this is probably wrong too.

    when run the program I get a bad access report in command window

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    atoi expects a string. However you can also do like this:

    Code:
            ret = scanf("%d", &cInput);
            if(!ret){
                    puts("Expected a number");
                    return 1;
            }

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    51
    @ Subsonics the problem i am working on is
    "Build a number guessing game that uses input validation (isdigit() function) to verify that the user has entered a digit and not a non-digit (letter). Store a random number between 1 and 10 into a variable each time the program is run. Prompt the user to guess a number between 1 and 10 and alert the user if he was correct or not."

    though after reading the question again, I think the solution is to use 2 different inputs,
    1. input a num, then check it is a digit
    2. input a number for a random num guessing game.

    *bangs head on table, I should read the question more carefully

    anyway at least I learnt a little about atoi() for when I come across it in the future.

    Thanks

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Subtract '0' from your character to obtain its equivalent integer.
    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.

  12. #12
    Registered User
    Join Date
    Apr 2010
    Posts
    51
    Subtracting 0 will give the the corresponding Ascii number won't it? i.e. 'A' char , 'A' - 0 = 65.
    I want the original number (entered into the char - isdigit test) turned from char back to int.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Look closely. It says subtract '0', ie the ASCII equivalent for the number 0.
    Since '0' is the ASCII equivalent with 0, it then seems feasible that subtracting '0' would yield 0. And since '1' is '0' + 1... well, you get the point.
    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.

  14. #14
    Registered User
    Join Date
    Apr 2010
    Posts
    51
    Oh yes, very interesting, Thank you Elysia.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Novice needs help
    By ghaasemi in forum C++ Programming
    Replies: 9
    Last Post: 05-30-2009, 08:20 AM
  2. Casting boolean as string
    By doofusboy in forum C Programming
    Replies: 11
    Last Post: 11-10-2005, 11:24 AM
  3. Replies: 4
    Last Post: 04-02-2004, 06:30 PM
  4. Working with boolean...
    By CompiledMonkey in forum C Programming
    Replies: 4
    Last Post: 11-03-2003, 09:39 AM
  5. Use struct to create a boolean type
    By skyglin in forum C Programming
    Replies: 6
    Last Post: 06-18-2003, 08:21 PM

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