Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with FUNCTION LOGIC and ARGUMENTS--C Programming

Posted on 2006-07-19
3
Medium Priority
?
429 Views
Last Modified: 2010-04-15
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
Comment
Question by:computerese
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 16

Assisted Solution

by:PaulCaswell
PaulCaswell earned 500 total points
ID: 17142035
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
 
LVL 8

Assisted Solution

by:manish_regmi
manish_regmi earned 500 total points
ID: 17143077
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
 
LVL 7

Accepted Solution

by:
nafis_devlpr earned 1000 total points
ID: 17144637
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question