Thread: What is wrong with this code? please help..

  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
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    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,472
    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, 10: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, 06: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