Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: Siege on December 25, 2003, 07:13 PM

Title: [C++] I need tips on my code
Post by: Siege on December 25, 2003, 07:13 PM
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.]
Title: Re:[C++] I need tips on my code
Post by: MrRaza on December 25, 2003, 07:26 PM
Fix the indenting, aswell, what if I want to add/multiply more than two numbers at once?
Title: Re:[C++] I need tips on my code
Post by: Kp on December 26, 2003, 12:03 AM
Don't use cin.  The way you're using it, it reacts very badly to non-numeric input iirc.
Title: Re:[C++] I need tips on my code
Post by: MrRaza on December 26, 2003, 01:18 AM
What would be recommended to use instead of cin? What if he added non-numeric bulletproofing?
Title: Re:[C++] I need tips on my code
Post by: Kp on December 26, 2003, 01:40 AM
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.
Title: Re:[C++] I need tips on my code
Post by: 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>
Title: Re:[C++] I need tips on my code
Post by: MrRaza on December 26, 2003, 10:35 AM
shouldnt it be cin.getline();

// instead of

cin.get();
Title: Re:[C++] I need tips on my code
Post by: Kp on December 26, 2003, 12:24 PM
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. :)
Title: Re:[C++] I need tips on my code
Post by: thetempest on December 26, 2003, 10:32 PM
cin.getline(str,len,dilimiter);
Title: Re:[C++] I need tips on my code
Post by: Siege on December 27, 2003, 02:36 AM
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 -_-.
Title: Re:[C++] I need tips on my code
Post by: Arta 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.
Title: Re:[C++] I need tips on my code
Post by: iago on December 27, 2003, 06:45 AM
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.
Title: If you people knew how to use consoles...
Post by: 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.
Title: Re:If you people knew how to use consoles...
Post by: CupHead on December 27, 2003, 11:29 AM
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.
Title: Re:If you people knew how to use consoles...
Post by: Kp on December 27, 2003, 12:05 PM
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
Title: Re:[C++] I need tips on my code
Post by: CupHead on December 27, 2003, 06:51 PM
So maybe I exaggerated slightly, but you get the idea.  :P
Title: Re:[C++] I need tips on my code
Post by: iago on December 27, 2003, 09:24 PM
Right now I'm putting my source in /usr/iago/sources/c :)