CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2019
    Posts
    4

    not printing true message scanf skipping problem in c.

    Code:
    #include<stdio.h>
    int main()
    {
        int cid,name;
        float quantity,rate;
    
        printf("Enter Customer id:");
        scanf("%d",&cid);
        printf("Enter Name:");
        scanf("%d",&name);
        printf("Enter quantity:");
        scanf("%f",&quantity);
        printf("Enter rate");
        scanf("%f",&rate);
    }
    Last edited by 2kaud; June 24th, 2019 at 04:24 AM. Reason: Added code tags

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: not printing true message scanf skipping problem in c.

    You aren't printing any values. What are you expecting to happen?

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: not printing true message scanf skipping problem in c.

    You have name declared as an integer (and reading as an integer).
    I suspect you are not entering an integer value for name.

  4. #4
    Join Date
    Jun 2019
    Posts
    4

    Re: not printing true message scanf skipping problem in c.

    i am printing values
    1. customer id
    2. name
    3.quantity
    4.rate
    problem is that when i enter values after name,it just print quantity and rate.it doesn't allow me to input value of quantity and rate.
    you must run this c program and tell me after that

  5. #5
    Join Date
    Jun 2019
    Posts
    4

    Re: not printing true message scanf skipping problem in c.

    please write correct program.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: not printing true message scanf skipping problem in c.

    As Philip said in post #3, name is defined as an int when it should probably be defined as a char array. Consider:

    Code:
    #include <stdio.h>
    int main()
    {	
        int cid;
        float quantity, rate;
        char name[20];
    
        printf("Enter Customer id: ");
        scanf("%d", &cid);
        printf("Enter Name: ");
        scanf("%19s", name);
        printf("Enter quantity: ");
        scanf("%f", &quantity);
        printf("Enter rate: ");
        scanf("%f", &rate);
    
        printf("Id: %i\nName: %s\nQuantity: %f\nRate: %f\n", cid, name, quantity, rate);
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Jun 2019
    Posts
    4

    Re: not printing true message scanf skipping problem in c.

    Thanks 2kaud for the response but problem is that u should make program with basic c programming not to use array or any derived data types

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: not printing true message scanf skipping problem in c.

    Quote Originally Posted by kapil765 View Post
    Thanks 2kaud for the response but problem is that u should make program with basic c programming not to use array or any derived data types
    name is text - so you need a char array to hold it. You could allocate memory for name using calloc(). The issue with your original code in post #1 is that name is defined as type int. Given the prompt 'Enter Name:', users will type letters which need to be stored and having name as type int isn't correct. You need either name to be a char array or a pointer to allocated memory for the name text.

    Code:
    char* name = (char*)calloc(20, sizeof(char));
    but now you're into dynamic memory etc and you now need to free the allocated memory...
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Aug 2002
    Posts
    756

    Re: not printing true message scanf skipping problem in c.

    Hi,
    Quote Originally Posted by kapil765 View Post
    Thanks 2kaud for the response but problem is that u should make program with basic c programming not to use array or any derived data types
    Is this a school assignment?
    This is wrong - as people explain to you name cannot be integer, unless you are trying to create a program for Nazi's in 1941. ;-)

    Try to enter a number after each prompt and see what happens.

    Thank you.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured