Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How can i make this code better?

Reply
Thread Tools

How can i make this code better?

 
 
eli m
Guest
Posts: n/a
 
      03-09-2013
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;
}
}
}
}
}
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      03-09-2013
eli m <(E-Mail Removed)> writes:
>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.

 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      03-09-2013
eli m <(E-Mail Removed)> writes:
>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.

 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      03-10-2013
On Sat, 2013-03-09, eli m wrote:
> 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

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
FAQ 5.9 How can I make a filehandle local to a subroutine? How do I pass filehandles between subroutines? How do I make an array of filehandles? PerlFAQ Server Perl Misc 0 01-12-2011 11:00 PM
Freeware to make animated GIF (can Irfanview make an animated GIF?) Annu Pai Digital Photography 4 11-23-2009 12:52 AM
how to make corresponding java code for html code david wolf Java 1 04-21-2006 09:58 PM
Using a photo-editor to make web pages - how can I reduce the source code? Joey HTML 15 01-10-2006 09:51 AM



Advertisments