Home > Net >  Am i doing a good job? I seem to have a lot of errors
Am i doing a good job? I seem to have a lot of errors

Time:09-30

int main() {
double num1, num2;
char f1;
printf("What to Count:");
scanf_s("%f", &num1);
scanf_s("%c", &f1);
scanf_s("%f=", &num2);

if (f1 = " ") {
    printf("%lf\n", num1   num2);
}
else if (f1 = "-") {
    printf("%lf\n", num2 - num2);
}
else if (f1 = "*") {
    printf("%lf\n", num1 * num2);
}
else if (f1 = "/") {
    printf("%.2f\n", num1 / num2);
}
else if (f1 = "%") {
    printf("%lf\n", num1 % num2);
}
else if (f1 = "#") {
    printf("%lf\n", num1 ^ num2);
}
else {
    printf("Invalid!\n");
}

system("pause");
return 0;

}

mvs said "a value of type "const char* " cannot be assigned to an entity of type "char" "expression must have integral or unscoped enum type" etc.

CodePudding user response:

You should get way more compiler warnings:

  • scanf_s("%f", &num1); here you have a parameter type mismatch. For double you need %lf.
    Fun fact: For printf("%lf\n", num1 num2); you don't need the extra l as float parameters are passed as double anyway.

  • Same for scanf_s("%f=", &num2);. In this call you also require the user to enter = after the number.

  • scanf_s("%c", &f1); The scanf_s function requires an extra argument for %c format specifier: a value of type rsize_t indicating the size of the receiving array

  • if (f1 = " ") This is an assignment instead of a comparison. Use == instead.

  • That is also wrong type. " " is a string while you only want to compare a character. That would be ' '.

  • As Jabberwocky pointed out, you are using all kind of functions without including the required headers. Don't do this.

  • printf("%lf\n", num1 ^ num2); What is this expression supposed to do? Bitwise XOR operator (^) is not allowed to be used with double values.

  • Same with printf("%lf\n", num1 % num2); The module operator mustn't be used with double values.

Additionally, you should always check return value of scanf and related function. Otherwise how would you know about errors?

From the operations that are not valid for double I assume you are supposed to use int instead.

As you do not use the lengh field of scanf_s you couls also use the standard functions instead. A fixed version of your code could look like this:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>


int main(void) {
    int num1, num2;
    char f1;
    printf("What to Count:");
    scanf("%d", &num1);
    scanf("%c", &f1);
    scanf("%d=", &num2);

    int result = 0; 
    bool invalid = false;

    if (f1 == ' ') {
        result = num1   num2;
    }
    else if (f1 == '-') {
        result = num1 - num2;
    }
    else if (f1 == '*') {
        result = num1 * num2;
    }
    else if (f1 == '/') {
        result = num1 / num2;
    }
    else if (f1 == '%') {
        result = num1 % num2;
    }
    else if (f1 == '#') {
        result = num1 ^ num2;
    }
    else {
        invalid = true;
    }
 
    if (invalid)
    {
      printf("Invalid!\n");
    }
    else
    {
      printf("%d\n", result);
    }

    system("pause");
    return 0;
}

I moved the printing into one place. That would make it easier to change type of calculations.

Output (in WSL):

What to Count:3 5=
8
sh: 1: pause: not found

CodePudding user response:

Confusing assignment and equality operator:

Single '=' is used for assigning a value. For example char a = 5;

This assigns a value of 5 to the character variable a.

Double '==' is used for comparison, or checking equality. For example in this case, you have used single '=' in the conditions, which as you now know is an assignment operator. Replace it with a '=='.

Not including the header files:

The functions like printf and scanf are predefined in the header file <stdio.h>. Without including this, your compiler wouldn't recognize what print and scanf means.

Not checking input validity:

When prompting the user for input, always check the return value of scanf. Consider your users stupid, or even hostile who wants to find a way to crash your program. What if I were to enter 'asdf' when you asked for a character and vice versa? Scanf is a powerful function, but it was meant for parsing, not getting input as such.

Confusing '' and " ":

' ' is for a char variable, whereas " " is for a string variable. You've declared f1 as a character variable after main, not a string variable.

Indentation:

The curly braces are not in their right places. And you may as well omit them when the conditions only contain a single line of code.

Format specifier for double variable:

The format specifier for a double variable in the scanf statement is %lf, whereas a %f is used while printing.

You've also included a '=' operator in the third scanf statement. That could also render the program to work improperly.

CodePudding user response:

int main(void)
{
    double num1, num2;
    char f1;
    
    printf("What to Count:");
    scanf_s("%lf", &num1);
    scanf_s("%c", &f1);
    scanf_s("%lf", &num2);

    double resultdivide = num1 / num2;
    double resultpow = pow(num1, num2);
    double resultplus = num1   num2;
    double resultminus = num1 - num2;
    double resultmult = num1 * num2;

    if (f1 == ' ') {
        printf("%.0f\n",resultplus);
    }
    else if (f1 == '-') {
        printf(".0f\n", resultminus);
    }
    else if (f1 == '*') {
        printf("%.0f\n", resultmult);
    }
    else if (f1 == '/') {
        printf("%.2f\n", resultdivide);
    }
    else if (f1 == '#') {
        printf("%.0f\n", resultpow);
    }
    else {
        printf("Invalid!\n");
    }
    
    system("pause");
    return 0;
}

this is what i've come up with after brainstorming looks messy but it actually works so im fine with it but still can't figure out how to add the % function D:

  •  Tags:  
  • c
  • Related