How can i make this code better?

E

eli m

Any idea on how i could make this code better?

My code:

#include <iostream>
#include <string>
using namespace std;

int main()
{
int run = 0;
string function, mathop;
cout << "Type in help for a list of functions\n";
//Start main loop
while (run == 0)
{
cout << "Type in a function:";
cin >> function;
//Math function
if (function == "math")
{
int mathloop;
mathloop = 0;
//Start while loop for math function
while (mathloop == 0)
{
cout << "What math operation do you want to use?";
cin >> mathop;
// Multiplication Math Function
if (mathop == "multiplication")
{
int mfirstnum, msecondnum, ans;
cout << "Type in your first number:";
cin >> mfirstnum;
cout << "Multiply your first number by:";
cin >> msecondnum;
ans = mfirstnum * msecondnum;
cout << ans << "\n";
}
//Break out of mathloop if main is entered
else if (mathop == "main")
{
mathloop = 1;
}
}
}
}
}
 
S

Stefan Ram

eli m said:
Any idea on how i could make this code better?

Splitting sensibly into several smaller functions, each of
which has a small documentation of its interface in front of
its definition and maybe adding a small user manual at the
start of the source code for the user of the program
describing the overall purpose of the program.
 
S

Stefan Ram

eli m said:
int main()
{
int run = 0;

Add indentation corresponding with nesting depth.
»boolean running = false;«
string function, mathop;
cout << "Type in help for a list of functions\n";

"Type in \"help\" for a list of functions.\n"
//Start main loop
while (run == 0)

while( running )
{
cout << "Type in a function:";
cin >> function;
//Math function
if (function == "math")

I am not aware of a function named »math«.

You might add some code to report errors,
e.g., unintelligible user input.
 
J

Jorgen Grahn

Any idea on how i could make this code better? ....
cout << "Type in help for a list of functions\n";

A meta-answer: these "please enter ..." exercises have been popular
since I was a kid, but in reality they don't make anyone happy. Noone
uses programs like that, not on Unix and not on Windows. And they are
hard to write.

How about this one instead?

The program reads lines of text from standard input (std::cin) and
writes to standard output. Lines which look like multiplication

number * number

will be evaluated and printed. Other lines will be silently ignored.

Then you can add a lot of things ...
- define more exactly what a "number" is
- what to do about numerical overflow
- add more operators
- add full expressions
- write the user manual
- create test suites
- improve error handling
- ...
In the end you will have the Unix bc(1) utility.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top