Calc Problem.

This is a discussion on Calc Problem. within the C Programming forums, part of the General Programming Boards category; Whats wrong with this code? It compiles fine but when I run it I type in the first # and ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    70

    Question Calc Problem.

    Whats wrong with this code? It compiles fine but when I run it I type in the first # and it crashes...

    main()
    {
    float first,second,exit,equal;

    printf("Please Enter The # You Want Added : ");
    scanf("%d",first);
    printf("Please Enter The Second # : ");
    scanf("%d",second);
    equal=first+second;
    printf("\nThe Answer to %d + %d = %d",first,second,equal);
    printf("This Program is finished. If you would like to quit type End and press enter now.");
    scanf("%d",exit);
    }
    Attached Files Attached Files

  2. #2
    Unregistered
    Guest
    exit is a float and you enter END? Also, why are you using %d with floats?

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    85
    You need read to learn with basic data types in C first before
    start to write a C program. Read closely with chapters talking
    about C data types such as float, char, int,....
    Basicly:
    %d with int
    %f with float
    %c with char
    %s wiht string
    Last edited by dv007; 06-02-2002 at 08:57 PM.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    umm the reason your program is crashing is probably because your not saving the read in int, double whatever using the memory address, you've got:
    Code:
    main() 
    { 
    float first,second,exit,equal; 
    
    printf("Please Enter The # You Want Added : "); 
    scanf("%d",first); 
    printf("Please Enter The Second # : "); 
    scanf("%d",second); 
    equal=first+second; 
    printf("\nThe Answer to %d + %d = %d",first,second,equal); 
    printf("This Program is finished. If you would like to quit type End and press enter now."); 
    scanf("%d",exit); 
    }
    and it should be:

    Code:
    main() 
    { 
    float first,second,equal,exit;
    
    printf("Please Enter The # You Want Added : "); 
    scanf("%f",&first); 
    printf("Please Enter The Second # : "); 
    scanf("%f",&second); 
    equal=first+second; 
    printf("\nThe Answer to %f + %f = %f",first,second,equal); 
    printf("This Program is finished. If you would like to quit type End and press enter now."); 
    scanf("%f",&exit); 
    }
    also I'm not sure what your trying to do with the last few lines, you should be using a string and doing a strcmp to see if they typed end. Hope this helps

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    70
    Thanks, really.
    and it may look sloppy the way I end it but
    heres my reasoning
    When I was learning I was doing the hello world and it would just close but then I saw scanf and that would stop it becuase not all the funtions were done so I got used to it plus Ive been programming for 'bout a week so I dont have eveything down.
    like I forgot that you call float different then int and char ect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM

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