``````#include <stdio.h>
/*pre-defined processor statements*/
#ifndef __OPERATION__
#define __OPERATION__
#define TOTAL 0
#define SUBTRACTION  2
#define MULTIPLICATION 3
#define	DIVISION 4
#define MODULAR 5
#endif

int main() {
/*assign the variables*/
int num1 = 0;
int num2 = 0;

char operation;
char operation2;
int total = 0;

/*modular is q while m is multiplication*/
printf(" Programming Assignment #2 - by Geoffrey \n");
printf("\n");

/* asking user for one input*/
scanf("%d", &num1);
fflush(stdin);

/* checks if the first user input is within range( bounds checking)*/
while (num1 < -1000 || num1 > 1000) {
printf("out of bounds\n");
scanf("%d", &num1);
fflush(stdin);

}

/* ask user for second input*/
scanf("%d", &num2);
fflush(stdin);

/* checks if the second user input is within range (bounds checking)*/
while (num2 < -1000 || num2 > 1000) {
printf("out of bounds\n");
scanf("%d", &num2);
fflush(stdin);
}

/* tells user for the next step as prompted*/

printf("Please select an operation to put in the blank space\n");
printf("%d__%d= \n", num1, num2);
printf("\n\n");
printf("type the associated letter and press enter:\n");
printf(" A - Addition\n S - Subtraction\n M - Multiplication\n D - Division\n Q - Modular\n");

/* use scanf to receive the letter for the operation*/
scanf("%d", operation2);

/* uss a switch case structure to use the user input calculator math */
switch (operation2)
{
case ADDITION : total = num1 + num2;
operation = '+';
break;

case SUBTRACTION: total = num1 - num2;
operation = '-';
break;

case MULTIPLICATION: total = num1 * num2;
operation = '*';
break;

case DIVISION: total = num1 / num2;
operation = '/';
break;

case MODULAR:
total = num1 % num2;
operation = '%';
break;

default: printf("Invalid Letter selected\n");

}

return(0);
}``````

## All 16 Replies

I know my problem is within the SWITCH CASE!!

I know my problem is within the SWITCH CASE!!

You might get a faster reply if you told us what's wrong..

Err 1:
Line 67 :

``scanf("%d", operation2);``

It should be

``scanf("%c", &operation2);``

Err 2:
Your macros are completely useless in this case.Even though you can change them to correct the mistake I would suggest an alternate way.

``````scanf("%c", operation2);

/* uss a switch case structure to use the user input calculator math */
switch (operation2)
{
case 'a':
case  'A': total = num1 + num2;
operation = '+';
break;

case 's':
case 'S': total = num1 - num2;
operation = '-';
break;

case 'm':
case 'M': total = num1 * num2;
operation = '*';
break;

case 'd':
case 'D': total = num1 / num2;
operation = '/';
break;

case 'q':
case 'Q':
total = num1 % num2;
operation = '%';
break;

default: printf("Invalid Letter selected\n");
}``````

Err 3:
What you are doing is mixing the concepts.Don't do it. Try to clarify your doubts and have a clear conceptual of MACROS and where they are used and where do we need to sue variables.And ya also try to have a look on why fflush(stdin) is bad and should be avoided.
You'll always find help in this community as long as you show some hard work from your side.

What to Help...?...i need a clear Question

Look in Preprocessor Statements..you defined wrong values like 0..1..2..

Buy you are taking input as 'A', 'S','M'....So you should define constants with ASCII values of A, S, M, D...

I want to print the operation of num1 and num2 using eithor addition, subtraction, multiplication, division, or modular, in my case, i need to use the case switch .

Plus scanf("%c", operation2);

should be

scanf("%c", &operation2);

I know my problem is within the SWITCH CASE!!

Plenty of thing are erroneous in your snippet, however I would like to point out the cause of your affliction for now.

``````printf(" A - Addition\n S - Subtraction\n M - Multiplication\n D - Division\n Q - Modular\n");

/* use scanf to receive the letter for the operation*/
scanf("%d", &operation2);``````

If you expect a letter, how come you are asking scanf() to parse an integer?
A why a letter, when you define ADDITION, SUBTRACTION, etc... as integer constants?

Here YOU go....Analyse th eProgram

``````#include <stdio.h>

/*pre-defined processor statements*/

#ifndef __OPERATION__

#define __OPERATION__

#define TOTAL 0

#define SUBTRACTION  83

#define MULTIPLICATION 77

#define DIVISION 68

#define MODULAR 81

#endif

int main() {

/*assign the variables*/

int num1 = 0;

int num2 = 0;

char operation;

char operation2;

int total = 0;

/*modular is q while m is multiplication*/

printf(" Programming Assignment #2 - by Geoffrey \n");

printf("\n");

/* asking user for one input*/

scanf("%d", &num1);

fflush(stdin);

/* checks if the first user input is within range( bounds checking)*/

while (num1 < -1000 || num1 > 1000) {

printf("out of bounds\n");

scanf("%d", &num1);

fflush(stdin);

}

/* ask user for second input*/

scanf("%d", &num2);

fflush(stdin);

/* checks if the second user input is within range (bounds checking)*/

while (num2 < -1000 || num2 > 1000) {

printf("out of bounds\n");

scanf("%d", &num2);

fflush(stdin);

}

/* tells user for the next step as prompted*/

printf("Please select an operation to put in the blank space\n");

printf("%d__%d= \n", num1, num2);

printf("\n\n");

printf("type the associated letter and press enter:\n");

printf(" A - Addition\n S - Subtraction\n M - Multiplication\n D - Division\n Q - Modular\n");

/* use scanf to receive the letter for the operation*/

scanf("%d", operation2);

/* uss a switch case structure to use the user input calculator math */

switch (operation2)

{

case ADDITION : total = num1 + num2;

operation = '+';

break;

case SUBTRACTION: total = num1 - num2;

operation = '-';

break;

case MULTIPLICATION: total = num1 * num2;

operation = '*';

break;

case DIVISION: total = num1 / num2;

operation = '/';

break;

case MODULAR:

total = num1 % num2;

operation = '%';

break;

default: printf("Invalid Letter selected\n");

}

return(0);

}

#include <stdio.h>

/*pre-defined processor statements*/

#ifndef __OPERATION__

#define __OPERATION__

#define TOTAL 0

#define SUBTRACTION  2

#define MULTIPLICATION 3

#define DIVISION 4

#define MODULAR 5

#endif

int main() {

/*assign the variables*/

int num1 = 0;

int num2 = 0;

char operation;

char operation2;

int total = 0;

/*modular is q while m is multiplication*/

printf(" Programming Assignment #2 - by Geoffrey \n");

printf("\n");

/* asking user for one input*/

scanf("%d", &num1);

fflush(stdin);

/* checks if the first user input is within range( bounds checking)*/

while (num1 < -1000 || num1 > 1000) {

printf("out of bounds\n");

scanf("%d", &num1);

fflush(stdin);

}

/* ask user for second input*/

scanf("%d", &num2);

fflush(stdin);

/* checks if the second user input is within range (bounds checking)*/

while (num2 < -1000 || num2 > 1000) {

printf("out of bounds\n");

scanf("%d", &num2);

fflush(stdin);

}

/* tells user for the next step as prompted*/

printf("Please select an operation to put in the blank space\n");

printf("%d__%d= \n", num1, num2);

printf("\n\n");

printf("type the associated letter and press enter:\n");

printf(" A - Addition\n S - Subtraction\n M - Multiplication\n D - Division\n Q - Modular\n");

/* use scanf to receive the letter for the operation*/

scanf("%d", operation2);

/* uss a switch case structure to use the user input calculator math */

switch (operation2)

{

case ADDITION : total = num1 + num2;

operation = '+';

break;

case SUBTRACTION: total = num1 - num2;

operation = '-';

break;

case MULTIPLICATION: total = num1 * num2;

operation = '*';

break;

case DIVISION: total = num1 / num2;

operation = '/';

break;

case MODULAR:

total = num1 % num2;

operation = '%';

break;

default: printf("Invalid Letter selected\n");

}

printf("RESULT:: %d %c %d = %d", &num1, &operation, &num2, &total);

return(0);

}
``````

I didn't really Understand...Why everyone is missing the Importance of #define statements in begining of the program, and using THE same constants in Switch case....Did you get the INTENTION of that program....So in this case...you should scan for an integer value to put into operation2...SO IT SHOULD BE %d

Syntax Error in my program..scanf("%d", operation2);
should be scanf("%d", &operation2);

Know the Intention of using #Define Constants in Switch Statements...So you should accept A as its ASCII Value.... it should be %d...Syntax Error in my Program...scanf("%d", operation2); should be scanf("%d", &operation2);

Plenty of thing are erroneous in your snippet, however I would like to point out the cause of your affliction for now.

``````printf(" A - Addition\n S - Subtraction\n M - Multiplication\n D - Division\n Q - Modular\n");

/* use scanf to receive the letter for the operation*/
scanf("%d", &operation2);``````

If you expect a letter, how come you are asking scanf() to parse an integer?
A why a letter, when you define ADDITION, SUBTRACTION, etc... as integer constants?

i think someone else pointed that out, i think im gonna use this..

scanf("%c", &operation2);
fflush(stdin);

also fflush(stdin) is the only way my teacher taught us how to make user inputs and the enter stroke key work together.

OKAY IT WORKS! heres the code that works THANKS TO ALL!

``````#include <stdio.h>
/*pre-defined processor statements*/
#ifndef __OPERATION__
#define __OPERATION__
#define TOTAL 0
#define SUBTRACTION  83
#define MULTIPLICATION 77
#define	DIVISION 68
#define MODULAR 81
#endif

int main() {
/*assign the variables*/
int num1 = 0;
int num2 = 0;

char operation = 0 ;
char operation2;
int total = 0;

/*modular is q while m is multiplication*/
printf(" Programming Assignment #2 - by Geoffrey \n");
printf("\n");

/* asking user for one input*/
scanf("%d", &num1);
fflush(stdin);

/* checks if the first user input is within range( bounds checking)*/
while (num1 < -1000 || num1 > 1000) {
printf("out of bounds\n");
scanf("%d", &num1);
fflush(stdin);

}

/* ask user for second input*/
scanf("%d", &num2);
fflush(stdin);

/* checks if the second user input is within range (bounds checking)*/
while (num2 < -1000 || num2 > 1000) {
printf("out of bounds\n");
scanf("%d", &num2);
fflush(stdin);
}

/* tells user for the next step as prompted*/

printf("Please select an operation to put in the blank space\n");
printf("%d__%d= \n", num1, num2);
printf("\n\n");
printf("type the associated letter and press enter:\n");
printf(" A - Addition\n S - Subtraction\n M - Multiplication\n D - Division\n Q - Modular\n");

/* use scanf to receive the letter for the operation*/
scanf("%c", &operation2);
fflush(stdin);

/* uss a switch case structure to use the user input with the math operations */

switch (operation2) {
case 'a':
case 'A': total = num1 + num2;
operation = '+';

break;

case 's':
case 'S': total = num1 + num2;
operation = '-';

break;

case 'm':
case 'M': total = num1 + num2;
operation = '*';

break;

case 'd':
case 'D': total = num1 + num2;
operation = '/';

break;

case 'q':
case 'Q': total = num1 + num2;
operation = '%';

break;

default: printf("Invalid Letter selected\n");

}

printf("RESULT: %d %c %d = %d\n", num1, operation, num2, total);

return(0);
}``````