Strings

Hello, I'm new to C++ and I'm supposed to write a program that will change the first letter in a file to uppercase and the remaining letters in the words to lowercase.
The input file looks like: onE fish two FISh red fISh bLuE Fish
THE doG is BlaCk aNd bLUE
The problem I'm having, is when I run it, it produces each word on the same line instead of each title on the same line. A title is "onE fish two FISh red fISh bLuE Fish" "THE doG is BlaCk aNd bLUE"
So my output looks like this:One Fish Two Fish Red Fish Blue Fish The Dog Is Black And Blue
Instead of::One Fish Two Fish Red Fish Blue Fish
The Dog Is Black And Blue
#include <iostream>
#include <string>
using namespace std;
void reformat (string&);
int main ()
{
string text;
cin >> text;
while (cin){
reformat (text);
cout << text;
cin >> text;
}
return 0;
}
void reformat (string& word)
{
int wlen;
wlen = word.length();
word[0]= toupper(word[0]);
for (int i=1; i<wlen; i++){
word [i]= tolower(word[i]);
}}

Last edited on
The input file looks like:


Your not using a input file. If your supposed to use a input file, maybe it would be wise to do that first and then correct the output.

closed account (48T7M4Gy)
As an adjunct to that, and either way, reading a line at a time instead of a single string at a time via cin is the solution to the separate lines problem.

ie getline() function

Then if spaces are the only non-alpha characters in the line, advantage can be taken of that in making the switch between upper and lower case. (Other separators are easily incorporated if necessary.)

http://www.cplusplus.com/doc/tutorial/files/
Topic archived. No new replies allowed.