I am trying to figure out what is causing my function to output letter by letter on separate lines after converting from a lowercase string to all uppercase, in an effort. My assignment is asking us to write a program that asks the user for a state's capital and then reports if the user was correct. It keeps a tally of the number of correct answers and gives it at the end.
It specifically states that the user's answers are not case sensitive, which is giving me the most trouble.
So far, I have tried using a character array and then converting that, but that became very confusing to try and convert/compare the multidimensional string array of states and capitals with the user's potential guesses.
Now, however, it has started saying that any input is incorrect unless it is in all caps. It has also started printing letter by letter, all caps, on different lines, so at least we know the loop is working.
My intention was to take input that was not in caps, convert it, and compare that converted string to the correct answer, so that both strings being compared were in all caps.
To spare y'all, I've cut it down to four states, and made sure to include a state with a space in the string answer.
1 #include <iostream>
2 #include <string>
3 using namespace std;
4 string convertCase(string entryToConvert){
5 string convertedString;
6 int i=0;
7 while (i <= size(entryToConvert)){
8 convertedString = toupper(entryToConvert[i]);
9 i++;
10 cout << convertedString << endl;
11 }
12 return convertedString;
13 }
14
15 int main() {
16 string statesAndCaps[4][2] = {
17 {"Alabama", "MONTGOMERY"}, {"Alaska", "JUNEAU"},
18 {"Arizona", "PHOENIX"}, {"Arkansas", "LITTLE ROCK"},
19 };
20 int correctCounter = 0, statesLoop = 0;
21 string guess, convertedString;
22 while (statesLoop != 4) {
23 cout << "Enter the capital of " << statesAndCaps[statesLoop][0] << " and press ENTER: ";
24 getline(cin, guess);
25 convertedString = convertCase(guess);
26 cout << convertedString;
27 if (convertedString == statesAndCaps[statesLoop][1]){
28 cout << "Correct. Total correct guesses: " << correctCounter << endl;
29 correctCounter++;
30 } else {
31 cout << "Incorrect. Total correct guesses: " << correctCounter << endl;
32 }
33 statesLoop++;
34 }
35 cout <<"You got " << correctCounter << " correct!" << endl;
36 return 0;
37 }
I have specifically coded in some outputs to try and check on what exactly was going awry. These are cout << convertedString << endl;
and cout << convertedString;
in lines 10 and 26. It produced:
Enter the capital of Alabama and press ENTER: Montgomery
M
O
N
T
G
O
M
E
R
Y
Incorrect. Total correct guesses: 0
Enter the capital of Alaska and press ENTER: JUNEAU
J
U
N
E
A
U
Incorrect. Total correct guesses: 0
Enter the capital of Arizona and press ENTER:
I believe it is the convertCase()
function causing this, but can't quite figure out what is setting it off to (seemingly) not compare the converted string as a whole.
Guidance appreciated! Please make it simple, I am still trying to figure this all out, it is rather confusing and there's a lot of jargon I am still puzzling through.