Tolower function not working?

I'm stuck on this one piece of code.
1
2
3
4
5
6
7
8
9
10
11
12
13
void inputValidation(string &userInput)
{
    do
    {
        if(userInput!="paper" && userInput !="rock" && userInput!="scissors")
        {
            cout << "Sorry, but you must enter rock, paper, or scissors." << endl;
            cin >> userInput;
            tolower(userInput);
        }
    }
    while(userInput!="rock" && userInput!="paper" && userInput!="scissors");
}
Last edited on
Tolower can only convert a single character each time it is called. To make sure that you convert a string with tolower, use this :

1
2
for(int i = 0; i < userInput.length(); i++)
userInput[i] = tolower(userInput[i]);
By the way, you don't need to check the input twice. You are checking it as a while condition as well as an if-condition. Just have it checked in a while loop, no need for the if-clause. Duplicate code is frowned upon.
Last edited on
Does that help you? :)
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
//Ashton Dreiling
//Rock paper scissors exercise
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <stdlib.h>

using namespace std;
const int ONE_FOR_CAL=1;
const int THREE_FOR_CAL=3;
const int TWO_FOR_CAL=2;

void switchStatements(int number, string &rockPaperScissorsStatus, string rock, string paper, string scissors);
void playGameAgain (string userInput, string rockPaperScissorsStatus, string rock, string paper, string scissors, int number);
void ifThenStatements(string userInput, string rockPaperScissorsStatus);
void inputValidation (string &userInput);


int main()
{
    int number=0;
    string rockPaperScissorsStatus;
    string rock="rock";
    string paper="paper";
    string scissors="paper";
    string doAgain;
    string userInput;
    srand(time(0));


    cout << "Today, you will be playing rock, paper, scissors against a computer!" << endl;

    do
    {
        number=rand()% THREE_FOR_CAL + ONE_FOR_CAL;

        cout <<"Please enter rock, paper, or scissors." << endl;
        cin >> userInput;
        for(int i = 0; i < userInput.length(); i++)
            userInput[i] = tolower(userInput[i]);

        inputValidation(userInput);
        switchStatements(number, rockPaperScissorsStatus, rock, paper, scissors);
        ifThenStatements(userInput, rockPaperScissorsStatus);

        if(userInput==rockPaperScissorsStatus)
        {
            playGameAgain(userInput, rockPaperScissorsStatus, rock, paper, scissors, number);
        }

        cout << "Would you like to play again?" << endl;
        cin >> doAgain;
    }
    while (doAgain=="yes" || "YES" || "Yes");

    system("Pause");

    return 0;

}//end main

void switchStatements(int number, string &rockPaperScissorsStatus, string rock, string paper, string scissors)

{
    switch(number)

    {
    case ONE_FOR_CAL:
        cout << "The computer has chosen rock." << endl;
        rockPaperScissorsStatus="rock";
        break;

    case TWO_FOR_CAL:
        cout << "The computer has chosen paper." << endl;
        rockPaperScissorsStatus="paper";
        break;
    case THREE_FOR_CAL:
        cout << "The computer has chosen scissors." << endl;
        rockPaperScissorsStatus="scissors";
        break;
    }//end switch statements
}//end of switchStatements

void ifThenStatements(string userInput, string rockPaperScissorsStatus)
{
    if (userInput==rockPaperScissorsStatus)
    {
        cout << "It's a draw." << endl;
    }

    else if (userInput=="rock" && rockPaperScissorsStatus=="scissors")
    {
        cout << "You won! Rock smashes scissors." << endl;
    }

    else if (userInput=="rock" && rockPaperScissorsStatus=="paper")
    {
        cout << "You lose. Paper covers rock." << endl;
    }

    else if (userInput=="scissors" && rockPaperScissorsStatus=="paper")
    {
        cout << "You win. Scissors cuts paper." << endl;
    }

    else if (userInput=="scissors" && rockPaperScissorsStatus=="rock")
    {
        cout << "You lose. Rock crushes scissors." << endl;
    }

    else if (userInput=="paper" && rockPaperScissorsStatus=="rock")
    {
        cout << "You win. Paper covers rock." << endl;
    }

    else if (userInput=="paper" && rockPaperScissorsStatus=="scissors")
    {
        cout << "You lose. Scissors cuts paper." << endl;
    }



}//end ifThenStatements

void playGameAgain(string userInput, string rockPaperScissorsStatus, string rock, string paper, string scissors, int number)
{
    do
    {
        cout << "You must continue to play until it's no longer a draw." << endl;
        cout <<"Please enter rock, paper, or scissors." << endl;
        cin >> userInput;

        switchStatements(number, rockPaperScissorsStatus, rock, paper, scissors);
        ifThenStatements(userInput, rockPaperScissorsStatus);
    }
    while(userInput==rockPaperScissorsStatus);
}

void inputValidation(string &userInput)
{
    do
    {
        if(userInput!="paper" && userInput !="rock" && userInput!="scissors")
        {
            cout << "Sorry, but you must enter rock, paper, or scissors." << endl;
            cin >> userInput;
            for(int i = 0; i < userInput.length(); i++)
                userInput[i] = tolower(userInput[i]);
        }
    }
    while(userInput!="rock" && userInput!="paper" && userInput!="scissors");
}


This is my final code, but I'm not sure how I'd get rid of the if-then statement?
Last edited on
Can you tell us the piece of code you want to get rid of please?
She said the if statement and do while loop was duplicated code?
1
2
3
4
5
6
7
8
9
10
11
12
13
void inputValidation(string &userInput)
{
    do    {
        if(userInput!="paper" && userInput !="rock" && userInput!="scissors")
        {
            cout << "Sorry, but you must enter rock, paper, or scissors." << endl;
            cin >> userInput;
            for(int i = 0; i < userInput.length(); i++)
                userInput[i] = tolower(userInput[i]);
        }
    }
    while(userInput!="rock" && userInput!="paper" && userInput!="scissors");
}


That's what Arslan7041 said.

==>
1
2
3
4
5
6
7
8
9
10
11
12
}
void inputValidation(string &userInput)
{
        while(userInput!="paper" && userInput !="rock" && userInput!="scissors")
        {
            cout << "Sorry, but you must enter rock, paper, or scissors." << endl;
            cin >> userInput;
            for(int i = 0; i < userInput.length(); i++)
                userInput[i] = tolower(userInput[i]);
        }
    
}



P.S : You should pay more attention to what others said, FBHSIE
But I don't understand how I didn't? I just repeated what she told me about the duplicated code, I think?

I was confused about how exactly you'd implement that without an if statement or a do loop, but a while loop makes sense. I finished my 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
//Ashton Dreiling
//Rock paper scissors exercise
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <stdlib.h>

using namespace std;
const int ONE_FOR_CAL=1;
const int THREE_FOR_CAL=3;
const int TWO_FOR_CAL=2;

void switchStatements(int number, string &rockPaperScissorsStatus, string rock, string paper, string scissors);
void playGameAgain (string userInput, string rockPaperScissorsStatus, string rock, string paper, string scissors, int number);
void ifThenStatements(string userInput, string rockPaperScissorsStatus);
void inputValidation (string &userInput);


int main()
{
    int number=0;
    string rockPaperScissorsStatus;
    string rock="rock";
    string paper="paper";
    string scissors="paper";
    string doAgain;
    string userInput;
    srand(time(0));


    cout << "Today, you will be playing rock, paper, scissors against a computer!" << endl;

    do
    {
        number=rand()% THREE_FOR_CAL + ONE_FOR_CAL;

        cout <<"Please enter rock, paper, or scissors." << endl;
        cin >> userInput;
        for(int i = 0; i < userInput.length(); i++)
            userInput[i] = tolower(userInput[i]);

        inputValidation(userInput);
        switchStatements(number, rockPaperScissorsStatus, rock, paper, scissors);
        ifThenStatements(userInput, rockPaperScissorsStatus);

        if(userInput==rockPaperScissorsStatus)
        {
            playGameAgain(userInput, rockPaperScissorsStatus, rock, paper, scissors, number);
        }

        cout << "Would you like to play again?" << endl;
        cin >> doAgain;
    }
    while (doAgain=="yes" || "YES" || "Yes");

    system("Pause");

    return 0;

}//end main

