What is wrong with this code? please help..

This is a discussion on What is wrong with this code? please help.. within the C Programming forums, part of the General Programming Boards category; this promgram supposed to add, subtract, multiply or divide the input until the user presses 'r', and then after it ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    8

    What is wrong with this code? please help..

    this promgram supposed to add, subtract, multiply or divide the input until the user presses 'r', and then after it will ask the user if he wants to go again.. please help me...


    Code:
    #include <stdio.h>
    #include <float.h>
    int main()
    {
    float num1=0.0, num2;
    char oper, a = 0,exit;
    char response[256] = {0};
    bool final = false;
    
    printf("Calculator is on.\n");
    do
    {
    while ( 1 )
    {
    printf("result = %lf\n", num1);
    
    do
    {
    scanf("%c", &oper);
    } while ( oper == '\n' );
    
    if ( oper == 'r' )
    {
    break;
    }
    
    num2 = FLT_MAX;
    scanf("%f", &num2);
    
    if ( num2 == FLT_MAX )
    {
    continue;
    }
    
    switch( oper )
    {
    case '+':
    num1 += num2;
    break;
    case '-':
    num1 -= num2;
    break;
    case '/':
    num1 /= num2;
    break;
    case '*':
    num1 *= num2;
    break;
    default:
    printf("Unknown operator!\n");
    break;
    }
    }
    
    printf("Final result: %lf\n", num1);
    num1 = 0;
    while ( !final && response[0] != 'y' && response[0] != 'Y' )
    {
    printf("Again? (y/n): ");
    scanf("%s", response);
    if ( response[0] == 'n' || response[0] == 'N' )
    {
    final = true;
    }
    }
    
    } while ( !final );
    return 0;
    }

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    975
    The first problem is that it's not indented.

    The second problem is that you did not specify what the problem is.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,409
    sorry mate that to some members eyes that will look like illiteracy! Its messed up, it looks hacked, perhaps just work one element at a time until they do what you expect and learn from process
    Last edited by rogster001; 10-08-2010 at 03:22 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with my code?
    By x2x3i5x in forum C Programming
    Replies: 6
    Last Post: 09-28-2009, 11:52 AM
  2. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  3. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 09:11 AM
  4. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 05:32 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM

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