Internet Explorer may not be fully supported. The CSP Team recommends you to download FireFox or Google Chrome. Thank you!

+ Reply to Thread
Page 1 of 2
1 2 LastLast
Results 1 to 10 of 14

Thread: C++ doubts

  1. #1
    Senior Member n3krO's Avatar
    Join Date
    Jul 2010
    Location
    Málaga, Spain
    Posts
    3,904

    C++ doubts

    Hey dudes, i'm learning C++ with a program that makes C++ even more simple and run what i do...

    I've done a game where the first player chose a number from 1 to 1000 and the second player try to guess the number (he has 10 tries), when he miss it's given a tip (number is higher/number is lower)... it says how must tries the second player has left, if he loses what was the correct number etc, etc...

    It also has 3 languages supported.

    My question is, how to i open new things in new cmd windows or how do i delete everything written until the moment i want?
    Right now Player 2 can see what Player 1 typed :S

    Here i let you see the code:

    Code:
    int main() {
    
    	bool ;
    	int a;
    	int l;
    	int p;
    	int t;
    	int x;
    
    	x=1;
    	p=10;
    	cout<<"Type your language take in consideration:"<<endl;
    	cout<<"We only support Portuguese, Spanish and English"<<endl;
    	cout<<"Spanish = 1, English = 2, Portuguese = 3"<<endl;
    	cin>>l;
    	switch (int(l)) {
    	case 1:
    		cout<<""<<endl;
    		cout<<"Empezando el juego en Español"<<endl;
    		cout<<"________________________________________________________________________________"<<endl;
    		cout<<"Jugador 1"<<endl;
    		cout<<"Elige un numero desde 1 hasta 1000."<<endl;
    		cin>>a;
    		if (a>1000 || a<1) {
    			cout<<"Valor invalido, sigue las reglas."<<endl;
    		} else {
    			cout<<"Jugador 2"<<endl;
    			cout<<"Intenta advinar el numero, desde 1 hasta 1000. Cuidado, tienes "<<p<<" intentos."<<endl;
    			cin>>t;
    			if (t<a || t>a) {
    				while ((t<a || t>a) && p>0) {
    					x=x+1;
    					p=p-1;
    					if (p>0) {
    						if (t<a) {
    							cout<<"Has fallado, el numero es mayor. Tienes mas "<<p<<" intentos."<<endl;
    							cin>>t;
    						} else {
    							cout<<"Has fallado, el numero es mas chico. Tienes mas "<<p<<" intentos."<<endl;
    							cin>>t;
    						}
    					} else {
    						cout<<"Has Perdido! El numero correcto era: "<<a<<endl;
    					}
    				}
    				if (p>0) {
    					cout<<"Has acertado a la "<<x<<"ª!"<<endl;
    				}
    			} else {
    				if (p>0) {
    					cout<<"Has acertado a la "<<x<<"ª, impresionante!"<<endl;
    				}
    			}
    		}
    		break;
    	case 2:
    		cout<<""<<endl;
    		cout<<"Starting the game in English"<<endl;
    		cout<<"________________________________________________________________________________"<<endl;
    		cout<<"Player 1"<<endl;
    		cout<<"Choise a number from 1 to 1000."<<endl;
    		cin>>a;
    		if (a>1000 || a<1) {
    			cout<<"Invalid number, follow the rules"<<endl;
    		} else {
    			cout<<"________________________________________________________________________________"<<endl;
    			cout<<"Player 2"<<endl;
    			cout<<"Try to guess the number. Be carefull, you have "<<p<<" tries."<<endl;
    			cin>>t;
    			if (t<a || t>a) {
    				while ((t<a || t>a) && p>0) {
    					x=x+1;
    					p=p-1;
    					if (p>0) {
    						if (t<a) {
    							cout<<"You failed, the number is bigger. You have "<<p<<" tries left."<<endl;
    							cin>>t;
    						} else {
    							cout<<"You failed, the number is smaller, You have "<<p<<" tries left."<<endl;
    							cin>>t;
    						}
    					} else {
    						cout<<"You Lost! The correct number was: "<<a<<endl;
    					}
    				}
    				if (x==2) {
    					cout<<"You won on the "<<x<<"nd try!"<<endl;
    				} else {
    					if (x==3) {
    						cout<<"You won on the "<<x<<"rd try!"<<endl;
    					} else {
    						cout<<"You won on the "<<x<<"th try!"<<endl;
    					}
    				}
    			} else {
    				if (p>0) {
    					cout<<"You won on the "<<x<<"st try, impressive!"<<endl;
    				}
    			}
    		}
    		break;
    	case 3:
    		cout<<""<<endl;
    		cout<<"Começando o jogo em Portuguese"<<endl;
    		cout<<"________________________________________________________________________________"<<endl;
    		cout<<"Jogador 1"<<endl;
    		cout<<"Elige um numero desde 1 até 1000."<<endl;
    		cin>>a;
    		if (a>1000 || a<1) {
    			cout<<"Valor invalido, segue as regras."<<endl;
    		} else {
    			cout<<"Jogador 2"<<endl;
    			cout<<"Tenta advinhar o numero, desde 1 até 1000. Cuidado, tens "<<p<<" tentativas."<<endl;
    			cin>>t;
    			if (t<a || t>a) {
    				while ((t<a || t>a) && p>0) {
    					x=x+1;
    					p=p-1;
    					if (p>0) {
    						if (t<a) {
    							cout<<"Falhaste, o numero é maior. Tens mais "<<p<<" tentativas."<<endl;
    							cin>>t;
    						} else {
    							cout<<"Falhaste, o numero é mais pequeno. Tens mais "<<p<<" tentativas."<<endl;
    							cin>>t;
    						}
    					} else {
    						cout<<"Perdeste! O numero correcto era: "<<a<<endl;
    					}
    				}
    				if (p>0) {
    					cout<<"Acertaste à "<<x<<"ª!"<<endl;
    				}
    			} else {
    				if (p>0) {
    					cout<<"Acertaste à "<<x<<"ª, impresionante!"<<endl;
    				}
    			}
    		}
    		break;
    	default:
    		cout<<"Language Not Supported"<<endl;
    		break;
    	}
    
    	return 0;
    
    }

  2. #2
    Handyman sIMson's Avatar
    Join Date
    Apr 2010
    Location
    Poland
    Posts
    872
    A few tips from me:
    • First of all, where are your includes? you should put a: #include <iostream> line at the beginning because otherwise it won't work in any standard compiler
    • You also forgot to declare the used namespace (using namespace std; ), otherwise it won't work normally
    • Make the tab size smaller, it's totally unreadable with a tab size like here!
    • Instead of making a switch which repeats the same code three times you should for example use a table of strings for each message with index number determining the language
    • Do not use other characters than unicode, C++ doesn't like this
    • Use more descriptive variable names, a, p, x, l or stuff like that does not tell you what is this variable for, people have to guess it
    • C++ is an object-oriented programming language, use classes if you know how to do it.

    As for you questions: "clean" C++ doesn't support screen cleaning or opening multiple windows for the same program. The first one can be solved using the ncurses library, but if you are just a beginner it might be too hard for you.

    How about this: instead of letting someone else choose the number have the computer to randomize it for you. To do this you have to use the function rand(). It's in the stdlib.h library so you need to include this one.

    I'll try to fix this code a bit and paste it.
    <yuri-> inc did a double fix of that fix; a fix inside a fix; yo dawg

  3. #3
    Senior Member n3krO's Avatar
    Join Date
    Jul 2010
    Location
    Málaga, Spain
    Posts
    3,904
    I forgot to include this in the start:

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

    It's in the code but in the initial declarations, forgot to copy that.

    I don't understand what you say about the tab size.

    Instead of making a switch which repeats the same code three times you should for example use a table of strings for each message with index number determining the language
    You mean using variables and then have a list of the meaning of the variables depending on the language? Just like in CSPromod for example that the translating files are just files with the meaning of those variables in each language?

    Do not use other characters than unicode, C++ doesn't like this
    This is a big problems when translating to portugues and spanish :S i noticed when i runned the program it showed bugs with those caracters but i thought when i was a bit more experienced i would know how to run the program as if it was a normal program (and not as a cmd window) and that wouldn't be a problem anymore (change the font maybe?)

    Use more descriptive variable names, a, p, x, l or stuff like that does not tell you what is this variable for, people have to guess it
    I believe it's a big problem when doing huge programs, but as this has only 150 lines i could just check what it was if i had any problem in remembering going to take the advise for future things...

    C++ is an object-oriented programming language, use classes if you know how to do it.
    Could you elaborate better?
    __________________________________________________ __________________

    As for compiling, what program would you suggest me?

    I'm using the program i refered earlier to do the functions i want and then the program ported it into C++, while with the program it's easier to do simple things, when in C++ i can do more things. Also, if i had start doing this with C++ i would has not understand shit. The program i'm using has a run option where i run the program i'm doing without compiling, that's how i check if my work is going well (it also says if i did something wrong).

    Thanks for the tips, as for the first day working with this i think i did an awsome thing

  4. #4
    Handyman sIMson's Avatar
    Join Date
    Apr 2010
    Location
    Poland
    Posts
    872
    Tab size? Use less spaces for code intendation.

    As for the classes: http://en.wikipedia.org/wiki/C%2B%2B_classes

    Compiling: I'd suggest Code::Blocks or Dev-C++, both are free.
    <yuri-> inc did a double fix of that fix; a fix inside a fix; yo dawg

  5. #5
    Member
    Join Date
    Feb 2010
    Location
    NY
    Posts
    420
    For anyone who knows both, How different is C++ from java?
    I put on my robe and wizard hat.

  6. #6
    QA Tester [EA]'s Avatar
    Join Date
    Nov 2008
    Location
    Virginia, USAAAAAAAAAAAAAA
    Posts
    1,992
    Quote Originally Posted by k1no View Post
    For anyone who knows both, How different is C++ from java?
    They are pretty different but basically the same concept.

    System.out.println("Hello World");

    cout << "Hello World";
    Official CSPromod Mumble:
    IP: 193.27.193.4
    Port: 40000
    ----------------------------
    Join #cspromod @ QuakeNet to find gathers/mixes/scrims/PUGS/whateverthefuckyoucallthem
    Join #cspromod @ GameSurge to talk to the developers of CSPromod
    ----------------------------
    Send all your business propositions to: alex@myeg.net

  7. #7
    Senior Member n3krO's Avatar
    Join Date
    Jul 2010
    Location
    Málaga, Spain
    Posts
    3,904
    Quote Originally Posted by sIMson View Post
    Tab size? Use less spaces for code intendation.

    As for the classes: http://en.wikipedia.org/wiki/C%2B%2B_classes

    Compiling: I'd suggest Code::Blocks or Dev-C++, both are free.
    Downloaded Dev-C++, compiled the code i had, it works perfectly as with the other program only that when i win/lose the game it instantly close :S

    --- Update ---

    Updated the code, now use self explanatory variables.
    Updated the game, now it doesn't instantly close after the match finish, it has rules shown up, it asks if you want to repeat the match (only availiable to repeat with the same language), if you set up an unkown language it instantly switchs to english and asks if you want to play in english or leave the game.

    @sIMson could you explain how would i make AdminVariable randomized by the program?
    As far as i understand i would need to add: #include<stdlib.h>
    and replace the player1 part by: rand(AdminVariable);

    The problem is that AdminVariable would be able not to be within the values i want (1 and 1000), how would i make it randomize between 1 and 1000 (1 and 1000 included)?

    Thx for the help

    The code right now is:
    Code:
    #include<iostream>
    #include<cmath>
    using namespace std;
    
    int main() {
    
    	int AdminVariable;
    	int Language;
    	int TryLeft;
    	int Guess;
    	int CurrentTry;
    	int Repeat;
    
    	Repeat=1;
    	cout<<"Type your language take in consideration:"<<endl;
    	cout<<"We only support Portuguese, Spanish and English"<<endl;
    	cout<<"Spanish = 1, English = 2, Portuguese = 3"<<endl;
    	cin>>Language;
    	while (Repeat == 1) {
              switch (int(Language)) {
    	case 1:
            CurrentTry=1;
            TryLeft=10;
    		cout<<""<<endl;
    		cout<<"Empezando el juego en Español"<<endl;
    		cout<<"________________________________________________________________________________"<<endl;
    		cout<<"Reglas:"<<endl;
    		cout<<"1: Jugador1 elige un numero entre 1 y 1000"<<endl;
    		cout<<"2: Jugador2 intenta advinar ese numero, teniendo 10 intentos"<<endl;
    		cout<<"3: Cada vez que el jugador2 falla, se le da una pista"<<endl;
    		cout<<"________________________________________________________________________________"<<endl;
    		cout<<"Jugador 1"<<endl;
    		cout<<"Elige un numero entre 1 y 1000."<<endl;
    		cin>>AdminVariable;
    		if (AdminVariable>1000 || AdminVariable<1) {
    			cout<<"Valor invalido, sigue las reglas."<<endl;
    		} else {
    			cout<<"Jugador 2"<<endl;
    			cout<<"Intenta advinar el numero, desde 1 hasta 1000. Cuidado, tienes "<<TryLeft<<" intentos."<<endl;
    			cin>>Guess;
    			if (Guess<AdminVariable || Guess>AdminVariable) {
    				while ((Guess<AdminVariable || Guess>AdminVariable) && TryLeft>0) {
    					CurrentTry=CurrentTry+1;
    					TryLeft=TryLeft-1;
    					if (TryLeft>0) {
    						if (Guess<AdminVariable) {
    							cout<<"Has fallado, el numero es mayor. Tienes mas "<<TryLeft<<" intentos."<<endl;
    							cin>>Guess;
    						} else {
    							cout<<"Has fallado, el numero es mas chico. Tienes mas "<<TryLeft<<" intentos."<<endl;
    							cin>>Guess;
    						}
    					} else {
    						cout<<"Has Perdido! El numero correcto era: "<<AdminVariable<<endl;
    						cout<<"Quieres repetir? (Si = 1, No = 2)"<<endl;
    						cin>>Repeat;
    					}
    				}
    				if (TryLeft>0) {
    					cout<<"Has Ganado! Has acertado a la "<<CurrentTry<<"ª!"<<endl;
    					cout<<"Quieres repetir? (Si = 1, No = 2)"<<endl;
    					cin>>Repeat;
    				}
    			} else {
    				if (TryLeft>0) {
    					cout<<"Has Ganado! Has acertado a la "<<CurrentTry<<"ª, impresionante!"<<endl;
    					cout<<"Quieres repetir? (Si = 1, No = 2)"<<endl;
    					cin>>Repeat;
    				}
    			}
    		}
    		break;
    	case 2:
            CurrentTry=1;
    	    TryLeft=10;
    		cout<<""<<endl;
    		cout<<"Starting the game in English"<<endl;
    		cout<<"________________________________________________________________________________"<<endl;
    		cout<<"Rules:"<<endl;
    		cout<<"1: Player1 choise a number between 1 and 1000"<<endl;
    		cout<<"2: Player2 tries to guess the number, having 10 tries"<<endl;
    		cout<<"3: Each time player2 fails it's given a tip"<<endl;
    		cout<<"________________________________________________________________________________"<<endl;
    		cout<<"Player 1"<<endl;
    		cout<<"Choise a number from 1 to 1000."<<endl;
    		cin>>AdminVariable;
    		if (AdminVariable>1000 || AdminVariable<1) {
    			cout<<"Invalid number, follow the rules"<<endl;
    		} else {
    			cout<<"________________________________________________________________________________"<<endl;
    			cout<<"Player 2"<<endl;
    			cout<<"Try to guess the number. Be carefull, you have "<<TryLeft<<" tries."<<endl;
    			cin>>Guess;
    			if (Guess<AdminVariable || Guess>AdminVariable) {
    				while ((Guess<AdminVariable || Guess>AdminVariable) && TryLeft>0) {
    					CurrentTry=CurrentTry+1;
    					TryLeft=TryLeft-1;
    					if (Guess>0) {
    						if (Guess<AdminVariable) {
    							cout<<"You failed, the number is bigger. You have "<<TryLeft<<" tries left."<<endl;
    							cin>>Guess;
    						} else {
    							cout<<"You failed, the number is smaller, You have "<<TryLeft<<" tries left."<<endl;
    							cin>>Guess;
    						}
    					} else {
    						cout<<"You Lost! The correct number was: "<<AdminVariable<<endl;
    						cout<<"Want to repite? (Yes = 1, No = 2)"<<endl;
    						cin>>Repeat;
    					}
    				}
    				if (CurrentTry==2) {
    					cout<<"You won on the "<<CurrentTry<<"nd try!"<<endl;
    					cout<<"Want to repite? (Yes = 1, No = 2)"<<endl;
    					cin>>Repeat;
    				} else {
    					if (CurrentTry==3) {
    						cout<<"You won on the "<<CurrentTry<<"rd try!"<<endl;
    						cout<<"Want to repite? (Yes = 1, No = 2)"<<endl;
    						cin>>Repeat;
    					} else {
    						cout<<"You won on the "<<CurrentTry<<"th try!"<<endl;
    						cout<<"Want to repite? (Yes = 1, No = 2)"<<endl;
    						cin>>Repeat;
    					}
    				}
    			} else {
    				if (TryLeft>0) {
    					cout<<"You won on the "<<CurrentTry<<"st try, impressive!"<<endl;
    					cout<<"Want to repite? (Yes = 1, No = 2)"<<endl;
    					cin>>Repeat;
    				}
    			}
    		}
    		break;
    	case 3:
            CurrentTry=1;
            TryLeft=10;
    		cout<<""<<endl;
    		cout<<"Começando o jogo em Portuguese"<<endl;
    		cout<<"________________________________________________________________________________"<<endl;
    		cout<<"Regras:"<<endl;
    		cout<<"1: Jogador1 escolhe un numero entre 1 y 1000"<<endl;
    		cout<<"2: Jogador2 tenta advinhar eese numero, tendo 10 tentativas"<<endl;
    		cout<<"3: Cada vez que o jugador2 falha, da-se uma pista"<<endl;
    		cout<<"________________________________________________________________________________"<<endl;
    		cout<<"Jogador 1"<<endl;
    		cout<<"Escolhe um numero desde 1 até 1000."<<endl;
    		cin>>AdminVariable;
    		if (AdminVariable>1000 || AdminVariable<1) {
    			cout<<"Valor invalido, segue as regras."<<endl;
    		} else {
    			cout<<"Jogador 2"<<endl;
    			cout<<"Tenta advinhar o numero, desde 1 até 1000. Cuidado, tens "<<TryLeft<<" tentativas."<<endl;
    			cin>>Guess;
    			if (Guess<AdminVariable || Guess>AdminVariable) {
    				while ((Guess<AdminVariable || Guess>AdminVariable) && TryLeft>0) {
    					CurrentTry=CurrentTry+1;
    					TryLeft=TryLeft-1;
    					if (TryLeft>0) {
    						if (Guess<AdminVariable) {
    							cout<<"Falhaste, o numero é maior. Tens mais "<<TryLeft<<" tentativas."<<endl;
    							cin>>Guess;
    						} else {
    							cout<<"Falhaste, o numero é mais pequeno. Tens mais "<<TryLeft<<" tentativas."<<endl;
    							cin>>Guess;
    						}
    					} else {
    						cout<<"Perdeste! O numero correcto era: "<<AdminVariable<<endl;
    						cout<<"Queres repetir?? (Sim = 1, Não = 2)"<<endl;
    						cin>>Repeat;
    					}
    				}
    				if (TryLeft>0) {
    					cout<<"Ganhaste! Acertaste à "<<CurrentTry<<"ª!"<<endl;
    					cout<<"Queres repetir?? (Sim = 1, Não = 2)"<<endl;
    					cin>>Repeat;
    				}
    			} else {
    				if (TryLeft>0) {
    					cout<<"Acertaste à "<<CurrentTry<<"ª, impresionante!"<<endl;
    					cout<<"Queres repetir?? (Sim = 1, Não = 2)"<<endl;
    					cin>>Repeat;
    				}
    			}
    		}
    		break;
    	default:
            Language=2;
            cout<<"Language Not Supported!"<<endl;
    		cout<<"If you want play in English press yes(1) or no(2)."<<endl;
    		cin>>Repeat;
    		break;
    	}
    }
    	return 0;
    }
    Last edited by n3krO; 11-16-2011 at 06:55 PM.

  8. #8
    Senior Member n3krO's Avatar
    Join Date
    Jul 2010
    Location
    Málaga, Spain
    Posts
    3,904
    Ok i now have it for only 1 player:

    AdminVariable = rand() % 1000 + 1

  9. #9
    New Member
    Join Date
    Feb 2010
    Posts
    14
    Quote Originally Posted by sIMson View Post
    A few tips from me:
    • C++ is an object-oriented programming language, use classes if you know how to do it.
    .
    Yeah about that. I have been doing some lightweight programming for quite some time now, but I still can't quite get the grasp of OOP. I develop software the procedural way: Do this, then do that, then that etc, you know like a one way flow. Can't really understand the point of making objects every time. I mean I guess I kinda see the point of OOP in big software projects, but, for example, is there really a point to go OOP for n3krO simple program. How would that even go? Like what would be the object and what could be the classes? I'm just curious

  10. #10
    Member
    Join Date
    Feb 2010
    Location
    NY
    Posts
    420
    using classes and objects helps you re-use code.
    I put on my robe and wizard hat.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts