Thread: What am i doing wrong??

  1. #1
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168

    What am i doing wrong??

    Hi guys, im new to programming and new to the boards, so bear with me for having such a stupid question to ask of you. Will be taking a programming course in university next year so thought i'd get a head start Anyway....

    I've read about printing text, receiving inputs from the user, loops, functions etc etc.... so i thought i'd get round to making a program of my own. I thought id code it bit by bit to make sure i had the syntax right, but straight away i get errors. This is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int num;
    
    int main()
    {
    
    	printf ("Enter the first number: ");
    
    	scanf ("%d", &num);
    
    	printf (num);
    
    return (0);
    }
    I get two errors:
    - Cannot convert 'Int' to 'const char*' in function main()
    - Type mismatch in parameter '_format' <wanted 'const char*', got 'int' in function main()

    Any ideas? i've looked through the FAQ on the site, and a couple of others and the code looks ok to me, am i missing something obvious? thanks in advance

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int num;
    
    int main()
    {
    
        printf ("Enter the first number: ");
    
        scanf ("%d", &num);
    
        printf ("The number you entered was %d.",num);
    
        return (0);
    }
    Global variables should be avoided where possible.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    you provided the wrong argument for printf(). You should first specifiy the format of your output. To print your number simply use the %d format speicifier (d = decimal). i.e :
    Code:
    printf("Number : %d", num);
    "Number : " will be printed as it is. The %d gets replaced with the value of num. You can display multiple values with printf this way :
    Code:
    printf("%d and %d", num, num2);
    the first %d will be replaced with num's value and the second %d will be replaced with num2's value.


    hope this helps
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  4. #4
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    ah thanks a lot, i thought my problem was with the scanf

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM