Convert string to uppercase?

So I'm making a really simple rock paper scissors game in c++. I used to be able to convert my char variable to uppercase, but it did not allow me to add spaces to my username. I converted it to string and I have been searching back and forth how to change the string to uppercase as my one does not work :S - hopefully some of you can help...thanks!

code:
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160

#include <iostream>
#include <cstring>
#include <ctime>
#include <cmath>
#include <cstdlib>
#include <fstream>

using namespace std;
int rand_0toN1(int n);
void upcase(string name); //calling upcase function
int main()
{
    int x;
    int n;
    int i;
    int r;
    int pc_score = 0;
    int user_score = 0;
    string name; //convert to uppercase
    int text = 0;
    int text2 = 0;

cout << "Enter your name:" << endl;
getline(cin, name);
upcase(name); //using the upcase function


while (1) {
    while(1) {
    while (1) {
    cout << endl;
    cout << "Please choose your class:" << endl;
    cout << "(1) Rock" << endl;
    cout << "(2) paper" << endl;
    cout << "(3) Scissor" << endl;
    cout << "(4) Save Scores" << endl;
    cout << "(5) Load Scores" << endl;
    cout << "(0) to exit" << endl;
    cout << endl;
    cin >> x;

    if (x == 0) {
    exit(EXIT_SUCCESS);
    }
        if (x > 5) {
        cout << endl;
        cout << "Please pick a number from one to three" << endl;
        cout << endl;
        } else {
        break;
        }
    }
    if (x == 4) { //just loading scores
    ofstream file;
    ofstream load1;
    ofstream load2;
    file.open("d:\\scores.txt");
    file << name << ": " << user_score << endl;
    file << "COMPUTER: " << pc_score << endl;
    file.close();
    cout << endl;
    cout << "File was successfully saved - if saved again, file will be overwritten" << endl;
    cout << endl;
    load1.open("c:\\scoresuser.txt");
    load1 << user_score;
    load2.open("c:\\scorespc.txt");
    load2 << pc_score;
    } else if (x == 5) {
    ifstream getscorepc ("c:\\scorespc.txt");
    if (getscorepc.is_open()) {
        while (!getscorepc.eof()) {
        int tempString;
        getscorepc >> tempString;
        text += tempString;
        }
        pc_score = text;
    }
    ifstream getscoreuser ("c:\\scoresuser.txt");
if (getscoreuser.is_open()) {
while (!getscoreuser.eof()) {
    int tempString2;
    getscoreuser >> tempString2;
    text2 += tempString2;
}
user_score = text2;
cout << endl;
cout << "Load successful." << endl;
cout << endl;
}
    }
     else {
    break;
    }
    }

n = 1;
for (i=1; i <=n; i++) {
r = rand_0toN1(3) + 1;
}
if (r == 1){
    cout << endl;
    cout << "The computer chooses rock" << endl;
    cout << endl;
} else if (r == 2) {
cout << endl;
cout << "The computer chooses paper" << endl;
cout << endl;
} else if (r == 3) {
cout << endl;
cout << "The computer chooses scissors" << endl;
cout << endl;
}

//here, the scores do not seem to show up as uppercase (the var is name)
if (r == x) {
    cout << "__________________________________________" << endl;
    cout << endl;
    cout << "It is a draw!" << endl;
    cout << endl;
    cout << "__________________________________________" << endl;
    cout << name << ": " << user_score << "            Computer: " << pc_score << endl;
cout << endl;
}
else if (r == 1 && x == 2 || r == 2 && x == 3 || r == 3 && x == 1){
    cout << "__________________________________________" << endl;
    cout << endl;
    cout << "You win!" << endl;
    cout << endl;
    cout << "__________________________________________" << endl;
    user_score++;
    cout << name << ": " << user_score << "            Computer: " << pc_score << endl;
    cout << endl;
}
else {
    cout << "__________________________________________" << endl;
    cout << endl;
    cout << "You loose!" << endl;
    cout << endl;
    cout << "__________________________________________" << endl;
    pc_score++;
    cout << name << ": " << user_score << "            Computer: " << pc_score << endl;
    cout << endl;
}
}
cout << endl;
return 0;
}

int rand_0toN1(int n) {
return rand() % n;
}
void upcase(string name){ //defining the function
int length;
length = name.size(); //making length = to the length of the string (works fine)
int p;
for(p = 0; p < length; p++) {
    name[p] = toupper(name[p]); //this SHOULD convert string name; to uppercase but it doesnt
}
}


Just as a reminder, I still am a semi-beginner so I imagine a lot of the code could be optimised
This works:
1
2
3
4
void upcase(string *name) {
	for(int i=0; i<name->size(); ++i)
		if((*name)[i]>96 && (*name)[i]<123) (*name)[i]-=32;
}
You want the user name to be uppercase?
If so you have to pass the string name by reference.

 
void upcase(string & name); //calling upcase function 


1
2
3
4
5
6
7
void upcase(string & name){ //defining the function
int length;
length = name.size(); //making length = to the length of the string (works fine)
int p;
for(p = 0; p < length; p++) {
    name[p] = toupper(name[p]); //this SHOULD convert string name; to uppercase but it doesnt
}
Last edited on
Script Coder:
For me it says (I'm using codeblocks if that helps in any way)
undefined reference to std::string name
this is with the upcase(name); line with your code :S
and if I put upcase(string name) it says expected primary-expression before name
Last edited on
@Script Coder: ┬┐why the obfuscation?

@khal: void upcase(string & name); that is not the `calling', just the prototype.

@OP: the prototype and definition must match.
If you are planning to use Script Coder's version, then the calling should be upcase( &name ); as the function ask for a pointer
@ne555 I did not mean to obfuscate my code, that is how I would have done it. how should I have done it?

@OP try including the string header
Thank you so much for your replies :), it worked @Script Coder and @ne555
@Script Coder:
You should simply do (*name)[K] = toupper( (*name)[K] ); (as in the original post)

If for some reason you did not want to use that function, then more clearly
1
2
if( (*name)[K]>='a' and (*name)[K]<='z' )
   (*name)[K] = (*name)[K]-'a'+'A';
Topic archived. No new replies allowed.