Mohit Singh
Mohit Singh

Reputation: 153

My code works for C++ but not when converted to C language

I am providing the code that I converted to the C language and the converted code as well. The code works just as desired in C++ but I am unable to enter the value of the 'Operator' variable in the C language version. This is the C++ code:

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
    float num;
    float num2;
    char Operator;
    char choice;
    float result;

    cout << "Select one of the following items:\n";
    cout << "B) - Binary Mathematical Operations, such as addition and subtraction.\n";
    cout << "U) - Unary Mathematical Operations, such as square root, and log.\n";
    cout << "A) - Advances Mathematical Operations, using variables, arrays.\n";
    cout << "V) – Define variables and assign them values.\n";
    cout << "E) - Exit\n";
    cin >> choice;

    if (choice == 'U' || choice == 'u') {
        cout << "Please enter the operation ( S (for square root) , L (for logarithm) , E (for exponential) , C (for ceil) , F (for floor) ):\n";
        cin >> Operator;
    rerun:
        cout << "Enter a positive value.\n";
        cin >> num;
        if (num < 0) {
            goto rerun;
        }
        else {
            switch (Operator) {
            case 'S':
                result = sqrt(num);
                break;
            case 'L':
                result = log(num);
                break;
            case 'E':
                result = exp(num);
                break;
            case 'C':
                result = ceil(num);
                break;
            case 'F':
                result = floor(num);
                break;
            default:
                result = 0;
                break;
            }
        }
        cout << fixed << setprecision(2) << result;
    }
    return 0;
}

This is the C language code:

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

int main()
{
    double num;
    char Operator;
    char choice;
    double result;

    printf("Select one of the following items:\n");
    printf("B) - Binary Mathematical Operations, such as addition and subtraction.\n");
    printf("U) - Unary Mathematical Operations, such as square root, and log.\n");
    printf("A) - Advances Mathematical Operations, using variables, arrays.\n");
    printf("V) - Define variables and assign them values.\n");
    printf("E) - Exit\n");
    scanf("%c", &choice);
    if (choice == 'U' || choice == 'u') {
        printf("Please enter the operation ( S (for square root) , L (for logarithm) , E (for exponential) , C (for ceil) , F (for floor) ):\n");
        scanf("%c", &Operator);
        printf("Enter a positive value.\n");
        scanf("%lf", &num);
        switch (Operator) {
        case 'S':
            result = sqrt(num);
            break;
        case 'L':
            result = log(num);
            break;
        case 'E':
            result = exp(num);
            break;
        case 'C':
            result = ceil(num);
            break;
        case 'F':
            result = floor(num);
            break;
        default:
            result = 0;
            break;
        }

        printf("the answer is %lf", result);
    }
    return 0;
}

Both the codes are working except I am unable to input the value of the 'Operator' variable in the C version whereas it works great in C++. Any help regarding this is truly appreciated.

Upvotes: 1

Views: 98

Answers (2)

Ravi Prakash
Ravi Prakash

Reputation: 313

This code will work for you!. Actually you should have better understanding of how buffered input works.

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

int main()
{
    double num;
    char Operator;
    char choice;
    double result;

    printf("Select one of the following items:\n");
    printf("B) - Binary Mathematical Operations, such as addition and subtraction.\n");
    printf("U) - Unary Mathematical Operations, such as square root, and log.\n");
    printf("A) - Advances Mathematical Operations, using variables, arrays.\n");
    printf("V) - Define variables and assign them values.\n");
    printf("E) - Exit\n");
    scanf("%c", &choice);

    char eat_line;
    scanf("%c", &eat_line);
    if (choice == 'U' || choice == 'u') {
        printf("Please enter the operation ( S (for square root) , L (for logarithm) , E (for exponential) , C (for ceil) , F (for floor) ):\n");
        scanf("%c", &Operator);
        scanf("%c", &eat_line);
        printf("Enter a positive value.\n");
        scanf("%lf", &num);
        switch (Operator) {
        case 'S':
            result = sqrt(num);
            break;
        case 'L':
            result = log(num);
            break;
        case 'E':
            result = exp(num);
            break;
        case 'C':
            result = ceil(num);
            break;
        case 'F':
            result = floor(num);
            break;
        default:
            result = 0;
            break;
        }

        printf("the answer is %lf", result);
    }
    return 0;
}

Upvotes: 0

john
john

Reputation: 87952

Unlike the equivalent C++ code inputing a character using %c does not skip whitespace. To do that you should add a space before the %c, like this

scanf(" %c",&Operator); // skip whitespace then read a char

Upvotes: 5

Related Questions