|
-
March 24th, 2007, 03:24 PM
#1
Could someone help me compress this code a little?
Hey, I just wrote this program when messing about with the FOR and IF commands and I want to know if someone could help me make it look less....well, make it more efficient if you understand what I mean. Thanks in advance.
Code:
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string name;
string quit; // Just using this to quit the program.
string correct;
string wrong;
cout << "Please enter your name: ";
cin >> name; // This is where you put a string into the name variable.
cout << "Your name is " << name; cout << ".\n"; // Dumps the variable contents of name to the screen.
restart: // Restart point.
cout << "Please type Quit and press enter. ";
cin >> quit;
if (quit == "Quit") // If the user types "quit".
{
cout << "Thankyou, please type Exit "; // Print message.
cin >> correct;
if (correct == "Exit") // If user types "exit".
{
goto exit; // Go to point "exit:"
}
else // If the user did NOT type "exit"
goto restart;
}
else // If the user did NOT type "quit"
cout << "You did not type Quit! .\n";
goto restart; // Go back!
exit: // Point exit:.
return 0;
}
x-Xaro-x
C++ n00b
/a ;-)
-
March 24th, 2007, 04:11 PM
#2
Re: Could someone help me compress this code a little?
Code:
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string name;
string quit; // Just using this to quit the program.
string correct;
string wrong;
cout << "Please enter your name: ";
cin >> name; // This is where you put a string into the name variable.
cout << "Your name is " << name; cout << ".\n"; // Dumps the variable contents of name to the screen.
int done = 0;
while (!done)
{
cout << "Please type Quit and press enter. ";
cin >> quit;
if (quit == "Quit") // If the user types "quit".
{
cout << "Thankyou, please type Exit "; // Print message.
cin >> correct;
if (correct == "Exit") // If user types "exit".
{
done = 1;
}
}
else
{
cout << "You did not type Quit! .\n";
}
}
return 0;
}
Is at least a partial cleanup. Even better is you state tro break it out to functions....
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
March 24th, 2007, 04:19 PM
#3
Re: Could someone help me compress this code a little?
Hi Xaro.
Eifficient in which way? Faster? Since this is a very simple program, you're probably beginning in C++. Perhaps, the first thing you might wanna try in your program is to rewrite it withoug using 'goto's. Usually, 'goto' is not a good practice from a software engineering point of view. For instance, in the case of your program you can achieve the same effect using a loop. Particularly, you could have something like this:
Code:
std::cin >> quit;
while (quit != "Quit")
{
std::cout << "You did not type quit!\n";
//...
std::cin >> quit;
}
A final observation is that you don't really need all those variables for the program.
-
March 24th, 2007, 05:49 PM
#4
Re: Could someone help me compress this code a little?
Cheers, i didnt think of using:
Code:
while (quit != "Quit")
And yes, I am just a beginner.

Xaro
x-Xaro-x
C++ n00b
/a ;-)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|