Thread: Error

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    19

    Error

    I got my first program to work but it has a little bug in it.


    What Could be the problem? would i need a few more printfs?

    Here the code:

    Code:
    #include <stdio.h>
    
    int main()
    
    {
       int a, b, c, d;
       
       printf( "Please enter five numbers between 1 and 10\n" );
       scanf( "%d", &a,&b,&c,&d); /* sets the numbers u inputed */
       if ( a== 1 || b == 1 || c == 1 || d == 1 || a == 2 || b == 2 || c == 2 || d == 2 || a == 3 || b == 3 || c == 3 || d == 3 || a == 4 || b == 4 || c == 4 || d == 4 || a == 5 || b == 5 || c == 5 || d == 5 ) {
            printf( "Wow your as smart as me\n" );
           
            }
           
            else if (a == 6 || b == 6 || c == 6 || d ==6 || a == 7 || b == 7 || c == 7 || d == 7 || a == 8 || b == 8 || c == 8 || d == 8 || a == 9 || b == 9 || c == 9 || d == 9 || a == 10 || b == 10 || c == 10 || d == 10 ) {
                  printf( "WOW You smarter than me\n" );
                 
                  }
                  else {
                        printf( "OMFG u thought of %d, %d, %d, %d\n", a, b, c, d );
                        }
                       
                        system("pause");
                        return 0;
                       
                        }
    Last edited by Kaho; 08-18-2005 at 06:38 PM.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    int main()
    {
    	int a,b,c,d;
    	
    	printf("Enter 4 space seperated numbers: ");
    	scanf("%d %d %d %d", &a, &b, &c, &d);
    
            // Do some stuff with a b c d here
    
    	printf("You entered: %d %d %d %d\n", a, b, c, d);
    	return 0;
    }
    Random values from somewhere off the stack or something are being stuffed into b, c, d using your code. You need to provide a proper format string to scanf for what you want, which would be "%d %d %d %d". You might find this MSDN entry somewhat interesting: http://msdn.microsoft.com/library/de...2c_.wscanf.asp

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    19
    Oh Thank u i was looking into that last night but i was looking at something else

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Tonto gave the correct solution, but his explanation of what was happening in the original code is incorrect. The scanf() call is only reading one value (the first of the four you input), and putting it into the variable a. The other variables are never initialised, so contain junk and are never modified by the scanf() call.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > printf( "Please enter five numbers between 1 and 10\n" );
    > scanf( "%d", &a,&b,&c,&d); /* sets the numbers u inputed */
    How about learning to count?
    You ask for 5
    You have 4 variables
    You scanf for just one one them
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    19
    Quote Originally Posted by Salem
    > printf( "Please enter five numbers between 1 and 10\n" );
    > scanf( "%d", &a,&b,&c,&d); /* sets the numbers u inputed */
    How about learning to count?
    You ask for 5
    You have 4 variables
    You scanf for just one one them
    Its all fixed and about the coutning issue i ment four but i hit 5 instead i was a little tired and didnt know what i was suppose to be doing.

    But anyways its all good now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM