I have a CharConverter class, with 2 functions - 1. uppercase member function, where a user inputs a sentence and makes any and all letters uppercase in a sentence, & 2. properwords function, where a user inputs a sentence, and makes the first letter of each words uppercase. I have the uppercase function done and working, but I need help with the properwords function. Here is my code so far..
#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>
usingnamespace std;
class CharConverter //CharConverter class
{
private: //private variables
string sentence;
char ch;
int count;
public: //public functions
void uppercase(string sentence)
{
cout << "Input a sentence and I will output every letter to uppercase\n";
getline(cin, sentence); //user inputs a sentence
getline(cin, sentence); //I have this second one in so the program works properly..
for (int count = 0; count < sentence.length(); count++) //for loop, if count is less than length of sentence
{
sentence[count] = toupper(sentence[count]); // uppercase a copy of the next character, assign to sentence
}
cout << sentence ; //displays sentence, letter by letter
return;
}
void properWords(string sentence)
{
cout << "Input a sentence and I will output the sentence using proper words\n";
getline(cin, sentence);
getline(cin, sentence);
}
};
int main()
{
CharConverter mainSentence; //defines a mainSentence object of class CharConverter
string sentence; //defines a sentence string
int getChoice; //defines a getChoice int
cout << "Would option would you like to choose?\n"; //asks which option the user wants
cout << "1. uppercase function\n" << "2. properwords function\n" << endl; //displays options
cin >> getChoice; //user inputs either 1 or 2
if (getChoice ==1) //if user inputs 1
{
mainSentence.uppercase(sentence); //proceedes with uppercase function from CharConverter class
system("PAUSE");
}
if (getChoice ==2)
{
mainSentence.properWords(sentence); //proceeds with properWords function from CharConverter class
system("PAUSE");
}
return 0;
}
I have 2 "getline(cin, sentence)" becasuse for some reason, the program wasn't responding with just 1. But I need help with the properwords function. I know it has something to do with ASCII values (for instance, A is 65 and a is 97), but how di I implement that in a function??
You are trying to uppercase the first letter in every word, right? You can look for whitespaces and then uppercase the following letter.
The reason, why you need two getlines is because the cin in line 47 leaves a newline character in the input stream. Here is a good article about this topic
Oh yeah, I need ot take the char ch; line out, I accidently left that in after editing my code. So just look for whitespaces? That's a good idea, I haven't thought about that. I have to research how to do that but thank you for leading me on the right path!!
Well, I put this line in, inside the properWords function:
1 2 3 4 5 6 7 8
for (int count = 0; count < sentence.length(); count++) //for loop, if count is less than length of sentence
{
if (sentence[count] = " ") // if whitespace is found
{
count++; //increment count, so it goes to the character after the whitespace
sentence[count] = toupper(sentence[count]); //make the character uppercase
}
}
But I got an error:
(37) : error C2440: '=' : cannot convert from 'const char [2]' to 'char'
There is no context in which this conversion is possible
If I was able to fix it, would the code solve my problem or no? I'm trying to make sense of the error..