Thread: mvc++ 6.0 scanf probs

  1. #1
    Effigy
    Guest

    mvc++ 6.0 scanf probs

    I'm just starting and working on some very easy programs. I was doing a few little programs that use the scanf() function, such as taking the input of 4 numbers and printing out the average or a program that subtracts 2 numbers from one another, and couldnt get anything to work. I'm using ms visual c++ 6.0 on win98.

    So then I tried this program:
    #include <stdio.h>

    main()
    {
    int a;
    scanf("enter a value %d ", &a);
    printf("a contains %d ", a);
    return 0;
    }

    The value of a (and all values I try scan f'ing in) ends up being something ridiculous like -823460123. Any ideas what is up with this?

    http://cboard.cprogramming.com/showt...t=scanf+result This thread says scanf has problems...is this my case?

    I've tried the suggestions in these threads:
    http://cboard.cprogramming.com/showt...t=scanf+result

    and another 1 I cant find the link to now. Any help appreciated.

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    This is how you should use scanf:
    Code:
    int main(void){
        int a = 0;
        printf("Enter a value ");
        scanf("%d", &a);
        printf("a is %d\n", a);
        return 0;
    }
    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>This is how you should use scanf:
    You should also check the return code from scanf() to ensure it did what you asked it to.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Effigy
    Guest
    Thanks guys, that seemed to do it. Is it because I wasn't initiallizing a to 0?

  5. #5
    Registered User purple's Avatar
    Join Date
    Mar 2002
    Posts
    28
    No, your major problem is that you are trying to use scanf for both input and output.

    scanf: inputs (scans in) values
    printf: outputs (prints out) values and/or strings of text

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling x64 code in VC++ 6.0?
    By cpjust in forum Windows Programming
    Replies: 7
    Last Post: 07-29-2008, 09:36 AM
  2. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 07:28 AM
  3. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM
  4. Visual Studio 6.0 Help
    By L_I_Programmer in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2003, 10:35 PM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM