• Welcome to Valhalla Legends Archive.
 

[C++] I need tips on my code

Started by Siege, December 25, 2003, 07:13 PM

Previous topic - Next topic

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!
#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();
}


Any tips appreciated!

[Edit: Fixed tabbing as much as I could.]

MrRaza

Fix the indenting, aswell, what if I want to add/multiply more than two numbers at once?

Kp

Don't use cin.  The way you're using it, it reacts very badly to non-numeric input iirc.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

MrRaza

What would be recommended to use instead of cin? What if he added non-numeric bulletproofing?

Kp

Quote from: MrRaza on December 26, 2003, 01:18 AM
What would be recommended to use instead of cin? What if he added non-numeric bulletproofing?

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.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

iago

#5
Quote from: Siege on December 25, 2003, 07:13 PM

...
             cout << "Input Error\n";
             cin >> input4;
             cin.get();
....


I don't quite understand what that does.  

Edit by Skywing: iago can't close tags in the right order! <fixed, was severely confusing the forum>
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


MrRaza

shouldnt it be cin.getline();

// instead of

cin.get();

Kp

Quote from: MrRaza on December 26, 2003, 10:35 AMshouldnt it be cin.getline();

Pretty sure cin.getline(...) takes parameters, which your invocation does not. :)
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

thetempest


Siege

#9
Like I said I'm pretty new to C++ :).
Quote from: iago on December 26, 2003, 05:42 AM
Quote from: Siege on December 25, 2003, 07:13 PM

...
             cout << "Input Error\n";
             cin >> input4;
             cin.get();
....


I don't quite understand what that does.  

Edit by Skywing: iago can't close tags in the right order! <fixed, was severely confusing the forum>

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 -_-.

Arta

Quote from: Siege on December 25, 2003, 07:13 PM

int input,add(),multiply(),input4; //variables and functions


That's pretty strange. Most people would do it more like this:



// Function signatures
int add();
int multiply();

int main()
{
   int input, input4;
   
   //...
}

int add()
{
   // ...
}

int multiply()
{
   // ...
}


afaik, there's nothing 'officially' wrong with the way you did it, though.

iago

Quote from: Arta[vL] on December 27, 2003, 04:52 AM
Quote from: Siege on December 25, 2003, 07:13 PM

int input,add(),multiply(),input4; //variables and functions


That's pretty strange. Most people would do it more like this:



// Function signatures
int add();
int multiply();

int main()
{
   int input, input4;
   
   //...
}

int add()
{
   // ...
}

int multiply()
{
   // ...
}


afaik, there's nothing 'officially' wrong with the way you did it, though.

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.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


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.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

CupHead

Quote from: Kp on December 27, 2003, 11:24 AM
... 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.

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.

Kp

Quote from: CupHead on December 27, 2003, 11:29 AM
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.

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
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!