#include <iostream>
#include <vector>
#include <algorithm>
#include<ctime>
#include<cstdlib>
#include<string>
using namespace std;
//characters required #, @, %, ?, !, ^, $, &, *, (, )
int main()
{
int choice;
char again = 'y';
while (again == 'y')
{
srand(time(0));//seeding the random number generator
const int MAX_WRONG = 10; // maximum number of incorrect guesses allowed
vector<string>::iterator iter;
vector<string> lvl1;
vector<string> lvl2;
vector<string> lvl3;
vector<string> lvl4;
vector<string> lvl5;
vector<string> lvl6;
vector<string> lvl7;
vector<string> lvl8;
int choice;
string input;
//difficulty 1 3char
lvl1.push_back("!!?");
lvl1.push_back("@?!");
lvl1.push_back("?%#");
lvl1.push_back("!!!"); //use this to test in the program
lvl1.push_back("#@%");
lvl1.push_back("$))");
//difficulty 2 4 char
lvl2.push_back("#@%!");
lvl2.push_back("?@%#");
lvl2.push_back("!!!!"); //use this to test in the program
//difficulty 3 5char
lvl3.push_back("#@%%%");
lvl3.push_back("#@#?%");
lvl3.push_back("!!!!!"); //use this to test in the program
lvl3.push_back("^^/>&@)");
//Difficulty 4 6char
lvl4.push_back("??@@$?");
lvl4.push_back("!!!!!!"); //use this to test in the program
lvl4.push_back("*@*#$)");
//Difficulty 5 7char
lvl5.push_back("#@@?@?#");
lvl5.push_back("!!!!!!!"); //use this to test in the program
lvl5.push_back("??@^$#%");
//Difficulty 6 8char
lvl6.push_back("?!#%#@@^");
lvl6.push_back("!!!!!!!!"); //use this to test in the program
lvl6.push_back("#!@?%?#!");
lvl6.push_back("#!@?##?@");
//diffiulty 7 9char
lvl7.push_back(")!(#^%$*@");
lvl7.push_back("!!!!!!!!!"); //use this to test in the program
lvl7.push_back("*#*$)!^#(");
//difficulty 8 10char
lvl8.push_back("&%&&@()@$%");
lvl8.push_back("!!!!!!!!!!");//use this to test in the program
lvl8.push_back("&%(!%^@*)@");
//the problem with this is that it does not check the code for correct guesses in the group of characters suppose to be choose a group of symbols from the selected vector and check which character guessed are coorect
srand(time(0));
random_shuffle(lvl1.begin(), lvl1.end());
const string THE_SYMBOL = lvl1[0]; // symbols to guess
int wrong = 0; // number of incorrect guesses
string soFar(THE_SYMBOL.size(), '-'); // symbols guessed so far
string used = ""; // symbols already guessed
cout << "Try to guess the symbols useing # @ % ? ! ^ $ & * ( )\n";
while(true)
{
system("CLS");
cout << "Enter the appropriate menu option" << endl;
cout << "1. Difficulty 1" << endl;
cout << "2. Difficulty 2" << endl;
cout << "3. Difficulty 3" << endl;
cout << "4. Difficulty 4" << endl;
cout << "5. Difficulty 5" << endl;
cout << "6. Difficulty 6" << endl;
cout << "7. Difficulty 7" << endl;
cout << "8. Difficulty 8" << endl;
cout << "9. quit" << endl;
cin >> choice;
system("CLS");
switch(choice)
{
case 1:
cout << "Theres a total of 3 characters" << endl;
cin >> input; //problem if i take this out the case will not work and i have to guess for a correct answer to continue before it says start guessing
iter = find(lvl1.begin(), lvl1.end(), input);
if(iter == lvl1.end())
cout << "You are incorrect" << endl;
else
{
cout << " start guessing" << endl;
cin >> input;
*iter = input;
cout << "\You are correct" << endl;
}
break;
case 2:
cout << "Theres a total of 4 characters" << endl;
cin >> input;
iter = find(lvl2.begin(), lvl2.end(), input);
if(iter == lvl2.end())
cout << "You are incorrect" << endl;
else
{
cout << " start guessing" << endl;
cin >> input;
*iter = input;
cout << "\You are correct" << endl;
}
break;
case 3:
cout << "Theres a total of 5 characters" << endl;
cin >> input;
iter = find(lvl3.begin(), lvl3.end(), input);
if(iter == lvl3.end())
cout << "You are incorrect" << endl;
else
{
cout << " start guessing" << endl;
cin >> input;
*iter = input;
cout << "\You are correct" << endl;
}
break;
case 4:
cout << "Theres a total of 6 characters" << endl;
cin >> input;
iter = find(lvl4.begin(), lvl4.end(), input);
if(iter == lvl4.end())
cout << "You are incorrect" << endl;
else
{
cout << " start guessing" << endl;
cin >> input;
*iter = input;
cout << "\You are correct" << endl;
}
break;
case 5:
cout << "Theres a total of 7 characters" << endl;
cin >> input;
iter = find(lvl5.begin(), lvl5.end(), input);
if(iter == lvl5.end())
cout << "You are incorrect" << endl;
else
{
cout << " start guessing" << endl;
cin >> input;
*iter = input;
cout << "\You are correct" << endl;
}
break;
case 6:
cout << "Theres a total of 8 characters" << endl;
cin >> input;
iter = find(lvl6.begin(), lvl6.end(), input);
if(iter == lvl6.end())
cout << "You are incorrect" << endl;
else
{
cout << " start guessing" << endl;
cin >> input;
*iter = input;
cout << "\You are correct" << endl;
}
case 7:
cout << "Theres a total of 9 characters" << endl;
cin >> input;
iter = find(lvl7.begin(), lvl7.end(), input);
if(iter == lvl7.end())
cout << "You are incorrect" << endl;
else
{
cout << " start guessing" << endl;
cin >> input;
*iter = input;
cout << "\You are correct" << endl;
}
break;
case 8:
cout << "Theres a total of 10 characters" << endl;
cin >> input;
iter = find(lvl8.begin(), lvl8.end(), input);
if(iter == lvl8.end())
cout << "You are incorrect" << endl;
else
{
cout << " start guessing" << endl;
cin >> input;
*iter = input;
cout << "\You are correct" << endl;
}
break;
case 9:
return 0;
default:
cout << "incorrect input" << endl;
}
}
cout << "\nSure theres better games outt here anyways.";
cout << endl;
cin.ignore();
cin.get();
}
}
Vector strings, specific loopsproblems looping through a case : andchecking characters
Page 1 of 1
4 Replies - 872 Views - Last Post: 15 October 2010 - 07:56 PM
#1
Vector strings, specific loops
Posted 15 October 2010 - 10:50 AM
I have this game that im making that is based off of the old logic game. in my code i have several vector strings for levels of difficulty. And in my switch area each one of my cases have a problem with looping. I want it it to loop the case if the quess for characters are incorrect. the last thing is that if you look at line 113 the cin >> input; i want to take that out so it will out put the total of characters required to guess and then you are told to start guessing. But if i take out line 113 the entire case will not work. please help. I have alot of comments in this so it should help you find specific problems that im having
Replies To: Vector strings, specific loops
#2
Re: Vector strings, specific loops
Posted 15 October 2010 - 11:05 AM
What's your question?
I have two.
1. Why complicate your question with 8 levels, when one level doesn't work.
If you're still debugging the logic, don't complicate it with extra levels, which all do the same basic thing.
Once you HAVE debugged it, then paramerise it so that you can make it a function that will deal with ANY length of coded string.
2. Why not use vector<vector< string> > levels rather than 8 names variables.
Then your monster switch/case reduces to a simple levels[difficulty] subscript.
I have two.
1. Why complicate your question with 8 levels, when one level doesn't work.
If you're still debugging the logic, don't complicate it with extra levels, which all do the same basic thing.
Once you HAVE debugged it, then paramerise it so that you can make it a function that will deal with ANY length of coded string.
2. Why not use vector<vector< string> > levels rather than 8 names variables.
Then your monster switch/case reduces to a simple levels[difficulty] subscript.
#3
Re: Vector strings, specific loops
Posted 15 October 2010 - 11:14 AM
Salem_c, on 15 October 2010 - 10:05 AM, said:
What's your question?
I have two.
1. Why complicate your question with 8 levels, when one level doesn't work.
If you're still debugging the logic, don't complicate it with extra levels, which all do the same basic thing.
Once you HAVE debugged it, then paramerise it so that you can make it a function that will deal with ANY length of coded string.
2. Why not use vector<vector< string> > levels rather than 8 names variables.
Then your monster switch/case reduces to a simple levels[difficulty] subscript.
I have two.
1. Why complicate your question with 8 levels, when one level doesn't work.
If you're still debugging the logic, don't complicate it with extra levels, which all do the same basic thing.
Once you HAVE debugged it, then paramerise it so that you can make it a function that will deal with ANY length of coded string.
2. Why not use vector<vector< string> > levels rather than 8 names variables.
Then your monster switch/case reduces to a simple levels[difficulty] subscript.
First each level is a difficulty level. the code techically works cause the debugger in Visual studio doesnt come up with any errors. And as for the functions i dont know how to create a function. Ive done it this way cause this is the only way i know how to do this. Im teaching myself c++ and this is mostly what I know how to do so far. if you can tell me how to create a funtion that would be awesome or at least the basic setup for a function.
so lets just go with the biggest question first which seems the most important now from your tips. how do i set up a function for something like this?
#4
Re: Vector strings, specific loops
Posted 15 October 2010 - 07:33 PM
blaxarbush, on 16 October 2010 - 03:14 AM, said:
how do i set up a function for something like this?
A good place to start
http://www.cplusplus...rial/functions/
#5
Re: Vector strings, specific loops
Posted 15 October 2010 - 07:56 PM
If you're self-learning and using pointers, vectors and vector iterators, stuff from <algorithm>, etc. but don't know how to use functions then you may want to take a step back and start with something more basic. Here's a link to get you started:
CPlusPlus Functions (1 out of 2) (janotte beat me to it!)
The great thing about functions is they're reusable. So it's very easy to condense your code and, especially since you have so many levels with near-identical setups, you only have to code it once (if you do it right). It might not seem too horrible with 8 levels - but imagine if you had 100 levels? If you did it without any functions, your code will be very large and very annoying to update. (I see something along the lines of "Don't code it more than once" floating around a lot).
More specifically about your code:
1. Everything Salem_c said.
2. Don't get fancy.
CPlusPlus Functions (1 out of 2) (janotte beat me to it!)
The great thing about functions is they're reusable. So it's very easy to condense your code and, especially since you have so many levels with near-identical setups, you only have to code it once (if you do it right). It might not seem too horrible with 8 levels - but imagine if you had 100 levels? If you did it without any functions, your code will be very large and very annoying to update. (I see something along the lines of "Don't code it more than once" floating around a lot).
More specifically about your code:
1. Everything Salem_c said.
2. Don't get fancy.
//...
case 1:
cout << "Theres a total of 3 characters" << endl;
cin >> input; //problem if i take this out the case will not work and i have to guess for a correct answer to continue before it says start guessing
iter = find(lvl1.begin(), lvl1.end(), input);
if(iter == lvl1.end())
//...
To begin, you're asking for an input to be used to define your iterator iter which is later what you use to see if the guess is correct. Then comparing the actual iterator to a value stored at lvl1.end(). Odd, and definitely not working.
//...
//This is just to give you a general idea of how it -might- look, won't work if you just copy and paste, so try and apply the concepts and not just the lines:
cout << "Theres a total of 3 characters" << endl;
cout << "Start guessing: " << endl;
cin >> input;
while ((input != correctAnswer) || (wrong == MAX_WRONG))
{
cout << "You are incorrect - try again:";
cin >> input;
wrong++;
}
if (input == correctAnswer)
{
cout << "Congratulations - you guessed correct!" << endl;
}
else
{
cout << "Sorry, the correct answer was " << correctAnswer;
cout << ". Better luck next time!" << endl;
}
All in all, this program could be a lot simpler. Stick to the basics - using too many bells and whistles will get you lost (at least that's what happens to me)
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote






|