1. Welcome! Please take a few seconds to create your free account to post threads, make some friends, remove a few ads while surfing and much more. ClutchFans has been bringing fans together to talk Houston Sports since 1996. Join us!

help from C programmers

Discussion in 'BBS Hangout' started by Steve_Francis_rules, Mar 9, 2003.

  1. Steve_Francis_rules

    Joined:
    Dec 11, 1999
    Messages:
    8,467
    Likes Received:
    300
    Hi everyone. It's been a while since I've posted because now that I'm in college I seem to be much more busy. I have a C programming class, and my 3rd assignment (due Wednesday) is to create a program that will basically emulate a calculator.
    The assignment is describedhere .

    I am having 2 problems: the first problem is that my error message in the scan_data function prints every time except for the first, even when a valid operator is entered. My second problem is that my accumulator variable is not accumulating the total, once it is set to 1 or 0, it is staying at 1 or 0. If anyone can offer any help, I would be very grateful.

    I don't know if this makes any difference, but I code in unix, not in windows.

    my code:

    #include <stdio.h>
    #include <math.h>


    double accumulator;
    double operand;
    char operator;
    int i = 1;

    double scan_operand;
    char scan_operator;

    void scan_data(char *operator, double *operand);
    void do_next_operation(char *operator, double *operand, double *accumulator);
    double factorial(double operand, double accumulator);


    int
    main (void)
    {
    operator = 0;
    operand = 1;

    while(operator != 'q' && operand != 0.0){

    scan_data(&operator, &operand);

    if (i == 1)
    if (operator == '+' || operator == '-')
    accumulator = 0;
    else accumulator = 1;


    i = i + 1;


    if (operator = '!')

    factorial(operand, accumulator);
    else {



    do_next_operation(&operator, &operand, &accumulator);
    }

    printf("\n");
    printf("The accumulated value is %lf", accumulator);
    }

    return(0);

    }

    void
    scan_data (char *operator, double *operand)
    {
    double scan_operand;
    char scan_operator;


    printf("\n");
    scanf("%c %lf", &scan_operator, &scan_operand);

    while (scan_operator != '+' && scan_operator != '-' && scan_operator !=
    '*' && scan_operator != '/' && scan_operator != '!' &&
    scan_operator != '^' && scan_operator != 'q'){
    printf("An invalid operator has been entered, please enter a new
    operator\n");
    scanf("%c %lf", &scan_operator, &scan_operand);
    };

    *operator = scan_operator;
    *operand = scan_operand;
    }

    void
    do_next_operation(char *operator, double *operand, double *accumulator)
    {

    switch(*operator){

    case '+':
    *accumulator = *accumulator + *operand;
    break;

    case '-':
    *accumulator = *accumulator - *operand;
    break;
    case '*':
    *accumulator = *accumulator * *operand;
    break;
    case '/':
    *accumulator = *accumulator / *operand;
    break;
    case '^':
    *accumulator = pow(*accumulator, *operand);
    break;
    }
    }

    double
    factorial(double operand, double accumulator)
    {
    int i;

    if (operand <0)
    printf("The factorial of a negative number is not defined");

    else if (operand ==0)
    accumulator = 1;
    else {
    accumulator = 1;
    operand = (int)operand;

    for(i = operand; i > 1; i = i - 1){
    accumulator = accumulator * operand;
    }
    }

    return(accumulator);
    }
     
  2. DCkid

    DCkid Contributing Member

    Joined:
    Oct 15, 2001
    Messages:
    9,570
    Likes Received:
    2,528
    ugh...pointers. Sorry, I haven't worked with C in a while.

    Have you tried printing out the scan_operator variable just to see if it's somehow being set wrong, which is causing the error message to be printed out?

    As for your second problem, I'm not sure. The only thing I can think of is that the operator is somehow not being set right, so none of the case statements are being executed in the do_next_operation function.

    also, did you only want one equals sign right here, or two: <b>if (operator = '!') </b>

    Sorry I couldn't be of any more help. I've pretty much been programming exclusively in Java the last two years and my I've let my C skills slip a little. :eek:
     
  3. Steve_Francis_rules

    Joined:
    Dec 11, 1999
    Messages:
    8,467
    Likes Received:
    300
    Thanks for the help. My accumulator is now working, the only problem was that I had forgotten to put if if(operator == '!')
    I'm going to also take your advice and print the scan_operator to see if the value is set wrong.
     
  4. Steve_Francis_rules

    Joined:
    Dec 11, 1999
    Messages:
    8,467
    Likes Received:
    300
    Ok, I tried printing out the scan_operand and scan_operator to see if the values were not being assigned correctly. For some reason, scan_operator has no value assignmed to it after the first loop, and scan_operand maintains its value from the first loop. The thing I don't understand is that, despite this problem, the rest of the program is working correctly, I just keep getting that error message when I shouldn't be.

    If anyone else has an idea of what may be the problem, I would greatly appreciate any help you can offer.
     

Share This Page

  • About ClutchFans

    Since 1996, ClutchFans has been loud and proud covering the Houston Rockets, helping set an industry standard for team fan sites. The forums have been a home for Houston sports fans as well as basketball fanatics around the globe.

  • Support ClutchFans!

    If you find that ClutchFans is a valuable resource for you, please consider becoming a Supporting Member. Supporting Members can upload photos and attachments directly to their posts, customize their user title and more. Gold Supporters see zero ads!


    Upgrade Now