cin to uppercase

I know that this has probably already been asked. I just can't find the answer and that is why I am seeking help. Or it may be brain freeze because I know it is probably something simple I am overlooking. So, I stress this is noobish.

I can not seem to find how to get cin user input converted to uppercase. It has to be uppercase to match the conditions of the variable it is to be compared to.

My code is below.

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

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include<ctype.h>

using namespace std;

int main()
{
    string test1;
    test1 = "FOO";

    string m;

    cout << "Below is the test I am trying." << endl;

    cin >> m;

    if(test1 == m)
    {
        cout << "you are correct" << endl;
    }
    else
    {
        cout << "Sorry that answer is incorrect." << endl;
    }

    getch();
    return 0;
}
I'm not completely sure, but I think if you use single quotes around FOO it takes it literally.
It's worth a shot.
Thanks for the reply...

I don't get what you mean by literally?

I did give it a shot and it did not work.

To explain a little bit more. I am trying to convert user input however many characters they input to uppercase, then run it though the if statement.

I have tried a few touppercase type codes and just can't seem to get them to work.

It don't seem like it would be that difficult, maybe I am making it harder than it really is.

any ideas??
One way is to convert the string to upper case before you do the comparison, by looping over all characters in the string and use the std::toupper function to convert each character to upper case.
http://en.cppreference.com/w/cpp/string/byte/toupper
Last edited on
1
2
3
4
5
6
#include <cctype>

....

    cin >> m;
    for ( auto &c : m ) c = toupper( c );

Or if the for statement based on range is not supported then

1
2
3
4
5
6
7
#include <cctype>
#include <algorithm>

...

    cin >> m;
    std::transform( m.begin(), m.end(), m.begin(), []( char c ) { return ( toupper( c ) ); } );
Last edited on
I'd tried looping it and had not got good results. I did however look back into it with your suggestion and I want to say thank you so very much.

I wanted to stay away from the std:: as much as possible. I did however get it to work.

I found something below, and the 1st example was the charm:

http://www.cplusplus.com/forum/beginner/19748/

Here is what I came up with.

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


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include<ctype.h>

using namespace std;

int main()
{
    string test1;
    test1 = "FOO";

    string m;

    cout << "Below is the test I am trying." << endl;

    cin >> m;

    /*
    for (int i = 0; i < restart.length(); i++) {
    restart[i] = toupper(restart[i]);
    }
    */

    for (int i = 0; i < m.length(); i++) {
    m[i] = toupper(m[i]);
    }

    cout << m << endl;  // To see if it looped right

    if(test1 == m)
    {
        cout << "you are correct" << endl;
    }
    else
    {
        cout << "Sorry that answer is incorrect." << endl;
    }

    getch();
    return 0;
}





So, on to the next step.... and hopefully I won't hit anymore snags.

Thanks
Last edited on
Hi vlad

I wanted to try your solution to see what happened. In my noobish way I will say
that I don't think I am set up for <cctype.h> I thought it was because I mispelled it above. But I am getting No such file or directory???

I liked the looks of your first example, but I got an error
" Warning 'auto' will change meanining in C++0x; please remove it"
Do you know what that is about?

Wouldn't you know that I would run into another snag right away. In the process of taking the code above further I came to another question.

as in......


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

    string m;
    string n;
    string o;

    cin >> m >> n >> o;

    for (int i = 0; i < m.length(); i++)
    {
    m[i] = toupper(m[i]);
    }
    for (int i = 0; i < n.length(); i++)
    {
    n[i] = toupper(n[i]);
    }
    for (int i = 0; i < o.length(); i++)
    {
    o[i] = toupper(o[i]);
    }


How to loop through more than one string. Can it be done in the same loop?
Because the code above runs, but I am looking for a way to combine them.

I saw something about nesting them but got really mixed up.

Loops don't really float my boat but understanding the big part they play in C++ I am trying to get my head around them.

I am told that strings are arrays of chars and I am guessing that is the reason for the for loops used in arrays???
Last edited on
closed account (3qX21hU5)
Haven't read the previous posts, but for your question
How to loop through more than one string
I would just store each string in a vector or array something like this.

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
// Takes unlimited Strings from a user and converts all them to upcase

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    string input = "";
    vector<string> userStrings;

    // Enter unlimited strings and store them into a vector
    while (true)
    {
        cout << "Enter a string (Type \"exit\" to exit the loop): ";
        cin >> input;

        if (input == "exit")
        {
            break;
        }

        userStrings.push_back(input);
    }

    // Changes all strings in the vector to uppercase
    for (vector<string>::size_type n = 0; n != userStrings.size(); ++n)
        for (string::size_type i = 0; i < userStrings[n].length(); ++i)
            userStrings[n][i] = toupper(userStrings[n][i]);

    // Prints the vector's contents
    for (vector<string>::size_type index = 0; index != userStrings.size(); ++index)
        cout << userStrings[index] << endl;

    return 0;
}


Now you can ignore all the size_type and use int's if you want since they are easier for just beginning but other then that it should be pretty simple. I used vectors since they are better then array's but if you haven't learned them yet arrays will work also. Let me know if you have any questions and hope this helps a bit.
Last edited on
Hi Zereo,

I have played with arrays but not with vectors before(heard of them though). But, as they say no better time then the present.

From what you are saying I may be able to do other things with them too. So, I am about to try it out. Hope it won't be to painful.

Will update you in a little bit.
There is a lot that I am seeing about vectors out there. The fact that they are
dynamic makes me think they might be better then arrays. I am not seeing to many involving strings though.

