Switch Statements Using both Char AND Int values?
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
#include <algorithm>
using namespace std;
// Functions
int instructions();
int game();
//Types and Arrays
string word[] = {"GOLDFISH", "COMPUTER", "ANONYMOUS", "JACKET", "SHARP", "SERVICE", "EFFORT", "CHARACTER", "CHANGE", "WITHOUT", "PRODUCT", "UNFOLDS", "MUSIC", "MOMENT", "LIFETIME", "PROVIDE"};
char guess[50];
char inst[50];
int main()
{
cout << "\tWelcome to Word Jumble By Khoanyneosr!\n";
cout << "\nWould you like to see the instructions?"
<< "\n\t1) Yes = 1"
<< "\n\t2) No = 2"
<< "\n- ";
cin.getline(inst, 50);
bool done = false;
do
{
switch(inst[50])
{
case 1:
instructions();
done = true;
break;
case 2:
cout << "\n\tGood luck!\n\n";
system("pause");
game();
done = true;
break;
default:
cout << "\n\nInvalid input. Try again\n- ";
cin.clear();
cin >> inst;
done = false;
continue;
}
}while (!done);
return 0;
}
int instructions()
{
system("cls");
cout << endl << endl << endl << "\tINSTRUCTIONS" << endl;
cout << endl << "1 - You will be presented with a word. Your job is to guess it." << endl;
cout << endl << "2 - At the beginning of the game your given 10 points." << endl;
cout << endl << "3 - You will be given 1 hint. Everytime you have to revisit\nthe hint 1 point will be deducted." << endl;
cout << endl << "4 - For everytime you guess wrong 1 point will be deducted." << endl;
cout << endl << "5 - When your point value reaches 0, the game is over." << endl;
cout << endl << "You can exit at anytime by pressing Q." << endl;
cout << endl << endl << "\tGood Luck!" << endl << endl << endl;
system("pause");
return game();
}
int game()
{
char guess[15];
system("cls");
srand(time(0));
std::string s = word[ rand() % 16 ];
std::cout << "\nOriginal word: " << s << '\n';
std::random_shuffle(s.begin(), s.end());
std::cout << "\nScrambled word: " << s << '\n';
cout << endl << "What is your guess?\n- ";
cin.get(guess, 0);
cout << guess[15] << endl;
return 0;
}
That's my code... Is there anyway that in the first switch statement i could use both Char values and int values? if i wanted the user to enter either 1 or a 2 and they enter a z or an f or something, i would like it to prompt them again... but i can't figure it out... Thanks
Khoanyneosr
Junior Poster in Training
61 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Problem #1:
switch(inst[50]) -- this command only checks the value atinst[50], which is the byte one past the end of the array inst. What are you trying to test here?
Problem #2:
case 1: -- You entered characters, not integers, and thiscase is an integer value.
Is there anyway that in the first switch statement i could use both Char values and int values?
No. Since you don't have any integers, it doesn't matter.if i wanted the user to enter either 1 or a 2 and they enter a z or an f or something, i would like it to prompt them again... but i can't figure it out...
You've already got it set up properly in thedefault case.
Your understanding of integer vs character needs to be fixed. If you are reading a character string ising cin.getline(inst, 50); , and you press the keys2 5 1 enter, you did not enter the integer 251, you entered the characters '2', '5', and '1'.
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
I see this all the time, and I hope by me explaining it, will make it clear to you.
After creating an array like so: char buffer[10] = { 'a','b','c'};
when you use the operator[] ( [ # ] ) you are accessing a SINGLE ELEMENT. No matter how you try to twist the meaning of using the operator[] on a simple array, it always does the same thing.
This is accessing a SINGLE element of the array, and if I did this: std::cout << buffer[1]; it would access the second element of the array, the second character. It would print 'b'.
The operator[] is NOT magic.
Pointers may seem like magic, but they aren't either. In fact, when I learned to program and had the epiphany that nothing about the computer is magic I was also disappointed.
A C style string (sometimes called a C string) is an array of char's with the last character set to 0, or NULL, or '\0'. The program will process characters in a C string until the terminating NULL character is encountered, this avoids needing to specify the length/size of a string. Thus an algorithm can simply process characters until the character NULL is encountered. This is very important for large strings. Variable length strings are used. The string can occupy a part of, or all of (minus the NULL) a character array.
Pascal strings were limited by a value specifying the length, please see this wikipedia page for more information. http://en.wikipedia.org/wiki/String_(computer_science)#Representations
pseudorandom21
Practically a Posting Shark
890 posts since Jan 2011
Reputation Points: 216
Solved Threads: 111