![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 198
Rep Power: 1
![]() |
Beginner "C" Calculator Problem
Hi All
Here is the source code... [code=c] /*C - Calculator*/
#include <stdio.h>
#include <stdlib.h>
#define LAPSE 9999999 /*My delay time for lapse().*/
float nr_1,nr_2,added,multi,divi,subtr;
int my_menu(void);
void lapse(void);
main()
{
puts("******************************");
puts("******>>C - Calculator<<******");
puts("******************************");
printf("\n\n");
printf("Enter any two numbers:\n");
printf("1st Number...\n");
scanf("%f", &nr_1);
printf("2nd Number...\n");
scanf("%f", &nr_2);
while(1)
{
puts("Choose Calculation Method:");
switch(my_menu())
{
case 1:
{
added = ((nr_1)+(nr_2));
printf("\n%.3f + %.3f = %.1f\n", nr_1,nr_2,added);
break;
lapse();
}
case 2:
{
if(nr_1 > nr_2)
{
subtr = ((nr_1)-(nr_2));
printf("\n%.3f - %.3f = %.1f\n", nr_1,nr_2,subtr);
}
else
subtr = ((nr_2)-(nr_1));
printf("\n%.3f - %.3f = %.1f\n", nr_2,nr_1,subtr);
break;
lapse();
}
case 3:
{
multi = ((nr_1)*(nr_2));
printf("\n%.3f X %.3f = %.1f\n", nr_1,nr_2,multi);
break;
lapse();
}
case 4:
{
if(nr_1 > nr_2)
{
divi = ((nr_1)/(nr_2));
printf("\n%.3f / %.3f = %.1f\n", nr_1,nr_2,divi);
}
else
divi = ((nr_2)/(nr_1));
printf("\n%.3f / %.3f = %.1f\n", nr_2,nr_1,divi);
break;
lapse();
}
case 5:
{
printf("\nReturning Main()...\n\a");
return (main());
}
case 6:
{
printf("\nExiting now...\n\a");
exit(0);
}
default:
{
printf("\nEnter Correct Choice!\n");
}
} /*Close Switch*/
} /*Close While*/
} /*Close Main()*/
int my_menu(void)
{
int choice;
puts("1 - Addition:");
puts("2 - Subtraction:");
puts("3 - Multiplication:");
puts("4 - Division:");
puts("5 - Re- Run...");
puts("6 - Quit...");
scanf("%d", &choice);
return choice;
}
void lapse(void)
{
int x;
for (x = 0; x < LAPSE;)
;
}My problem is that I would like to know if there is a shorter way to exit the program from my_menu(). If the program gets re-run, it asks for two values. Pressing the '6' to exit doesn't work then. I am learning through SAMS teach yourself C and also using The C-Book for reference. The question was basically to write a program that works like a calculator at end of chapter 13,Exercise 8. http://lib.daemon.am/Books/C/ch13/ch13.htm They don't give the answer for this exercise, so I don't know if this is correct? All advise and comments will be considered. Thanks BstrucT(South-Africa) |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4
![]() |
Re: Beginner "C" Calculator Problem
I have one suggestion. When you divide you check which number is larger and divide it by the smaller one. However you didn't take account of the fact that the numbers entered could be 0. Try feeding it a 0 to see what happens.
|
|
|
|
|
|
#3 | |
|
Programmer
Join Date: Oct 2007
Posts: 39
Rep Power: 0
![]() |
Re: Beginner "C" Calculator Problem
Quote:
You probably shouldn't be calling exit() in the function. Just return and let main() handle exiting.You should also consider better code formatting. Yours isn't too bad but it's still hard to follow. |
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 198
Rep Power: 1
![]() |
Re: Beginner "C" Calculator Problem
Thanks alot guys.
I will work on my code formatting, I see it is not so good. Still learning so I appreciate any input, thanks. I understand the point about feeding it a '0', will fix that. I will also remove the exit(0); and let main() return.Really appreciate the input. I now realize it was a dumb question to try and exit the program while it is running, awaiting user input. My apologies guys, learning by tria and error. Thanks to all |
|
|
|
|
|
#5 | ||
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,120
Rep Power: 5
![]() |
Re: Beginner "C" Calculator Problem
Quote:
I have two suggestions for your code. First is to always (always!) validate input, especially if it comes directly from the user. If it's coming from a file, you should still validate it, but you might get by functioning under the assumption that it's valid (which is often the case if the file is the output of a program, and has not been tampered with). However, direct user input needs validation. Even if the user doesn't deliberately enter erroneous information, mistakes happen (plus, some users are just plain stupid). If you don't validate the input, you're asking for trouble, particularly if you subsequently use it in an operation. The second thing is to use a more modular approach to your coding. Use functions more, and make those functions as self-contained as possible. For example, when you want to get a number from the user, you could write a function to do so. You could have two parameters to the function. One of these would be a pointer to the number to be filled in, while the other would be a string to use as a prompt. The function would return a value indicating success or failure (failure generally being a case of user error, such as entering 'hello' when asked for a number). Note that this allows you to easily use the first point, without cluttering up your code. While I realise you might have chosen C because it is freely available and seems like a logical first step to learning a higher-level language like C++, you might want to reconsider. Generally speaking, the lower-level the language, the harder it is to learn. Since C++ can do everything C can (in fact, most valid C programs are also valid C++ programs), there is little reason not to use if it's available (and it probably is, given the number of free C++ compilers out there). More than that, many things are safer or easier in C++, string handling being a notable example. If you're concerned your purchase of the books (if indeed you purchased them, rather than borrowed them from a library or something) will be wasted if you move to a different language, don't worry. Much of the language syntax from C is identical in C++, so you can still use everything in that book. It's just that the usual approach to problems in C++ tends to differ because the programmer can make use of language features not available in C. Another good choice, particularly if you're wanting to write GUI applications for Windows, is C#. It's another language using the same general syntax as C and C++, and is basically Microsoft's answer to Java (and frankly, I think they did a better job with the language than sun did with Java). I was rather skeptical of C# before I tried it, perhaps because I'd already been exposed to Java, but after giving it a shot, it's become one of my favorite languages. Like C++, compilers and IDEs for C# are freely available. The downside here is that your books will be less applicable, as you're basically forced to use classes. Still, much of the basic stuff will still apply, such as loops, conditional expressions, and data types. I'm not trying to discourage you from learning C, here. Rather, I'm just saying there are other alternatives, and you might find some of them more suited to being your first language. Quote:
![]()
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
||
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 198
Rep Power: 1
![]() |
Re: Beginner "C" Calculator Problem
Hi Lectricpharaoh.
Firstly thanks for taking time to inform me of the possibilities and advantages out there corncerning the different languages. All my study material is downloaded so it's okay. Figured I'll finish up with SAMS teach yourself C as I am now busy with Chapter 18, and have only 3 left to go. But I don't rush it. I see what you mean by using functions in the calculator program. I didn't even consider it, but it makes sense, especially that I can have it check for a number being entered, not a word, as suggested. I will definitely take a look at C# and C++. Especially as a first language. Appreciate your support and will search properly before posting as to avoid re-posting of old threads. Thanks Again BstrucT |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| I'm a Beginner | Chuckiferd | Python | 37 | Dec 19th, 2007 2:09 PM |
| beginner question | dchankhour | C | 9 | Jan 19th, 2006 8:55 AM |
| beginner project -- Bingo! | Jessehk | Project Ideas | 10 | Oct 25th, 2005 2:00 AM |
| what open source code is good to read for a beginner? | linuxpimp20 | Other Programming Languages | 22 | Aug 30th, 2005 4:01 PM |
| Sorry to ask...VBScript beginner question | mackenga | JavaScript and Client-Side Browser Scripting | 6 | Aug 27th, 2005 12:18 PM |