Click to See Complete Forum and Search --> : Check out my code, please.


twofoolish2b
06-15-2001, 11:30 AM
What's going on guys? Well I think I'm starting to learn C. I have played with it a little before, but not much. Anyway here is "My Calculator Program", tell me what you think.

#include <stdio.h>
main()
{
int a = 1, b = 2, c = 3, d = 4, e[8];
int answer, total, exit = 5;
printf("\n");
printf("\t\t\t# My Calculator #\n");
printf("1. Add.\n");
printf("2. Sub.\n");
printf("3. Mul.\n");
printf("4. Div.\n");
printf("5. Exit.\n");
printf("\n");
printf("What would you like to do? ");
scanf("%d", &answer);
if(answer == a)
{
printf("First Number: ");
scanf("%d", &e[0]);
printf("Second Number: ");
scanf("%d", &e[1]);
total = e[0] + e[1];
printf("Total: %d + %d = %d\n", e[0], e[1], total);
}
else if(answer == b)
{
printf("First Number: ");
scanf("%d", &e[2]);
printf("Second Number: ");
scanf("%d", &e[3]);
total = e[2] - e[3];
printf("Total: %d - %d = %d\n", e[2], e[3], total);
}
else if(answer == c)
{
printf("First Number: ");
scanf("%d", &e[4]);
printf("Second Number: ");
scanf("%d", &e[5]);
total = e[4] * e[5];
printf("Total: %d * %d = %d\n", e[4], e[5], total);
}
else if(answer ==d)
{
printf("First Number: ");
scanf("%d", &e[6]);
printf("Second Number: ");
scanf("%d", &e[7]);
total = e[6] / e[7];
printf("Total: %d / %d = %d\n", e[6], e[7], total);
}
else
printf("Goodbye!\n");
return 0;
}



Is there a way I could have done this better? Remember I'm just a newbie when it comes to C. Thanks...

[ 15 June 2001: Message edited by: twofoolish2b ]

Stuka
06-15-2001, 11:52 AM
Coupla hints:
1) Do all of your prompts in one place (a function perhaps) rather than writing them four times.
2) Use a switch statement in place of the if...else structure - it's perfect for this type of situation.
3) Maybe put the functional part (menu, input, output) inside a loop so that it can be used multiple times before quitting.

Question:
Why are you using an array for the input numbers? There are always two, so an 8 element array isn't really necessary.

binaryDigit
06-16-2001, 06:00 PM
why did you declare variables to compare the user input for the menu?
like Strike said a switch statement would have been better.



int
main(void) {

int menu_choice = 0, answer = 0;
/* first two numbers in the array would be user input. the third would be the final
answer */
int numbers[] = { 0, 0, 0 };

/* print menu choices */
/* get menu choice */
/* switch menu choice */
/* each operation would have it's own function. you can pass the array to each function */
switch(menu_choice) {
case 1: add_function(numbers); break;
case 2: subtract_function(numbers); break;
/* and so on */
default: /* print the 'not an option' statement */
}

/* print the answer */
return 0;
}

void
add_function(int number_array[]) {

/* get the numbers from the users */
number_array[2] = number_array[0] + number_array[1];

}



that's kind of how i would have done it

[ 16 June 2001: Message edited by: binaryDigit ]

sans-hubris
06-17-2001, 03:00 AM
You're obviously a novice in C and perhaps programming in general. First off, I would change the variable names you have for the different operations into what they actually are (i.e. add, subtract, etc. instead of a, b, c) so that when I refer to them in my could, the could would describe it self (answer==add is a lot more descriptive than answer==a or even answer==1.) Second, I would use a switch-case setup (look it up, they're very useful.) Thirdly, I would probably use chars instead of ints to scan for the first letter of each word. That's much more intuitive. I would also figure out whats common to all operations and keep those out of the switch-case setup (or the if-else statements if you use those.) I would only keep the things that are different inside the switch-case setup (or if-else statements.) It would also be a good idea to put those things in a loop so the user can do as many of those operations as he/she pleases.

PS I would also prolly use global constants to make the code in general a little easier to read (look up macros, global variables, and const.)

[ 17 June 2001: Message edited by: ndogg.cpp ]

[ 17 June 2001: Message edited by: ndogg.cpp ]