Very confused as to how to get the strings to work with this. I tried substituting input and it got stuck in the loop and would not exit the window.

Something else I tried and thought it worked but then it did not do something and the result came out wrong.

I am really wanting to see how it would work with the vectors, but at a loss as to how to get the multiple string input to work with the vectors

I have been working on coding all night without sleep so it all may be running together on me. Really confused now.

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


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <vector>

using namespace std;

int main()
{

    string test1;
    string test2;
    string test3;

    test1 = "FOO";
    test2 = "BAR";
    test3 = "AR";

    cout << "Below is the test I am trying." << endl;
    string m;
    string n;
    string o;

    cin >> m >> n >> o;

    string input = "";
    vector<string> userStrings;

    // Enter unlimited strings and store them into a vector
     while (true)
    {
        cout << "Enter a string (Type \"exit\" to exit the loop): ";
        cin >> input;

        if (input == "exit")
        {
            break;
        }

        userStrings.push_back(input);
    }

    // Changes all strings in the vector to uppercase
    for (vector<string>::size_type n = 0; n != userStrings.size(); ++n)
        for (string::size_type i = 0; i < userStrings[n].length(); ++i)
            userStrings[n][i] = toupper(userStrings[n][i]);

    // Prints the vector's contents
    for (vector<string>::size_type index = 0; index != userStrings.size(); ++index)
        cout << userStrings[index] << endl;




/**
    for (int i = 0; i < m.length(); i++)    
    {                           
    m[i] = toupper(m[i]);       
    }                           
    for (int i = 0; i < n.length(); i++)
    {
    n[i] = toupper(n[i]);
    }
    for (int i = 0; i < o.length(); i++)
    {
    o[i] = toupper(o[i]);
    }

*/
 if((test1 == m) && (test2 == n) && (test3 == o)) /// if for all 3 conditions
    {
        cout << "You are correct!!" << endl;
    }
    else
    {
        cout << "You are wrong!!" << endl;
    }

    getch();
    return 0;
}



Just knowing this is going to be a monster because it is growing and not even seperated class and souce files yet..... help?

gonna take a break and come back to it later today....
Last edited on
closed account (3qX21hU5)
Well first nice copy and pasting of my code example ;p, but I'll ignore that since you seem like you are trying to learn and do the work. Here is some things you need to do to solve your problems

1) Delete all your old variables and the cin statement. You no longer need to use these since the while loop is taking care of the storage of the strings and the user input now.

2) Now I am not sure what you need to test for the last step? It looks like you are testing to make sure all 3 words that the user entered matches certain words.

A) Since you only need to work with 3 words rework the while loop at the top so that it stops after the user enters 3 words. This will make it easier to do your tests since you are working with a fixed number of strings.

B) if((test1 == m) && (test2 == n) && (test3 == o)) You are going to have to change this so that it tests to see if the words in elements 0, 1, 2 of your vector make their respective words (Hint: Remember with the subscript operator [] you can access whatever string is in a element of a vector).

That is the 2 things that I see wrong with it so far. Also with vectors you can basically think of the as arrays that are dynamic. They function just about the same as arrays do.

No harm meant at all. I am in awe of programmers that take the time to help those less experienced. I am self taught on the programming I do know, so I thank you for your time.

Vectors are as new a concept to me as when you suggested it. I am liking the concept and tried to work it with my code and was running into walls.

Now, I have a fresh brain after a nap and excited about working it out.
closed account (3qX21hU5)
Ya it always helps me to when I take a break for a few hours, and then have a fresh look at my code. Anyways if you run into any more walls just come post a topic here and we will be glad to help.
I was really wreaking havoc with the first for loop.

Suddenly, an ahh hahh moment. Went back to your original loop. Thinking back to what you said about thinking of vectors as an array, and here is what I came up with....

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
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <vector>

using namespace std;

int main()
{

    string test1 = "FOO";
    string test2 = "BAR";
    string test3 = "AR";

    cout << "Below is the test I am trying." << endl;

    string input = "";
    vector<string> userStrings;

    // Enter unlimited strings and store them into a vector
     while (true)
    {
        cout << "Enter a string (Type \"exit\" to exit the loop): ";
        cin >> input;

        if (input == "exit")
        {
            break;
        }

        userStrings.push_back(input);
    }

    // Changes all strings in the vector to uppercase
    for (vector<string>::size_type n = 0; n != userStrings.size(); ++n)
            for (string::size_type i = 0; i < userStrings[n].length(); ++i)
            userStrings[n][i] = toupper(userStrings[n][i]);

    // Prints the vector's contents
    for (vector<string>::size_type index = 0; index != userStrings.size(); ++index)
        cout << userStrings[index] << endl;

 if((userStrings[0] == test1) && (userStrings[1] == test2) && (userStrings[2] == test3))
    {
        cout << "You are correct!!" << endl;
    }
    else
    {
        cout << "You are wrong!!" << endl;
    }

    getch();
    return 0;
}


Your statement below is right. I read that vectors can be about templating.

I am thinking that if it is that maybe I could use the vector here as a template as well. So if this code can store unlimited strings, then I can use it for other test with the same format right, but not the same amount of word the user input??? It seems pretty flexible.

If so that could be pretty powerful and just what I need.


2) Now I am not sure what you need to test for the last step? It looks like you are testing to make sure all 3 words that the user entered matches certain words.
Last edited on
Topic archived. No new replies allowed.