I know that I have to convert what the user puts in to all uppercase if the letter is lowercase. I think I know how to do that. The part that I don't know is 1. How to start the code and 2. The properword function that uppercases the first word after each space. I don't the that function.
//Chris Cochran
//COP2000.0M2
//Project 7
//This program will uppercase words and sentences
#include <iostream>
#include <cctype>
#include <string>
usingnamespace std;
class CharConverter
{
private:
string sentence;
int count;
public:
void uppercase(string sentence)
{
for (int count = 0; count < sentence.length(); count ++)
{
sentence[count] = toupper(sentence[count]);
cout << sentence;
return;
}
}
void properWords(string sentence)
{
for (int count = 0; count < sentence.length(); count++)
{
sentence[count] = toupper(sentence[count]);
if (sentence[count] = ' ')
{
count++;
sentence[count] = toupper(sentence[count]);
}
}
}
};
int main()
{
CharConverter mainSentence;
string choice;
string sentence;
cout << "Type a word or sentence.\n";
cin >> sentence;
mainSentence.uppercase(sentence);
mainSentence.properWords(sentence);
return 0;
}
It compiles and runs fine, but when I type in a word or sentence, it gives me this error:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Microsoft Visual C++ Debug Library
---------------------------
Debug Assertion Failed!
Program: ...hris\Desktop\COP2000 Visual C++\Project 7\Debug\Project 7.exe
File: c:\program files (x86)\microsoft visual studio 10.0\vc\include\xstring
Line: 1440
Expression: string subscript out of range
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
---------------------------
Abort Retry Ignore
---------------------------
Is that something to do with something in int main()?
Not sure if this is what caused your error, but look at line 21, where you increment count. Also, when I tried your code it only took the first word of what I typed. I think you might need to use another function other than cin. I'm not sure which one it is in C++, but I've been seeing alot of getline(cin, variable) around.
Is it supposed to turn "hello my username is swp147. your name is lordZedd." into "Hello my username is swp147. Your name is lordZedd" ?
void uppercase(string sentence)
{
cout << "Type a sentence to see every letter be uppercased\n";
cin >> sentence;
for (int count = 0; count < sentence.length(); count ++)
{
sentence[count] = toupper(sentence[count]);
cout << sentence;
}
}
void properWords(string sentence)
{
cout << "Type a sentence to see the first letter of every word be uppercased\n";
cin >> sentence;
for (int count = 0; count < sentence.length(); count++)
{
sentence[count] = toupper(sentence[count]);
if (sentence[count] = ' ')
{
count++;
sentence[count] = toupper(sentence[count]);
}
}
}