• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 431
  • Last Modified:

Help with FUNCTION LOGIC and ARGUMENTS--C Programming

I'm seeking help with a specific problem I'm having with a program I wrote that displays a menu and uses functions to calculate results based on a user's entries. I have 4 functions that add, multiply, divide and subtract, but they are not working exactly right and I not sure what's wrong. I'm also not correctly getting user input. Could someone review my code and enlighten me about my mistakes?

Thanks very much in advance.

----------------------------------------HERE IS MY CODE----------------------------------------------

#include <stdio.h>
char get_choice(void);
char get_first(void);
float get_float(void);
float add();
float divide();
float multiply();
float subtract();
int main(void)
{
    int choice;
   
    while ( (choice = get_choice()) != 'q') //compare choice to 'q' terminator
    {
        switch (choice)
        {
            case 'a' :  add();
                        break;
            case 'd' :  divide();
                        break;
            case 'm' :  multiply();
                        break;
            case 's' :  subtract();
                        break;
            default  :  printf("Program error!\n");
                        break;
        }
    }
    printf("See ya' later alligator!\n");
   
    return 0;
}

    float add() //function 1 (add two numbers)
{
    float sum = 0;
    float n,i;

    printf("Enter first number: ");
    printf("Enter second number: ");
    n = get_float();
    for (i = 1; i <= n; i++)
        printf("%f\n", i);
        sum == n + n;
    while ( getchar() != '\n') //get response, compare to \n
        continue; //skip rest of line
       
    return sum;
}
    float divide() //function 2 (divide two numbers)
{
    float quotient = 0;
    float n,i;

    printf("Enter first number: ");
    printf("Enter second number: ");
    n = get_float();
    for (i = 1; i <= n; i++)
        printf("%f\n", i);
        quotient == n / n;
       
    while ( getchar() != '\n') //get response, compare to \n
        continue; //skip rest of line
       
    return quotient;
}

    float multiply() //function 3 (multiply two numbers)
{
    float product = 0;
    float n,i;

    printf("Enter first number: ");
    printf("Enter second number: ");
    n = get_float();
    for (i = 1; i <= n; i++)
        printf("%f\n", i);
        product == n * n;
    while ( getchar() != '\n') //get response, compare to \n
        continue; //skip rest of line
       
    return product;
}
    float subtract() //function 4 (subtract two numbers)
{
    float difference = 0;
    float n,i;

    printf("Enter first number:\n");
    printf("Enter second number:\n");
    n = get_float();
    for (i = 1; i <= n; i++)
        printf("%f\n", i);
        difference == n - n;
    while ( getchar() != '\n') //get response, compare to \n
        continue; //skip rest of line
       
        return difference;
}
char get_choice(void) //function 5 (get_choice)
{
    float ch;

    printf("Enter the letter for the operation of your choice:\n");
    printf("a. ADD              s. SUBTRACT\n");
    printf("m. MULTIPLY         d. DIVIDE\n");
    printf("q. QUIT\n");
   
    ch = get_first();  //validate user input
    while (  (ch < 'a' || ch > 's') && ch != 'q')
    {
        printf("Please respond with a, d, m, s, or q.\n");
        ch = get_first();
    }
   
    return ch;
}

char get_first(void) //function 6 (get_first letter)
{
    int ch;

    ch = getchar();
    while (getchar() != '\n') //get response, compare to \n
        continue;
       
    return ch;
}

float get_float(void) //function 7 (get float)
{
    float input;
    char ch;
   
    while (scanf("%f", &input) != 1)
    {
        while ((ch = getchar()) != '\n') //get response, compare to \n
            putchar(ch);  // dispose of bad input
        printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
    }
   
    return input;
}
0
computerese
Asked:
computerese
3 Solutions
 
PaulCaswellCommented:
Hi computerese,

There are several problem here.

1.             case 'a' :  add();

You are callint the 'add' function but you are not saving or using the result. Try something like:

    int choice;
    float result; // <<< Add this

Then use:

            case 'a' :  result = add(); // << Do this for all cases.

And after the case statement, something like:

 printf("Result is %f\n", result);

2.         sum == n + n;

This is not doing what you meant it to do. Look at it carefully! :-) There are several others like this.


Paul
0
 
manish_regmiCommented:
some of i found:

if you are returning just 1 float just doing this is fine

float get_float(void) //function 7 (get float)
{
    float input;
    char ch;
   
    scanf("%f", &input);
    return input;
}

  float add() //function 1 (add two numbers)
{
    float sum = 0;
    float n,i;

    printf("Enter first number: ");
    printf("Enter second number: ");
    n = get_float();
   i = get_float()
 
  sum = i + n;
    return sum;
}

similarly do as i have done in add to substract, multiply and divide.

regards
Manish Regmi
0
 
nafis_devlprCommented:
there are several, most of them are logical, you should change the get float method in the following way


float get_float(void) //function 7 (get float)
{
    float input;
   
    while (scanf("%f", &input) != 1)
     {
         printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
         fflush(stdin);
      }
   
    return input;
}

now comes the error detection, the errors are almost same in all the function, i am showing you the add and divide function, change the other functions accordingly

float add() //function 1 (add two numbers)
{
    float sum = 0;
    float n,i;

    printf("Enter first number: ");
    printf("Enter second number: ");//<<------------------------------error
    n = get_float();
    for (i = 1; i <= n; i++)//<<------------------------------error
        printf("%f\n", i);
        sum == n + n;//<<------------------------------error
    while ( getchar() != '\n') //get response, compare to \n
        continue; //skip rest of line
       
    return sum;
}
    float divide() //function 2 (divide two numbers)
{
    float quotient = 0;
    float n,i;

    printf("Enter first number: ");
    printf("Enter second number: ");//<<------------------------------error
    n = get_float();//<<------------------------------error
    for (i = 1; i <= n; i++)//<<------------------------------error
        printf("%f\n", i);
        quotient == n / n;//<<------------------------------error
       
    while ( getchar() != '\n') //get response, compare to \n
        continue; //skip rest of line
       
    return quotient;
}


you should change the functions in the following way

float add() //function 1 (add two numbers)
{
    float sum = 0;
    float n;

    printf("Enter first number: ");
    sum = get_float();
    printf("Enter second number: ");
    sum += get_float();        
    return sum;
}

    float divide() //function 2 (divide two numbers)
{
    float quotient = 0;
    float n;

    printf("Enter first number: ");
    n = get_float();
    printf("Enter second number: ");
     quotient = get_float();
    while(quotient == 0.0)
      {
         printf("divisor cant be zero\nEnter second number: ");
         quotient = get_float();
       }
   
      quotient = n / quotient;
    return quotient;
}


there are some more errors in the get_first() and get_choice() function, change them into following

char get_choice(void) //function 5 (get_choice)
{
    char ch;

    printf("Enter the letter for the operation of your choice:\n");
    printf("a. ADD              s. SUBTRACT\n");
    printf("m. MULTIPLY         d. DIVIDE\n");
    printf("q. QUIT\n");
   
    ch = get_first();  //validate user input
   //while (  (ch < 'a' || ch > 's') && ch != 'q')  this will accept anything inside a to s as a valid input like 'b' or 'c'
    while (  ch != 'a' && ch != 's' && ch != 'm' && ch != 'd' && ch != 'q')
    {
        printf("Please respond with a, d, m, s, or q.\n");
        ch = get_first();
    }
   
    return ch;
}

char get_first(void) //function 6 (get_first letter)
{
    char ch;

    scanf("%c", &ch)
    fflush(stdin);
       
    return ch;
}

Hope this helps

Nafis
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now