Results 1 to 3 of 3

Thread: input help

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    10
    Thanks
    0
    Thanked
    0 times in 0 posts

    Post input help

    Hello

    First post so hoping you can help.

    Having problems with a bit C coding.

    Trying to return error when ever inserting a letter or nothing at all.

    The code is below, looking for help with the bolded line.


    Code:
    /*******************************************************************************************************************
     Author: Josh Brown
     Date: 28/2/09
     Purpose: Speed Camera
    *******************************************************************************************************************/
    
    #include <stdio.h>
    
    int main ()
    {
     const int LIMIT = 59, MAX1 = 70, MAX2 = 90; 
     int speed;
    
     /*Asks User for Speed*/
     printf ("Please enter speed: ");
     scanf ("%d%*c", &speed);
    
     if (speed <= LIMIT)
      {printf("Not Speeding");}
     else
      if  ( ( speed >= LIMIT ) && ( speed < MAX1 ) )
       {printf("Speeding 1 to 10 Km/h over the limit. Fine is $80");}
      else
       if  ( ( speed >= MAX1 ) && ( speed < MAX2 ) )
        {printf("Speeding 11 to 30 Km/h over the limit. Fine is $150");}
       else
        if (speed >= MAX2)
         {printf("Speeding 30+ Km/h over the limit. Fine is $500");}
        else
          {printf("You did not enter a valid speed");};
    
    	return(0);
    }
    
    }
    Thanks

    Josh

  2. #2
    Senior Member
    Join Date
    Sep 2004
    Posts
    371
    Thanks
    44
    Thanked
    10 times in 9 posts

    Re: input help

    There are two problems that are causing the error message not to be printed:
    1) You're not initialising the speed variable and as a result it will contain junk data. When I ran your code I happened to get -1073743700.
    2) Your if statement accepts all integer values, positive and negative.

    To solve your problem you need to initialise the speed variable to a negative number and you need to check for numbers less than 0. This will mean that if the user enters a negative number (e.g. -10) the speed variable will be set to this number and the < 0 condition in your if statement will be satisfied, thus printing the error message. If the user enters something that isn't a number (e.g. abc) then the speed variable will not be set. However, since it already contains a negative number the error message will be printed.

    I have made the changes to your code so you can see what I mean. I have also made some formatting changes which make it easier to read and I've changed your constants to #defines at the top of the file since this is good practice for constant values. This helps with maintainability of your code.

    Code:
    #include <stdio.h>
    
    #define LIMIT 60
    #define MAX1 70
    #define MAX2 80
    
    int main(void)
    {
        int speed = -1;
        
        /* Asks User for Speed */
        printf("Please enter speed: ");
        scanf("&#37;d%*c", &speed);
        
        if(speed < 0)
        {
    	printf("You did not enter a valid speed\n");
        }
        else if(speed < LIMIT)
        {
    	printf("Not Speeding\n");
        }
        else if(speed < MAX1)
        {
    	printf("Speeding 1 to 10 Km/h over the limit. Fine is $80\n");
        }
        else if(speed < MAX2)
        {
    	printf("Speeding 11 to 30 Km/h over the limit. Fine is $150\n");
        }
        else
        {
    	printf("Speeding 30+ Km/h over the limit. Fine is $500\n");
        }
    
        return 0;
    }
    Welcome to Hexus

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    10
    Thanks
    0
    Thanked
    0 times in 0 posts

    Re: input help

    Thanks for you help mate

    I took some of your ideas and put them into my code and got what i wanted.

    Thanks

    Josh

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. No Input Signal - Display problem.
    By Jonathong in forum Help! Quick Relief From Tech Headaches
    Replies: 21
    Last Post: 17-04-2008, 11:48 AM
  2. "USB Input (decoding only)" What does this mean?
    By christiani in forum Help! Quick Relief From Tech Headaches
    Replies: 6
    Last Post: 03-04-2007, 04:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •