Arithmetic Operation

SOLVED!



This is what I've made so far
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>

int main()
{
    int a, b, c, d, e, f;
    int 
    float Addition, Subtraction, Multiplication, Division, Modulo, exit;
    while (n < 1 || n > 10);
    printf("Please enter which arithmetic operation you would like to use:\n");
    printf("Input a for Addition\n");
    printf("Input b for Subtraction\n");
    printf("Input c for Multiplication\n");
    printf("Input d for Division\n");
    printf("Input e for Modulo\n");
    printf("Input f for exit\n");
        day = getchar();
    switch(day){
    case 'a':
        printf("You have chosen addition!:\n");
        printf("How many numbers would you like to add:\n");
        printf("1st:\n")
        scanf("%f", &n);
        addition = (1st + n)
        scanf("%f", &n);
        printf("current sum:\n");
        scanf("%f")
Last edited on
switch has a default:
1
2
3
4
5
6
7
8
switch(day)
{
  case 'a': whatever;
  break; //remember to break!
  ...
   default: cout << "enter a valid choice or something";
}


and for the main question,
1
2
3
4
5
6
7
8
do
{
 printf("You have chosen addition!:\n");
        printf("How many numbers would you like to add:\n");
        printf("1st:\n")
        scanf("%f", &n);
} while(n > 10 || n < 2); //whatever condition
Last edited on
Oh I see, Thank you so much! And how about the "Current sum" thing? also the "whatever" doesn't work on onlinegdb. com
Last edited on
As C code and for the addition operation, consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdio.h>
#include <ctype.h>

int getInt(const char* prm)
{
	int n = 0;

	while (printf(prm) && scanf("%i", &n) != 1) {
		puts("Invalid number");
		while (getchar() != '\n');
	}

	return n;
}

float getFloat(const char* prm)
{
	float n = 0.0;

	while (printf(prm) && scanf("%f", &n) != 1) {
		puts("Invalid number");
		while (getchar() != '\n');
	}

	return n;
}

int main()
{
	const char* const numbs[] = {"zero", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th"};
	bool cont = true;

	while (cont) {
		printf("\nPlease enter which arithmetic operation you would like to use:\n\n");
		printf("Input a for Addition\n");
		printf("Input b for Subtraction\n");
		printf("Input c for Multiplication\n");
		printf("Input d for Division\n");
		printf("Input e for Modulo\n");
		printf("Input f for exit\n");
		printf("\nEnter option: ");

		int opt = 0;

		while (isspace(opt = getchar()));

		switch (opt) {
			case 'a':
			{
				printf("You have chosen addition!:\n");

				int n = 0;

				do {
					n = getInt("How many numbers would you like to add (max 10): ");
				} while ((n < 1 || n > 10) && printf("Invalid number\n"));

				float result = 0.0;

				for (int i = 1; i <= n; ++i) {
					char prm[10] {};

					snprintf(prm, sizeof(prm), "%s: ", numbs[i]);
					result += getFloat(prm);
					printf("Current sum= %g\n", result);
				}
			}
				break;

			case 'f':
				cont = false;
				break;

			default:
				puts("Unknown operation");
				break;
		}
	}
}




Please enter which arithmetic operation you would like to use:

Input a for Addition
Input b for Subtraction
Input c for Multiplication
Input d for Division
Input e for Modulo
Input f for exit

Enter option: a
You have chosen addition!:
How many numbers would you like to add (max 10): q
Invalid number
How many numbers would you like to add (max 10): 3
1st: z
Invalid number
1st: 1
Current sum= 1
2nd: 2
Current sum= 3
3rd: 3
Current sum= 6

Please enter which arithmetic operation you would like to use:

Input a for Addition
Input b for Subtraction
Input c for Multiplication
Input d for Division
Input e for Modulo
Input f for exit

Enter option: f

Last edited on
min01 wrote:
also the "whatever" doesn't work on onlinegdb. com

It's pseudocode, not actual code. Of course it won't compile.

In case you don't know what pseudocode is, it's basically like notes that programmers use to work out the logic of a program. Then they can write actual code based upon the pseudocode.

@seeplus,
Just a note– although you proably already know this– <stdio.h> and <ctype.h> are for C programs, not C++. The C++ versions are <cstdio> and <cctype>.

Although that was more for the benefit of the OP, because you likely already knew that.
That's why I said at the beginning of that post that it was C code :) :)
Oops! Sorry, I guess I completely missed that...somehow!
THANK YOU SO MUCH FOR YOUR HELP GUYS!
Topic archived. No new replies allowed.