LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 01-21-2012, 04:51 PM   #1
cryingthug
Member
 
Registered: Jun 2009
Posts: 117

Rep: Reputation: 18
Simple IF statement not flowing. Why?


Whenever I select a number that is too low the "else" section runs. If I select a number that is too high the "else" line is not run. Why???

Code:
#include <stdio.h>
#include <ctype.h>

main(){

    int num = 6;
    int answer = 0;

    printf("THIS IS THE NUMBER GUESSING GAME.\n\n");
    printf("Select a number between 1 and 10.\n");
    scanf("%d", &answer);

    if(isdigit(answer))
        if(answer == num){
            printf("You guessed correctly!\n");
        }        
        if(answer < num){
            printf("You guessed too low.\n");
        } 
        if(answer > num){
            printf("You guessed too high.\n");
        } 
        // This line will run no matter what.
        else{
             printf("Please select a number.\n");
    }

}
 
Old 01-21-2012, 05:18 PM   #2
Doc CPU
Member
 
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Ubuntu, Gentoo, Mint, Win 2k/XP
Posts: 521

Rep: Reputation: 124Reputation: 124
Hi there,

Quote:
Originally Posted by cryingthug View Post
Whenever I select a number that is too low the "else" section runs. If I select a number that is too high the "else" line is not run. Why???
because each of the if statements is considered separately, while you seem to consider them related. They're not.

Code:
        if(answer == num){
            printf("You guessed correctly!\n");
        }        
        if(answer < num){
            printf("You guessed too low.\n");
        } 
        if(answer > num){
            printf("You guessed too high.\n");
        } 
        // This line will run no matter what.
        else{
             printf("Please select a number.\n");
    }
If you guess too low, i.e. answer<num, the last of the if statements is not true, so the else branch is executed.
But honestly, what is your intention - in words, not in code? Whatever you enter as answer, it is either less than, or greater than, or equal to num. There is no other situation that you might want to catch with your else statement.
Think again about the logics in your code ...

[X] Doc CPU
 
1 members found this post helpful.
Old 01-21-2012, 10:20 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
An illustrative problem lies at the beginning of the if-else sequence:

Code:
    if(isdigit(answer))
The isdigit() function is meant to take a single char variable--not an int. The purpose of the isdigit() function is to verify that one or more characters in a string contain a digit (i.e. 0-9).

For instance, take the following code:
Code:
#include <stdio.h>

int main( int argc, char *argv() )
{
  char charNumber='9';
  int  intNumber=9;

  if( charNumber == intNumber )
    printf( "The values match!\n" );
  else
    printf( "The values ARE NOT EQUAL!\n" );

  return 0;
}
If you run the above code, you will get "The values ARE NOT EQUAL!" for output. The char representation of 9 is a numeric value from the ASCII table. The int representation of 9 is the binary value of 9.

All of this to say two things:
1. It appears that the program is intended to do some user-input verification.
2. Assuming #1 is true, the "Please select a number." should be moved out of the interior if-else clause to serve as the else for the isdigit()/input verification code.

Last edited by Dark_Helmet; 01-21-2012 at 10:29 PM.
 
1 members found this post helpful.
Old 01-24-2012, 04:30 AM   #4
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 546

Rep: Reputation: 38
Put the block in the same tab and its easier to see the blocks like

{

}



Code:
#include <stdio.h>
#include <ctype.h>

main()
{

    int num = 6;
    int answer = 0;

    printf("THIS IS THE NUMBER GUESSING GAME.\n\n");
    printf("Select a number between 1 and 10.\n");
    scanf("%d", &answer);

    if(isdigit(answer))
    {
        if(answer == num)
        {
            printf("You guessed correctly!\n");
        } 
       
        if(answer < num)
        {
            printf("You guessed too low.\n");
        } 

        if(answer > num)
        {
            printf("You guessed too high.\n");
        } 

    }else
    {
        printf("Please select a number.\n");
    }
}

and you have to fix the isdigit .

Last edited by kalleanka; 01-24-2012 at 04:46 AM.
 
1 members found this post helpful.
Old 01-25-2012, 05:25 AM   #5
chidam
LQ Newbie
 
Registered: Jan 2012
Location: Bangalore
Posts: 4

Rep: Reputation: Disabled
Cool

One small suggestion.
Use else-if in this case. because you are unnecessarily executing if statements
If first if-statements is true then there is no need of executing the following if statements


#include <stdio.h>
#include <ctype.h>

main()
{

int num = 6;
int answer = 0;

printf("THIS IS THE NUMBER GUESSING GAME.\n\n");
printf("Select a number between 1 and 10.\n");
scanf("%d", &answer);

if(isdigit(answer))
{
if(answer == num)
{
printf("You guessed correctly!\n");
}

else if(answer < num)
{
printf("You guessed too low.\n");
}

else if(answer > num)
{
printf("You guessed too high.\n");
}

}else
{
printf("Please select a number.\n");
}
}
 
1 members found this post helpful.
Old 05-20-2012, 12:14 PM   #6
cryingthug
Member
 
Registered: Jun 2009
Posts: 117

Original Poster
Rep: Reputation: 18
Thanks!

Thank you all for your help! I figured it out thanks to you.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Android tablets flowing LXer Syndicated Linux News 0 08-27-2010 08:40 PM
[SOLVED] Shell script for adding a statement in a file after a particular statement Anisha Kaul Programming 4 06-28-2010 03:07 AM
LXer: Requests for Linux drivers flowing in LXer Syndicated Linux News 0 08-14-2009 12:30 PM
help with simple sql statement sekelsenmat Programming 6 08-06-2005 12:01 PM
DV to V4L, how do I get the bits flowing in that direction? oneman00 Linux - Hardware 1 11-23-2004 01:47 PM


All times are GMT -5. The time now is 05:22 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration