| View previous topic :: View next topic |
| Author |
Message |
reubatsch456 Cheater
Reputation: 0
Joined: 03 Jan 2008 Posts: 30
|
Posted: Tue Jan 29, 2008 6:58 pm Post subject: [C] Rate my Source Code |
|
|
This is my first program in C. (Besides the "Hello World!" which in my opinion doesn't really count ) Its a calculator thats weird and kind of hard to use. I also wrote up some instructions that are very user friendly. So I just wanted you guys to see how well I wrote the code and if I can improve in any areas. It took me a few hours to write it and even more debugging it.
I know I need comments....I'll try adding them later.
| Code: | #include <stdio.h>
#include <stdlib.h>
void menu(void);
void instruction(void);
void calculator(void);
float addition(float, float);
float subtraction(float, float);
float multiplication(float, float);
float division(float, float);
int main(void)
{
puts("-------------------------------------------------------------------------------");
puts("This is program that performs basic operations. It is almost like any other");
puts("hand-held calculator. If this is your first time using this program, go");
puts("through the instructions to avoid future errors. If the program does not");
puts("look right, try resizing the window. Usually the default size works fine.");
puts("-------------------------------------------------------------------------------\n");
system("pause");
puts("\n-------------------------------------------------------------------------------");
puts("Select one of the options below by entering the corresponding number.");
puts("Warning! Enter only integers! If you do not you WILL encounter an error!");
puts("-------------------------------------------------------------------------------");
menu();
return 0;
}
void menu(void)
{
int menu_input = 0;
do
{
puts("\n1 - Instructions for Use");
puts("2 - Start Calculating");
puts("3 - Quit\n");
puts("\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("Your choice: ");
scanf("%d", &menu_input);
switch (menu_input)
{
case 1:
{
instruction();
break;
}
case 2:
{
calculator();
break;
}
case 3:
{
;
break;
}
default:
{
puts("-------------------------------------------------------------------------------");
puts("The value you entered is invalid.");
puts("-------------------------------------------------------------------------------");
break;
}
}
}
while (menu_input < 1 || menu_input > 3);
}
void instruction(void)
{
puts("\n((INSTRUCTIONS))");
puts("-------------------------------------------------------------------------------");
puts("Again, this is a program that performs basic operations. It is almost like any");
puts("other hand-held calculator. When using this program, do not scroll the window.");
puts("You will only mess things up. The program will adjust what you see.");
puts("-------------------------------------------------------------------------------");
puts("When you use the calculator:\n");
puts(" - Enter in three values. Be sure that spaces, as large as you want, separate");
puts(" these three values.");
puts(" - The first two values are the operands that you want to add/subtract/multiply");
puts(" divide/modulo.");
puts(" - If you do not enter anything, the default value of the operands are 0.");
puts(" - The third value is the number that corresponds to the operator that");
puts(" will be used. The third value entered can also signal quitting the program.");
puts(" - For example, you enter in 2 2 1. The result will be 2 + 2 = 4.");
puts(" - The two 2s are the first two values entered in. 1 corresponds to addition");
puts(" (you will see later).");
puts(" - If you did the preceding correctly, then you will get correct results.");
puts(" - These steps will repeat until you select quit, which terminates the program.\n");
puts("Make sure that you read through all these instructions.\n");
system("pause");
puts("\n-------------------------------------------------------------------------------");
puts("Select one of the options below by entering the corresponding number.");
puts("Warning! Enter only integers! If you do not you WILL encounter an error!");
puts("-------------------------------------------------------------------------------");
menu();
}
void calculator(void)
{
float first_operand = 0;
float second_operand = 0;
float result = 0;
int user_choice = 0;
puts("\n((CALCULATOR))");
puts("-------------------------------------------------------------------------------");
puts("Use the table shown later as a guide to selecting an operator. For example,");
puts("if you want to multiply 5 and 4, you would enter in 5 4 3. The number 3");
puts("corresponds to the multiplication operator. When 3 is the 3rd value entered,");
puts("the program is informed that you want to multiply the first two numbers.");
puts("The menu also allows you to go reread the instructions or quit the program.");
puts("-------------------------------------------------------------------------------\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
system("pause");
puts("\n");
while(user_choice != 6, user_choice != 5)
{
puts("-------------------------------------------------------------------------------");
puts("Warning! Enter only integers to select the operator! If you do not you WILL");
puts("encounter an error! Very large numbers do not work");
puts("-------------------------------------------------------------------------------\n");
puts("1 - Addition");
puts("2 - Subtraction");
puts("3 - Multiplication");
puts("4 - Division");
puts("5 - Instructions");
puts("6 - Quit\n\n\n\n\n\n\n\n\n\n\n");
printf("Enter the three values: ");
scanf("%f%f%d", &first_operand, &second_operand, &user_choice);
switch (user_choice)
{
case 1:
{
result = addition(first_operand, second_operand);
printf("\n%f + %f = %f\n", first_operand, second_operand, result);
break;
}
case 2:
{
result = subtraction(first_operand, second_operand);
printf("\n%f - %f = %f\n", first_operand, second_operand, result);
break;
}
case 3:
{
result = multiplication(first_operand, second_operand);
printf("\n%f * %f = %f\n", first_operand, second_operand, result);
break;
}
case 4:
{
result = division(first_operand, second_operand);
printf("\n%f / %f = %f\n", first_operand, second_operand, result);
break;
}
case 5:
{
instruction();
break;
}
case 6:
{
;
break;
}
default:
{
puts("Reenter the three values again to procede.");
break;
}
}
}
}
float addition(float a, float b)
{
long double c;
c = a + b;
return c;
}
float subtraction(float a, float b)
{
long double c;
c = a - b;
return c;
}
float multiplication(float a, float b)
{
long double c;
c = a * b;
return c;
}
float division(float a, float b)
{
long double c;
c = a / b;
return c;
}
|
_________________
|
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Tue Jan 29, 2008 7:34 pm Post subject: |
|
|
The source is clean but the program has an error. I compiled it and tried addition with A + 1. It went into an endless loop. Use error catch. But nice style with the tabs and stuff, it looks good. not all smooshed.
_________________
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Tue Jan 29, 2008 7:53 pm Post subject: |
|
|
{
}'s on cases? ughh
Whats with the long double c;? You're allowing floats to be used but then just decide to do that?
you don't need to declare the function prototypes if you put them above. Personally I'd just throw them into another file to keep it all neat but that's just me.
|
|
| Back to top |
|
 |
reubatsch456 Cheater
Reputation: 0
Joined: 03 Jan 2008 Posts: 30
|
Posted: Tue Jan 29, 2008 8:38 pm Post subject: |
|
|
| Quote: | | The source is clean but the program has an error. I compiled it and tried addition with A + 1. It went into an endless loop. Use error catch. But nice style with the tabs and stuff, it looks good. not all smooshed. |
Yeah I'm not sure how to print an error message when the user enters in a char in the menu or other places where they enter stuff. What error catch (yeah im nooby)?
| Quote: | {
}'s on cases? ughh
Whats with the long double c;? You're allowing floats to be used but then just decide to do that? Confused
you don't need to declare the function prototypes if you put them above. Personally I'd just throw them into another file to keep it all neat but that's just me. |
Whoops I thought that you need braces on switch if you have a compound statement, like in the if statement. Oh and I was really going to use long doubles so the user can enter very large values but it kept displaying them in scientific notation. So i switched everything to float but probably forgot the c's.
Thanks for the feedback...
_________________
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Tue Jan 29, 2008 9:10 pm Post subject: |
|
|
| floats will end up in scientific notation if they get too large, they are probably your best bet.
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
|
| Back to top |
|
 |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Wed Jan 30, 2008 9:15 am Post subject: |
|
|
| why do you sometimes use puts instead of printf ?
|
|
| Back to top |
|
 |
the_undead Expert Cheater
Reputation: 1
Joined: 12 Nov 2006 Posts: 235 Location: Johannesburg, South Africa
|
Posted: Wed Jan 30, 2008 9:23 am Post subject: |
|
|
print if formatted print, no point in using it if you dont need to format anything.
puts is faster.
/as far as I know at least
_________________
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Wed Jan 30, 2008 9:37 am Post subject: |
|
|
puts only sends a constant string to the console. It doesn't do formatting of the string.
printf allows you to have a non-constant string that is formatted based on an argument list that after being formatted is then put into the console.
_________________
- Retired. |
|
| Back to top |
|
 |
--Pillboi-- Grandmaster Cheater Supreme
Reputation: 0
Joined: 06 Mar 2007 Posts: 1383 Location: I don't understand the question. Is this a 1 to 10 thing?
|
Posted: Wed Jan 30, 2008 12:28 pm Post subject: |
|
|
http://www.gidnetwork.com/b-56.html
_________________
Enter darkness, leave the light, Here be nightmare, here be fright...
Earth and Water, Fire and Air. Prepare to meet a creature rare.
Enter now if you dare, Enter now the dragon's lair. |
|
| Back to top |
|
 |
tornarrow Master Cheater
Reputation: 0
Joined: 29 Jan 2008 Posts: 289
|
Posted: Wed Jan 30, 2008 12:59 pm Post subject: |
|
|
| 8/10 :O
|
|
| Back to top |
|
 |
reubatsch456 Cheater
Reputation: 0
Joined: 03 Jan 2008 Posts: 30
|
Posted: Wed Jan 30, 2008 5:57 pm Post subject: |
|
|
Ok thanks to everyone. I'll try using the different methods you posted. Btw, theres many bugs on the source posted above. So if you compile the code, you might run into the errors.
These questions are gonna sound noobish:
-is there a difference between getch() and _getch()?
-what exactly is an overhead?
_________________
|
|
| Back to top |
|
 |
Lorrenzo Moderator
Reputation: 4
Joined: 02 Jun 2006 Posts: 3744
|
Posted: Wed Jan 30, 2008 6:23 pm Post subject: |
|
|
I have no idea on what it means, but it is dang organized. I give it a 11/10 for organization.
_________________
LAWLrrenzolicious |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Thu Jan 31, 2008 5:39 pm Post subject: |
|
|
Not much of a difference between getch() and _getch(), they are the same function, just that getch() is the old original one. It was depreciated when VS2005 was released, and _getch() was introduced.
_________________
- Retired. |
|
| Back to top |
|
 |
|