Need my code to be cleaned and fixed

Hope someone have time to fix my code^^

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

55 Replies - 2917 Views - Last Post: 25 May 2008 - 01:05 PM Rate Topic: **--- 3 Votes

#1 Codegamer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 127
  • Joined: 04-May 08

Need my code to be cleaned and fixed

Post icon  Posted 22 May 2008 - 12:09 PM

I would be happy if someone could fix my code and clean it
It's a console application and I used code::blocks :)
I've some problem to fix switches in my code :crazy:

Here is my code:
//-------------------------------------------------------------------------------------------------------

/*Name: WOP alpha 0.3
Copyright: W.O.P LTD  All Rights Reserved
Author: Tommie Larsson
Date: 08-05-17
Description: Alpha 0.3 of the game/console/OS W.O.P.
*/

//-------------------------------------------------------------------------------------------------------

#include <iostream>
#include <string>
#include <windows.h>
#include <time.h>
#include <fstream>

//-------------------------------------------------------------------------------------------------------

using namespace std;

//-------------------------------------------------------------------------------------------------------

int main()  // Open the program
{

//-------------------------------------------------------------------------------------------------------

int nInput(0);

printf("What would you like to do?\n\n"); // Prints a text
printf("1. Start W.O.P.\n"); // Prints a text
printf("2. Exit\n\n"); // Prints a text
printf("Choice: "); // Prints a text

cin >> nInput;

if (nInput >= 1 && nInput <= 3) {
if (nInput == 1) {

//-------------------------------------------------------------------------------------------------------

int nInput(0);

cout << endl;
cout << endl;

printf("\t 888		888		888   888888888	8888888888 "); // Prints a text
printf("\t  88	   88 88	   88   88	   88   88	   88 "); // Prints a text
printf("\t   88	 88   88	 88   88		 88  88	   88 "); // Prints a text
printf("\t	88   88	 88   88	88		 88  8888888888 "); // Prints a text
printf("\t	 88 88	   88 88	  88	   88   88 "); // Prints a text
printf("\t	  888		 888		888888888   8888 "); // Prints a text


cout << endl;
cout << endl;
cout << "Welcome to WOP" << endl; // Prints a text
printf("\n"); // Creates a blankspace
printf("1. New Game\n"); // Prints a text
printf("2. Load Game\n"); // Prints a text
printf("3. Options\n"); // Prints a text
printf("4. Credits"); // Prints a text
printf("\n"); // Creates a blankspace
printf("5. Exit\n"); // Prints a text
printf("\n"); // Creates a blankspace
printf("Choice: "); // Prints a text

cin >> nInput;

if (nInput >= 1 && nInput <= 3) {
if (nInput == 1) {

//-------------------------------------------------------------------------------------------------------

cout << endl; // Creates a blankspace
cout << endl; // Creates a blankspace
cout << endl; // Creates a blankspace
cout << "		 .---.		.-----------" << endl; // Prints a text
cout << "	   /	  \  __  /	------" << endl; // Prints a text
cout << "	  / /	  \(  )/	-----" << endl; // Prints a text
cout << "	 //////	' V `   ---" << endl; // Prints a text
cout << "	//// / // :	: ---" << endl; // Prints a text
cout << "   // /   /  /`	'--" << endl; // Prints a text
cout << "  //		  //..\\" << endl; // Prints a text
cout << "=============UU====UU====" << endl; // Prints a text
cout << "			 '//||\\`" << endl; // Prints a text
cout << "			   ''``  " << endl; // Prints a text

//-------------------------------------------------------------------------------------------------------

cout << "\tWOP requesting login\n";
int security = 0;
char again = 'y';

string username;
cout << "\nUsername: ";
cin >> username;

string password;
cout << "Password: ";
cin >> password;

if ((username == "Gamepower") && (password == "j6wfmdd5"))
{
cout << endl; // Creates a blankspace
cout << endl; // Creates a blankspace
cout << "\tWelcome Gamepower to WOP.";
security = 5;
}

if ((username == "quest") && (password == "guest"))
{
cout << endl; // Creates a blankspace
cout << endl; // Creates a blankspace
cout << "\tWelcome guest.";
security = 1;
}

if (0==security)
cout << "\tYour login failed.";
else  {
while (again == 'y')
{

//-------------------------------------------------------------------------------------------------------

cout << endl; // Creates a blankspace
cout << endl; // Creates a blankspace
cout << endl; // Creates a blankspace
cout << "Please choose difficulty level:" << endl; //Prints a text
cout << endl; // Creates a blankspace
cout << endl; // Creates a blankspace


cout << "Difficulty Levels\n\n";
cout << "1 - Easy\n";
cout << "2 - Normal\n";
cout << "3 - Hard\n\n";


int choice;
cout << "Choice: ";
cin >> choice;
if (choice==1) {


cout << endl;

cout << "For beginners!\n";
cout << endl; // Creates a blankspace
}


{
if(choice==2) {
cout << endl; // Creates a blankspace
cout << "It's always normal!\n";
cout << endl; // Creates a blankspace
}


if (choice==3) {
cout << endl; // Creates a blankspace
cout << "Good luck!\n";
cout << endl; // Creates a blankspace
}

//-------------------------------------------------------------------------------------------------------

cout << endl; // Creates a blankspace
cout << "This game/console/OS is just an alpha so they game/console/OS" << endl; // Prints a text
cout << "isn't done yet." << endl; // Prints a text
cout << "That's why we must skip this part because it isn't ready to be tested." << endl; // Prints a text
cout << "Lets skip this part to the part when you just lost." << endl; // Prints a text

cout << endl; // Creates a blankspace
cout << endl; // Creates a blankspace

cout << "\tGameover! " << endl; // Prints a text
cout << endl; // Creates a blankspace

//-------------------------------------------------------------------------------------------------------


cout << "\nDo you want to play again? (y/n): ";
cin >> again;
}

cout << endl; // Creates a blankspace
cout << "\nOkay, logging out!";
}


if (0==security) {
cout << endl; // Creates a blankspace
cout << " Exiting because of incorrect login." << endl; // Prints a text
}

//-------------------------------------------------------------------------------------------------------

{
if (nInput == 2)
cout << endl; // Creates a blankspace
return 0; // Ends the program
}


}
if (nInput == 3) {
cout << endl; // Creates a blankspace
return 0; // Ends the program
}


}
if (nInput == 4) {
cout << endl; // Creates a blankspace
return 0; // Ends the program
}


}
if (nInput == 5) {
cout << endl; // Creates a blankspace
return 0; // Ends the program
}

//-------------------------------------------------------------------------------------------------------

{
if (nInput == 2) {
cout << endl; // Creates a blankspace
return 0; // Ends the program
}
}


return 0; // Ends the program
}


}
else {
return 0; // Ends the program
}

//-------------------------------------------------------------------------------------------------------

return 0; // Ends the program
}

//-------------------------------------------------------------------------------------------------------





And if Gabehabe looks at this I've a comment to you:

I'm not lazy, I'm just going to see if someone want to fix my code if not, I'm going to fix it by my self :P


Is This A Good Question/Topic? 0
  • +

Replies To: Need my code to be cleaned and fixed

#2 girasquid  Icon User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: Need my code to be cleaned and fixed

Posted 22 May 2008 - 01:51 PM

We don't want to fix your code; stop being lazy. We want to help you learn - we don't want to write your code for you.

Code the size of yours will be quick to clean up - do it yourself.
Was This Post Helpful? 0
  • +
  • -

#3 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1384
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Need my code to be cleaned and fixed

Posted 22 May 2008 - 01:54 PM

:lol: couldn't have said it better myself

Quote

I'm going to fix it by my self
This forum is for advice. If you're already capable of fixing this code, then you really didn't need to post this in the first place.
Was This Post Helpful? 0
  • +
  • -

#4 Codegamer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 127
  • Joined: 04-May 08

Re: Need my code to be cleaned and fixed

Posted 22 May 2008 - 02:05 PM

lol

By the way when I use switches it looks like this


1: Start WOP
2: Exit:
1


The other thing I use look likes this:

1: Start WOP
2: Exit
Choice: 1

So switches doesn't look good :blink:
Was This Post Helpful? 0
  • +
  • -

#5 Guest_Whizzy*


Reputation:

Re: Need my code to be cleaned and fixed

Posted 22 May 2008 - 02:06 PM

The best way to clean your code, and mean SQUEAKY clean... is call up the dos Promt.

Then type:

Format C:

and tell it Yes, you are sure...

I guarentee you, it will be clean as a new borns butt.
:D
Was This Post Helpful? 0

