#Code Review

15 messages · Page 1 of 1 (latest)

proud zenith
#

I've been learning C for 4 hours. Could anyone review my code? It does work, I'm just asking to find any issues or get feedback on how it's written. I know I should have used switch statements, but I don't know how to use them yet.

strong geode
#

you could deduplicate a lot of the code

#
int x, y;

clearScreen();
printf("You are doing Addition...\n\n"); // <---
printf("Please enter first number: ");

scanf("%d", &x);

clearScreen();
printf("You are doing Addition...\n\n"); // <---
printf("Please enter second number: ");

scanf("%d", &y);

clearScreen();
// C expects format strings even if its calling a function, so i should always include "%d" for a integer.
printf("The answer of %d + %d is %d", x, y, addition(x, y)); // <---
        
while (getchar() != '\n');
        
printf("\nPress Enter to exit...");
getchar();
#

only 3 lines differ between all the options

obtuse ravine
#

hmm, I don't think there is switch statements in C

proud zenith
#
obtuse ravine
#

oh ok

strong meteor
#

this is a perfect example for function pointers id say. you have multiple copies of basically the same thing. and whenever such a thing exists in your program you should make a function that can do all cases. which should be possible here

strong meteor
#

for the subtraction addition multiplication division

wicked falcon
#

there is here a way to use em ```c
scanf("%d", &Task);

int num1,num2;

    switch (Task)
{
case 1:
   printf("Number 1 :");
   scanf("%d", &num1);
   printf("Number 2 :");
   scanf("%d", &num2);
   printf("%f", addition(num1,num2));
    break;```