toupper help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <vector>

using namespace 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>

using namespace 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;
}
Last edited on
That makes sense, but still confused on how to use toupper's statement.

I tried
1
2
3
4
5
6
7
8
9

char d = answer[0];
d = (char)toupper(d);

char *d = &answer[0];
d = (char)toupper(d);

answer[0] = toupper(0);


But non of those worked either
Last edited on
Topic archived. No new replies allowed.