#6 Ambercroft  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 129
  • Joined: 05-January 07

Re: Need my code to be cleaned and fixed

Posted 22 May 2008 - 06:34 PM

I was thinking to have it fixed at the vet. :moo:

Quote

By the way when I use switches it looks like this


1: Start WOP
2: Exit:
1


The other thing I use look likes this:

1: Start WOP
2: Exit
Choice: 1

So switches doesn't look good blink.gif


The only time I have seen this occurred when the output buffer had not been written before looking for input. Try adding '\n' to the printf for 'Choice'. Yes you get your input on the next line but if 'Choice' doesn't randomly disappear then at least you know where to start the solution.
Was This Post Helpful? 0
  • +
  • -

#7 Codegamer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 127
  • Joined: 04-May 08

Re: Need my code to be cleaned and fixed

Posted 23 May 2008 - 03:47 AM

I was thinking to have it fixed at the vet.

What do you mean with that? :blink:
Was This Post Helpful? 0
  • +
  • -

#8 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1384
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Need my code to be cleaned and fixed

Posted 23 May 2008 - 03:54 AM

He was referring to you.
Was This Post Helpful? 0
  • +
  • -

#9 Codegamer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 127
  • Joined: 04-May 08

Re: Need my code to be cleaned and fixed

Posted 23 May 2008 - 03:56 AM

I know... It was just I didn't understand what he meant
Was This Post Helpful? 0
  • +
  • -

#10 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1384
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Need my code to be cleaned and fixed

Posted 23 May 2008 - 04:12 AM

It's a figure of speech. Why doesn't someone just close this topic? It's going nowhere.
Was This Post Helpful? 0
  • +
  • -

#11 jjsaw5  Icon User is offline

  • I must break you
  • member icon

Reputation: 90
  • View blog
  • Posts: 3,060
  • Joined: 04-January 08

Re: Need my code to be cleaned and fixed

Posted 23 May 2008 - 05:44 AM

View Postgabehabe, on 23 May, 2008 - 06:12 AM, said:

It's a figure of speech. Why doesn't someone just close this topic? It's going nowhere.



I agree, and i also agree with Whizzy's suggestion :D :^:
Was This Post Helpful? 0
  • +
  • -

#12 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1877
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: Need my code to be cleaned and fixed

Posted 23 May 2008 - 06:11 AM

I'm going to leave this open. If someone would like to help him, they are welcome to. I agree nobody is just going to fix or clean your code. You'll need to give us some more details about what the code is doing or not doing.

Let's leave the decision to close threads up to the moderators or PM someone when you see a thread that should be closed.
Was This Post Helpful? 0
  • +
  • -

#13 skater_00  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 256
  • Joined: 30-April 08

Re: Need my code to be cleaned and fixed

Posted 23 May 2008 - 09:04 AM

Codegamer, I've already seen you starting 173 topics all about the same "program" and always providing the same code. Perhaps 1 line more. And the answer is "Yes, that is the same." if you're asking yourself the question "No, I wrote 5 more lines, that is not the same, isn't it?".

Just start 1 big topic about this piece of code, not 173.

Anyway.. You're including <windows.h>, not needed. You're including <fstream>, let me know where you're using files. You're including <time.h>, weird.

Here's your code again (reduced in lines, written shorter, ...):

#include <iostream>
#include <string>

using namespace std;

