Character Converter Class

Pages: 123
Oh! duh. I don't need to ask the user in int main() because I already ask them in the uppercase part.
lordzedd,bro.Solving this problem in this fashion is nothing more than a temporary solution.
you obviously don't grasp the most basic of concepts about programming
do what the people in page one told you to do.learn about loops,conditional statements,functions,all of the basics,then come back to this problem,and you will be able to solve it easily
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
//Chris Cochran
//COP2000.0M2
//Project 7
//This program will uppercase words and sentences

#include <iostream>
#include <cctype>
#include <string>
using namespace std;


class CharConverter
	{
	private:
		string sentence;
		int count;
	public:
		void uppercase(string sentence)
		{
			cout << "Type a sentence to see every letter be uppercased\n";
			getline(cin,sentence);
		for (int count = 0; count < sentence.length(); count ++)
		{
			sentence[count] = toupper(sentence[count]);
		}
			cout << sentence << endl;
		}
		void properWords(string sentence)
		{
			cout << "\nType a sentence to see the first letter of every word be uppercased\n";
                        cin.sync();
			getline(cin,sentence);
			for (int count = 0; count < sentence.length(); count++)
			{
				sentence[count] = toupper(sentence[count]);
				if (sentence[count] = ' ') 
				{
					count++; 
					sentence[count] = toupper(sentence[count]);
				}
				cout << sentence << endl;
			}
		}

};

int main()
{

	CharConverter mainSentence;
	string choice;
	string sentence;

	mainSentence.uppercase(sentence);
	mainSentence.properWords(sentence);

	do
	{
	cout << "Do you want to try again?(Y/N)\n";
	cin >> choice;

	if (choice != "y" && choice != "Y" && choice != "n" && choice != "N")
		cout << "Please enter 'y' for yes or 'n' for no.\n";
	} while (choice != "y" && choice != "Y" && choice != "n" && choice != "N");	
	 while (choice == "y" || choice == "Y");
		cout << "Have a good day!\n\n\n";
		
	return 0;
}


I am almost done except the properWords function output is weird and when you type y or Y to do again, it doesn't do anything. Any help to fix those?
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
//Chris Cochran
//COP2000.0M2
//Project 7
//This program will uppercase words and sentences

#include <iostream>
#include <cctype>
#include <string>
using namespace std;


class CharConverter
	{
	private:
		string sentence;
		int count;
	public:
		void uppercase(string sentence)
		{
			cout << "Type a sentence to see every letter be uppercased\n";
			getline(cin,sentence);
		for (int count = 0; count < sentence.length(); count ++)
		{
			sentence[count] = toupper(sentence[count]);
		}
			cout << sentence << endl;
		}
		void properWords(string sentence)
		{
			cout << "\nType a sentence to see the first letter of every word be uppercased\n";
			cin.sync();
			getline(cin,sentence);
			sentence[ 0 ] = toupper(sentence[ 0 ]);
			for (int count = 0; count < sentence.length(); count++)
			{
				if (sentence[count] == ' ') 
				{
					count++; 
					sentence[count] = toupper(sentence[count]);
				}
									
			}
			cout << sentence << endl;
		}

};

int main()
{

	CharConverter mainSentence;
	string choice;
	string sentence;

	mainSentence.uppercase(sentence);
	mainSentence.properWords(sentence);

	do
	{
	cout << "Do you want to try again?(Y/N)\n";
	cin >> choice;
	
	if (choice != "y" && choice != "Y" && choice != "n" && choice != "N")
	{
		cout << "Please enter 'y' for yes or 'n' for no.\n";
		cin >> choice;
	}
	}
	 while (choice == "y" || choice == "Y");
	 {
	 
	 }
	
		cout << "Have a good day!\n\n\n";
	
	return 0;
}


Alright, I found out that I had to add sentence[ 0 ] = toupper(sentence[ 0 ]); right before the for statement because that makes it set the first letter of the string to uppercase. I had to take out the first sentence[count] = toupper(sentence[count]); statement because it was redundant and in the if statement, I had to make it == not =. So I got the properWords function completed. I just need help with the last loop that asks if the user wants to do it again. I'm having troubles trying to figure that out.
if (choice != "y" && choice != "Y" && choice != "n" && choice != "N") this should probably be a while loop not an if statement.

Also why is line 52 a string instead of character? Though if you make it a character you would put ' around characters instead of " since that is for a string so "n" would become 'n'.

Lines 70 and 72 are not needed by the way.



ya i figured that out about the while instead of if. I changed the int main()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{

	CharConverter mainSentence;
	string choice;
	string sentence;
	while (choice != "n" && choice != "N"){
		mainSentence.uppercase(sentence);
		mainSentence.properWords(sentence);

	
		cout << "Do you want to try again?(Y/N)\n";
		cin >> choice; // here is where its looping back to the function
	
		while (choice != "y" && choice != "Y" && choice != "n" && choice != "N")
		{
			cout << "Please enter 'y' for yes or 'n' for no.\n";
			cin >> choice;
		}
	}
	
	cout << "Have a good day!\n\n\n";
	return 0;
}

It works properly now. I just had to use some common sense lol
I'm glad you have this figured out.
Topic archived. No new replies allowed.
Pages: 123