lower to uppercase

I am trying to complete an exercise from c++ primer 5th edition but keep i failing.
The exercise is:

Write a function that takes a reference to a string object as its parameter and that converts the contents of the string to uppercase. Use the toupper() function described in
Table 6.4. Write a program that uses a loop which allows you to test the function with
different input. A sample run might look like this:

Enter a string (q to quit) : go away
GO AWAY
Next string (q to quit) : good grief!
GOOD GRIEF!
Next string (q to quit) :
Bye.

please copy this source code and compile it and see how you can help me,
i would really appreciate some 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
#include <iostream>
#inclyde <cctype>
void lower_to_upper(char & Letters);

int main()
{
using namespace std;

char words;

cout << "Enter a string (press q to quit): ";
cin.get(words);
while(true)
{

if(words == 'q')
{
cout << "Bye.\n";
break;
}

else if(words != 'q')
{
lower_to_upper(words);
cout << "Next string (q to quit):";
}

cin.get(words);
}
return 0;
}

void lower_to_upper(char & Letters)
{
using namespace std;
cout << (char) toupper(Letters);
}


Here is my output:

Enter a string:code 
C
ONext string (q to quit):DNext string (q to quit):ENext string (q to quit):


As you can see the program has problems. If anyone knows what i mean please help me.
You are using a single char(acter) to store input, when you clearly want to store a string (i.e. multiple characters).
closed account (zb0S216C)
words can only hold a single character. So, when the user enters a string, it only reads a single character. Changing words to a char *, or string will help you.

Wazzak
However a char* variable will break the if statement in your loop.
I was bored and ended up writing my own program that does this, like I always end up doing on these forums. So anyway here it is! (lol)
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
#include <string>
#include <iostream>

void ConvertString(std::string &, bool);

int main()
{
	std::string myString;
	char upperType;

	std::cout << "Enter a string: ";
	std::getline(std::cin, myString);
	std::cout << "Would you like to convert this string [U]pper or [L]ower case: ";
	std::cin >> upperType;

	switch(tolower(upperType))
	{
	case 'u':
		ConvertString(myString, true);
		std::cout << "Your converted string is: " << myString << std::endl;
		break;
	case 'l':
		ConvertString(myString, false);
		std::cout << "Your converted string is: " << myString << std::endl;
		break;
	default:
		std::cout << "ERROR! You have selected an invalid choice." << std::endl;
		break;
	}

	return 0;
}

void ConvertString(std::string &StringToConvert, bool Upper)
{
	switch(Upper)
	{
	case true:
		for (int i = 0; i < StringToConvert.size(); i++)
			StringToConvert[i] = toupper(StringToConvert[i]);
		break;
	case false:
		for (int i = 0; i < StringToConvert.size(); i++)
			StringToConvert[i] = tolower(StringToConvert[i]);
		break;
	}
}
Last edited on
Perhaps don't just post answers, sort of defeats the purpose of an excericse (though I'm guilty of this :/ )
Last edited on
Yeah i should'nt have i just wanted to be more specific. Thanks to TheNoobie i have now solve the problem and thanks to you all for your help.
Topic archived. No new replies allowed.