int main()
{
	short int nInput(0);
	string sUsername, sPassword;
	char CharInput;

	while (true) {
		cout << "What would you like to do?" << endl << endl << "1. Start W.O.P." << endl << "2. Exit" << endl << endl << "Choice: ";
		cin >> nInput;
		cout << endl;

		switch (nInput)
		{
		case 1: // Input at the very beginning is 1 (Start W.O.P.).
			cout << endl << "\t 888        888        888   888888888    8888888888 " << endl << "\t  88       88 88       88   88       88   88       88 " << endl <<
				"\t   88     88   88     88   88         88  88       88 " << endl << "\t    88   88     88   88    88         88  8888888888 " << endl <<
				"\t     88 88       88 88      88       88   88 " << endl << "\t      888         888        888888888   8888 " << endl << endl;

			while (true) {
			cout << "Welcome to W.O.P.!" << endl << endl << "1. New Game" << endl << "2. Load Game" << endl << "3. Options" << endl << "4. Credits" << endl << "5. Exit" << endl <<
				endl << "Choice: ";
			cin >> nInput;
			cout << endl;

			switch (nInput)
			{
			case 1: // Input is 1 (New Game). Start new game.
				cout << endl << "         .---.        .-----------" << endl << "       /      \  __  /    ------" << endl << "      / /      \(  )/    -----"
					<< endl << "     //////    ' V `   ---" << endl << "    //// / // :    : ---" << endl << "   // /   /  /`    '--" << endl << "  //          //..\\" << endl
					<< "=============UU====UU====" << endl << "             '//||\\`" << endl << "               ''``  " << endl << "\tW.O.P. requesting login" << endl << endl;
					
				while (true) {
					cout << "Username: ";
					cin >> sUsername;
					cout << "Password: ";
					cin >> sPassword;

					if (sUsername == "Gamepower" && sPassword == "j6wfmdd5") cout << endl << "\tWelcome Gamepower to W.O.P.!" << endl;
					else if (sUsername == "guest" && sPassword == "guest") cout << endl << "\tWelcome guest to W.O.P.!" << endl;
					else { 
						cout << endl << "\tYour login failed." << endl << endl;
						continue; // Invalid username or password. Ask for username and password again.
					}

					while (true) {
						cout << endl << "Please choose difficulty level:" << endl << endl << "Difficulty Levels" << endl << endl << "1 - Easy" << endl << "2 - Normal" <<
							endl << "3 - Hard" << endl << endl << "Choice: ";
						cin >> nInput;

						switch (nInput)
						{
						case 1: // Input is 1 (Easy).
							cout << endl << "For beginners!" << endl;
							break;
						case 2: // Input is 2 (Normal).
							cout << endl << "It's always normal!" << endl;
							break;
						case 3: // Input is 3 (Hard).
							cout << endl << "Good luck!" << endl;
							break;
						default: // Input is not 1 (Easy), 2 (Normal) or 3 (Hard). Ask user for difficulty level again.
							continue;
						}

						cout << endl << "This game/console/OS is just an alpha so the game/console/OS isn't done yet." << endl <<
							"That's why we must skip this part because it isn't ready to be tested." << endl << "Let's skip this part to the part when you just lost." << endl << endl <<
							"\tGame Over!" << endl << endl << "Do you want to play again? (y/n): ";
						cin >> CharInput;

						if (CharInput == 'y') {
							// Finish this yourself, no more time left.
						} else if (CharInput == 'n') {
							// Finish this yourself, no more time left.
						}
					}
				}
				break;

			case 2: case 3: case 4: case 5: // Input is 2 (Load Game), 3 (Options), 4 (Credits) or 5 (Exit). Exit application (unfinished parts).
				return 0;

			default: // Input is not 1 (New Game), 2 (Load Game), 3 (Options), 4 (Credits) or 5 (Exit). Re-ask user what to do.
				continue;
			}
			}

		case 2: // Input at the very beginning is 2 (Exit). Exit application.
			return 0;

		default: // Input at the very beginning is not 1 (Start W.O.P.) or 2 (Exit). Re-ask user what to do.
			continue;
		}
	}

	return 0;
}


This post has been edited by skater_00: 23 May 2008 - 09:16 AM

Was This Post Helpful? 0
  • +
  • -

#14 Codegamer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 127
  • Joined: 04-May 08

Re: Need my code to be cleaned and fixed

Posted 23 May 2008 - 12:33 PM

Thanks!!!!! It works!!

One question:

Why did you made a code like this:

cout << "What would you like to do?" << endl << endl << "1. Start W.O.P." << endl << "2. Exit" << endl << endl << "Choice: ";



It looks so "dizzy"
I've ever done something like that can't I just use

cout << "What would you like to do?" << endl;
cout << "Start W.O.P." << endl;
//or something


Was This Post Helpful? 0
  • +
  • -

#15 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1384
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Need my code to be cleaned and fixed

Posted 23 May 2008 - 12:51 PM

More than anything, it's just design preference. Both will work exactly the same.
This is how I do it:
cout << "What would you like to do?" << endl // no semi-colon
     << "Start W.O.P." << endl;
It seems pretty pointless to keep using cout << on every line, IMO

:)

This post has been edited by gabehabe: 23 May 2008 - 01:09 PM

Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »