Author | Message | Time |
---|---|---|
Siege | Hi and Merry Christmas all :) I did a small console app in C++ and I want to know what I could do to improve my code. What it basically does is it ask you to choose from addition, multiplication or exit and I bet you can figure it out from what it does from looking at my code. By the way I just started learning C++ so my code will probably and most likely suck. So here it is! [code]#include <iostream> int main() { int input,add(),multiply(),input4; //variables and functions cout << "Please choose one of the options below by typing the number\n"; cout << "1 add\n"; cout << "2 multiply\n"; cout << "3 Exit\n"; cin >> input; switch (input) { case 1: add(); break; case 2: multiply(); break; case 3: return 0; //exit default: cout << "Input Error\n"; cin >> input4; cin.get(); } return 0; } int add() //my add function { int input1,input2,answer,number; //variables cout << "You chose Addition!\n"; cout << " Please type one number: "; cin >> input1; cout << " Please type another number: "; cin >> input2; answer = input1 + input2; cout << " The answer is " << answer << endl; cout << " Press any number to exit.\n"; cin >> number; cin.get(); } int multiply() //My multiplication function { int input1,input2,answer,number; //variables cout << "You chose Multiplication!\n"; cout << " Please type one number: "; cin >> input1; cout << " Please type another number: "; cin >> input2; answer = input1 * input2; cout << " The answer is " << answer << endl; cout << " Press any number to exit.\n"; cin >> number; cin.get(); } [/code] Any tips appreciated! [Edit: Fixed tabbing as much as I could.] | December 26, 2003, 1:13 AM |
MrRaza | Fix the indenting, aswell, what if I want to add/multiply more than two numbers at once? | December 26, 2003, 1:26 AM |
Kp | Don't use cin. The way you're using it, it reacts very badly to non-numeric input iirc. | December 26, 2003, 6:03 AM |
MrRaza | What would be recommended to use instead of cin? What if he added non-numeric bulletproofing? | December 26, 2003, 7:18 AM |
Kp | [quote author=MrRaza link=board=30;threadid=4452;start=0#msg37153 date=1072423126] What would be recommended to use instead of cin? What if he added non-numeric bulletproofing?[/quote] As written, he can't add proofing against non-numeric input because cin methods are in control during the reading. I'd say either use the stdio library, or, if you're determined to use cin, use cin.getline(...) to read in an entire line, then parse it up internally so you can validate the fields as you go. | December 26, 2003, 7:40 AM |
iago | [quote author=Siege link=board=30;threadid=4452;start=0#msg37132 date=1072401189] [code]... cout << "Input Error\n"; cin >> input4; cin.get(); ....[/code] I don't quite understand what that does. [/quote] Edit by Skywing: Iago can't close tags in the right order! <fixed, was severely confusing the forum> | December 26, 2003, 11:42 AM |
MrRaza | shouldnt it be [code]cin.getline(); // instead of cin.get();[/code] | December 26, 2003, 4:35 PM |
Kp | [quote author=MrRaza link=board=30;threadid=4452;start=0#msg37172 date=1072456510]shouldnt it be [code]cin.getline();[/code][/quote] Pretty sure cin.getline(...) takes parameters, which your invocation does not. :) | December 26, 2003, 6:24 PM |
thetempest | cin.getline(str,len,dilimiter); | December 27, 2003, 4:32 AM |
Siege | Like I said I'm pretty new to C++ :). [quote author=iago link=board=30;threadid=4452;start=0#msg37157 date=1072438971] [quote author=Siege link=board=30;threadid=4452;start=0#msg37132 date=1072401189] [code]... cout << "Input Error\n"; cin >> input4; cin.get(); ....[/code] I don't quite understand what that does. [/quote] Edit by Skywing: Iago can't close tags in the right order! <fixed, was severely confusing the forum> [/quote] Well I didn't know how to show the "Input Error" text without the console app closing in a flash :P. I guess I'll do a loop until the user chooses a valid choice. What is dilimiter BTW? Oh yeah, I'm using Bloodshed Dev-C++ compiler if anyone wanted know -_-. | December 27, 2003, 8:36 AM |
Arta | [quote author=Siege link=board=30;threadid=4452;start=0#msg37132 date=1072401189] [code] int input,add(),multiply(),input4; //variables and functions [/code] [/quote] That's pretty strange. Most people would do it more like this: [code] // Function signatures int add(); int multiply(); int main() { int input, input4; //... } int add() { // ... } int multiply() { // ... } [/code] afaik, there's nothing 'officially' wrong with the way you did it, though. | December 27, 2003, 10:52 AM |
iago | [quote author=Arta[vL] link=board=30;threadid=4452;start=0#msg37228 date=1072522363] [quote author=Siege link=board=30;threadid=4452;start=0#msg37132 date=1072401189] [code] int input,add(),multiply(),input4; //variables and functions [/code] [/quote] That's pretty strange. Most people would do it more like this: [code] // Function signatures int add(); int multiply(); int main() { int input, input4; //... } int add() { // ... } int multiply() { // ... } [/code] afaik, there's nothing 'officially' wrong with the way you did it, though. [/quote] I actually didn't even notice that, but it does look silly. I've never seen anybody do it before, but I'm pretty sure it's perfectly legal. Some comments: You *can* use system("pause") to wait for a keypress, but Skywing will tell you why that's a bad thing Because Add, multiply, etc. all use two operators, you should do the inputting in your main function. Or at least, I would. And pass in the two operators as parameters. | December 27, 2003, 12:45 PM |
Kp | ... then you'd know better than to run a console application in a transient console. Run it from a preexisting one and it won't go away when the program exits. | December 27, 2003, 5:24 PM |
CupHead | [quote author=Kp link=board=30;threadid=4452;start=0#msg37249 date=1072545888] ... then you'd know better than to run a console application in a transient console. Run it from a preexisting one and it won't go away when the program exits. [/quote] That would be the ideal solution, but some of us (me) use paths like: d:\Projects\Programming\Visual Studio .NET 2002\Visual C++\Project Name\Files\Binaries\bin\ProjectName.exe So... Trying to get there is more effort than stopping a transient console programatically. | December 27, 2003, 5:29 PM |
Kp | [quote author=CupHead link=board=30;threadid=4452;start=0#msg37250 date=1072546180] That would be the ideal solution, but some of us (me) use paths like: d:\Projects\Programming\Visual Studio .NET 2002\Visual C++\Project Name\Files\Binaries\bin\ProjectName.exe So... Trying to get there is more effort than stopping a transient console programatically.[/quote] Sounds like it's your own fault for using horrible pathnames. :) Mine are usually about 4 levels off drive root, and I have an environment variable that points to the master programming directory. So all it takes is 'cd %SOURCE%\myprog' and I'm there. Out of curiousity, why 'Binaries\bin'? It seems redundant. On that same vein, why 'Project Name\Files'? What nonfiles would you be storing in the Project Name directory? :P | December 27, 2003, 6:05 PM |
CupHead | So maybe I exaggerated slightly, but you get the idea. :P | December 28, 2003, 12:51 AM |
iago | Right now I'm putting my source in /usr/iago/sources/c :) | December 28, 2003, 3:24 AM |