void switchStatements(int number, string &rockPaperScissorsStatus, string rock, string paper, string scissors)

{
    switch(number)

    {
    case ONE_FOR_CAL:
        cout << "The computer has chosen rock." << endl;
        rockPaperScissorsStatus="rock";
        break;

    case TWO_FOR_CAL:
        cout << "The computer has chosen paper." << endl;
        rockPaperScissorsStatus="paper";
        break;
    case THREE_FOR_CAL:
        cout << "The computer has chosen scissors." << endl;
        rockPaperScissorsStatus="scissors";
        break;
    }//end switch statements
}//end of switchStatements

void ifThenStatements(string userInput, string rockPaperScissorsStatus)
{
    if (userInput==rockPaperScissorsStatus)
    {
        cout << "It's a draw." << endl;
    }

    else if (userInput=="rock" && rockPaperScissorsStatus=="scissors")
    {
        cout << "You won! Rock smashes scissors." << endl;
    }

    else if (userInput=="rock" && rockPaperScissorsStatus=="paper")
    {
        cout << "You lose. Paper covers rock." << endl;
    }

    else if (userInput=="scissors" && rockPaperScissorsStatus=="paper")
    {
        cout << "You win. Scissors cuts paper." << endl;
    }

    else if (userInput=="scissors" && rockPaperScissorsStatus=="rock")
    {
        cout << "You lose. Rock crushes scissors." << endl;
    }

    else if (userInput=="paper" && rockPaperScissorsStatus=="rock")
    {
        cout << "You win. Paper covers rock." << endl;
    }

    else if (userInput=="paper" && rockPaperScissorsStatus=="scissors")
    {
        cout << "You lose. Scissors cuts paper." << endl;
    }



}//end ifThenStatements

void playGameAgain(string userInput, string rockPaperScissorsStatus, string rock, string paper, string scissors, int number)
{
    do
    {
        cout << "You must continue to play until it's no longer a draw." << endl;
        cout <<"Please enter rock, paper, or scissors." << endl;
        cin >> userInput;

        switchStatements(number, rockPaperScissorsStatus, rock, paper, scissors);
        ifThenStatements(userInput, rockPaperScissorsStatus);
    }
    while(userInput==rockPaperScissorsStatus);
}

void inputValidation(string &userInput)
{

    while(userInput!="paper" && userInput !="rock" && userInput!="scissors")
    {
        cout << "Sorry, but you must enter rock, paper, or scissors." << endl;
        cin >> userInput;
        for(int i = 0; i < userInput.length(); i++)
            userInput[i] = tolower(userInput[i]);
    }

}

Last edited on
So if someone helps you you don't bother say thanks? O.o
Of course I do!

Well, most of the time if I'm being honest, but I didn't know you were talking about.

Yes, I'm thankful lol.
Glad it helped :)
I had to fix my do while loop, but I'm running into a logic error I'm not sure how to fix.

It keeps doing this. The computer keeps choosing rock if I continue to put in rock (though it generally randomizes).



http://prntscr.com/bsehpz
Hi,
I haven't found the error yet, but look at how you initialize variables in inputValidation() :

1
2
3
string rock="rock";
string paper="paper";
string scissors="paper"; // shouldn't it be "scissors" instead??? 
Also, change the topic title so that it matches your new problem. So that we can continue to help you.
I'm actually solving this in another thread which is why I checked it was solved. I didn't want to get in trouble for duplicating. It's in the loop thread I made.

It started off with ideas for loops, and as I figured it out, it ended with the same problems.

Thanks for pointing that out. Sorry I'm making some silly mistakes today. I'm rushing since I have three more programs to do.
Topic archived. No new replies allowed.