Thread: cannot Scanf float? terminates program

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    9

    cannot Scanf float? terminates program

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<process.h>
    #include<ctype.h>
    int i;
    void main()
    {clrscr();
    struct SIZE
    {
    int pr;
    int sz;
    };
    struct STOCK
    {
    int style;
    float price;
    struct SIZE s;
    }x[10];
    
    for(i=0;i<2;i++)
    {
        printf("Enter Style: ");
        scanf("%d", &x[i].style);
        printf("Enter Size: ");
        scanf("%d", &x[i].s.sz);
        printf("Enter Pairs: ");
        scanf("%d", &x[i].s.pr);      
        printf("Enter price: ");
        scanf("%f", &x[i].price);     //Program terminates here.
    }
    
    printf("Stock no.\tStyle no.\tSize\t\tPairs\t\tPrice\n");
    
    for(i=0;i<2;i++)
    {
        printf("%d\t\t%d\t\t%d\t\t%d\t\t%f\n", i+1, x[i].style, x[i].s.sz, x[i].s.pr, x[i].price);
    }
    getch();
    Last edited by hirano; 03-08-2010 at 10:54 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    http://cboard.cprogramming.com/dos-p...ead-first.html
    SourceForge.net: Void main - cpwiki

    >>for(i=0;i<2;i++)
    >>{printf("Enter Style: ");

    AVOID THIS! Either do

    for(i=0;i<2;i++) {
    printf("Enter Style: ");

    ...or...

    for(i=0;i<2;i++)
    {
    printf("Enter Style: ");

    And don't forget to indent!

    And then let's start again by telling us what, exactly, the problem is.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    9
    Okay. Can u help me ? XD

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Read through my first reply again. Perform the series of steps outlined and then reply again with the necessary information.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    9
    it doesnt indent what to do?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Use code tags! See link.
    I put them there in the first place so you would click them and read the material written there.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    9
    Sorry mam/sir, it's my first time here.

    Gonna edit now. Thanks

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I would recommend you read stickies in a forum you're new to before posting.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    9
    Okay i'll read later, there I put code tags already.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then the next step would be to mention what the problem is.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    9
    I put a comment on the program.

    Anyway, the problem is the program terminates whenever I'm about to input the Price w/c is a float.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The code runs fine for me.
    I suggest you demonstrate an example of how to reproduce this. For example, what do you input to make this happen?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Mar 2010
    Posts
    9
    I just put random numbers and I've tried different combination but still at the price part the program terminates.

    btw, I use Turbo C++ IDE. ver. 3.0 Borland Int'l Inc.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I suggest you dump Turbo C++ and get yourself a modern compiler and IDE.
    SourceForge.net: Integrated Development Environment - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Mar 2010
    Posts
    9
    Okay, where can I get modern compiler?

    The easier to use. XD

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. errors in multi-selection program
    By volk in forum C Programming
    Replies: 5
    Last Post: 02-24-2003, 08:27 PM
  5. ~ Array Help ~
    By Halo in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 04:19 PM