Thread: If function

  1. #1
    Registered User
    Join Date
    Jan 2022
    Posts
    5

    If function

    So I'm trying to make a calculator that has 2 modes simple and scientific. The simple one will be only for +, -, /, *. And in this case, a request will be made to the operator to know the number of elements to use (min: 2, max: 5). I'm still working on the simple mode but I have apparently the "If" function is not really recognized in my programming... I'm still new to C++ so maybe I don't understand the function really well

    here's the code
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    
    
    
    
    void viderBuffer(void);
    int main()
    {
        char operation;             //normal datatype intro
        float n1,n2,n3,n4,n5;
        int total;
    
        printf("Veuillez choisir un operateur  (+, -, *, /): \n"); //part to ask how what operator and how many operation
        scanf("%c", &operation);
        viderBuffer();
        printf("Vous voulez faire combien d'operation?\n");
        scanf("%d", &total);
    
    
        //case for 2 operation choosed
        if(total=='2')
        {
            printf("Veuillez saisir le premier chiffre: \n");
            scanf("%f", &n1);
            viderBuffer();
            printf("Veuillez taper le deuxieme chiffre: \n");
            scanf("%f", &n2);
            viderBuffer();
    
         /*   switch(operation)
        {
            case '+':
                printf("%.1f + %.1f = %.1f",n1, n2, n1+n2);
                break;
    
            case '-':
                printf("%.1f - %.1f = %.1f",n1, n2, n1-n2);
                break;
    
            case '*':
                printf("%.1f * %.1f = %.1f",n1, n2, n1*n2);
                break;
    
            case '/':
                printf("%.1f / %.1f = %.1f",n1, n2, n1/n2);
                break;
    
            // operator doesn't match any case constant +, -, *, /
            default:
                printf("Error! operator is not correct");
        }
    
    */
        }
    
        //case for 3 operation choosed
    
        else if(total=='3')
        {
            printf("Veuillez saisir le premier chiffre: \n");
            scanf("%f", &n1);
            viderBuffer();
            printf("Veuillez taper le deuxieme chiffre: \n");
            scanf("%f", &n2);
            viderBuffer();
            printf("Veuillez taper le troisieme chiffre: \n");
            scanf("%f", &n3);
            viderBuffer();
        }
    
        //case for 4 operation choosed
    
        else if(total=='4')
        {
            printf("Veuillez saisir le premier chiffre: \n");
            scanf("%f", &n1);
            printf("Veuillez taper le deuxieme chiffre: \n");
            scanf("%f", &n2);
            printf("Veuillez taper le troisieme chiffre: \n");
            scanf("%f", &n3);
            printf("Veuillez taper le quatrieme chiffre: \n");
            scanf("%f", &n4);
        }
    
        //case for 5 operation choosed
    
        else if(total=='5')
        {
            printf("Veuillez saisir le premier chiffre: \n");
            scanf("%f", &n1);
            viderBuffer();
            printf("Veuillez taper le deuxieme chiffre: \n");
            scanf("%f", &n2);
            viderBuffer();
            printf("Veuillez taper le troisieme chiffre: \n");
            scanf("%f", &n3);
            viderBuffer();
            printf("Veuillez taper le quatrieme chiffre: \n");
            scanf("%f", &n4);
            viderBuffer();
            printf("Veuillez taper le cinquieme chiffre: \n");
            scanf("%f", &n5);
            viderBuffer();
        }
    
        //this time I used switch case for the operation
        //right now it's only simple operation for 2 operation
        //will add another if function for the 3,4,5 operation
    
    
        return 0;
    }
    
    
    void viderBuffer()
    {
        int c = 0;
        while (c != '\n' )  //this function is to prevent the repetition
        {
            c = getchar();
        }
    }
    Last edited by Salem; 01-28-2022 at 11:36 PM. Reason: Remove unnecessary size tags

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,138
    Code:
    if(total=='2')
    The above is wrong because total is an integer.

    You likely want

    Code:
    if (total == 2)
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    145
    This is C code. Maybe someone can move it to the right place.

  4. #4
    Registered User
    Join Date
    Jan 2022
    Posts
    5
    oh yes I'm sorry for that I'm new to the forum site so I think I posted it in the wrong place...

  5. #5
    Registered User
    Join Date
    Jan 2022
    Posts
    5
    Thank you so much. It is working so well now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-19-2020, 05:50 AM
  2. Function Prototype, Function Call, and Function definition
    By dmcarpenter in forum C Programming
    Replies: 9
    Last Post: 04-09-2013, 03:29 AM
  3. Replies: 13
    Last Post: 03-20-2012, 08:29 AM
  4. Print function: sending a function.. through a function?
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 11-05-2008, 05:03 PM
  5. Replies: 9
    Last Post: 01-02-2007, 04:22 PM

Tags for this Thread