need help with basics!

This is a discussion on need help with basics! within the C Programming forums, part of the General Programming Boards category; I have just begun C programming half an hour ago and have gotten a problem already. After the introductory tutorial ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    6

    need help with basics!

    I have just begun C programming half an hour ago and have gotten a problem already.

    After the introductory tutorial here I tried writing the simple code that just returns the number that has been input ("Enter your number" ---- "Your number was :"), but keep getting just a single number.

    If I choose the variables to be integers I keep getting -36 as the output number all the time, and if the variables are made to be floating point numbers the output remains -38 all the time.

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    post code and i'll try to explain what your code is doing wrong
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Please post your code. We can't quite figure out what mistake was made without seeing it.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    6
    Code:
    #include<stdio.h>
    int main()
    {
    int number;
    
    printf("Enter your number:\n");
    scanf("%d", &number);
    
    printf("Your number was %d", &number);
    
    getchar();
    
    }
    I did it just the way in the tutorials.

  5. #5
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    you dont need & for printf
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    6
    Thanks! That worked!

    This programming thing is decent deal you know.

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    6
    This thing messed up again. What's wrong with this program now?

    Code:
    #include<stdio.h>
    main()
    {
    int a=0;
    int b=0;
    
    printf("Enter two numbers \n");
    scanf("%d,%d",&a,&b);
    
    printf("Your numbers are %d,%d",a,b);
    
    getchar();
    
    }
    The first number comes ou alright but the second one always remains zero. I think something's wrong with the scanf statement.

  8. #8
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    get rid of the comma i.e

    scanf("%d,%d",&a,&b);

    should be

    scanf("%d %d", &a, &b);

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    6
    On similar lines...

    Code:
    #include<stdio.h>
    main()
    {
    int a;
    int b;
    
    printf("Enter two numbers\n");
    scanf("%d,%d",&a,&b);
    
    printf("Your numbers are %d,%d",a,b);
    getchar();
    
    }
    NOTE that I haven't put in 'int a=0' and 'int b=0' in this case.
    This code gives the first number correctly but the second number is always 187 (this must be some sort of a place value of the stored data).

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    6
    Thanks man. That is working.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    printf("Enter two numbers separated by a comma\n");
    scanf("%d,%d",&a,&b);

    printf("Enter two numbers separated by a colon\n");
    scanf("%d:%d",&a,&b);

    printf("Enter two numbers separated by a space\n");
    scanf("%d %d",&a,&b);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Learned the basics, SO ?
    By TuxPayne in forum C Programming
    Replies: 4
    Last Post: 09-11-2005, 12:32 PM
  2. "Modern C++ and Basics of OO Programming" (e-learning course)
    By Erhard Henkes in forum C++ Programming
    Replies: 5
    Last Post: 09-16-2004, 03:01 PM
  3. C++ Basics part two
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 11-22-2003, 10:34 AM
  4. Desire & Fear: Two Basics of Human..
    By zahid in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 11-16-2002, 10:11 PM
  5. The Basics
    By Granger9 in forum Windows Programming
    Replies: 5
    Last Post: 09-13-2002, 05:12 PM

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