#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <vector>
usingnamespace std;
int main()
{
vector<string> Words;
Words.push_back("Hard");
Words.push_back("String");
Words.push_back("Car");
vector<string>Hints;
Hints.push_back("A rock is...\n");
Hints.push_back("Used for words in C Programming.\n");
Hints.push_back("Drive a in a...\n");
srand(time(0));
int choice = rand()%Words.size();
string theWord = Words[choice];
string theHint = Hints[choice];
string jumble = theWord;
int length = jumble.size();
for(int i=0; i < length; i++)
{
int z = rand()%length;
int y = rand()%length;
char temp = jumble[z];
jumble [z] = jumble[y];
jumble[y] = temp;
}
cout << "\n\t\tWelcome to Jumble!\n\n";
cout << "Enter 'hint' for the hint or 'quit' to quit.\n\n";
cout << "The jumble is " << jumble;
string answer;
cout << "\nEnter your guess: ";
cin >> answer;
answer = (char)toupper(0);
while(answer != theWord && answer!= "Quit")
{
if(answer == "Hint")
{
cout << theHint << endl;
}
else
{
int r = rand()%3;
switch(r)
{
case 0:
cout << "Nope thats not it\n";
break;
case 1:
cout << "Try again\n";
break;
case 2:
cout << "Thats incorrect\n";
break;
default:
cout << "Sorry, thats the wrong answer\n";
}
}
cout << "\nEnter your guess: ";
cin >> answer;
}
if(answer == theWord)
{
cout << "Congrats! You have guessed the correct word '" << theWord << "'!" << endl;
}
cout << "\nThanks for playing!\n\n";
}
I dont get how I keep messing up toupper. I want to understand it but jesus, everytime I use it, it never works. In this code I guess I did something wrong with it. answer = (char)toupper(0);. Ive asked for help on toupper on another forum of mine, but I guess I wont be able to understand it.
Isnt toupper suppose to be... theVariable=(type)toupper(letter_you_want_to_change)? If not then what did I do wrong?
A string is basically a char array. You want to take the first element of the array and check if it's a lower case letter. If it is, make it into an upper case letter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
int main()
{
string word;
cout << "Enter a word (First letter should be lower case) ";
getline(cin, word);
cout << "Word before check: " << word << endl;
if (islower(word[0]))
word[0] = toupper(word[0]);
cout << "Word after check: " << word << endl;
}