The first day of class today we had an (optional) diagnostic programming assignment. The problem is: to take an input file with a sentence in it, and make all letters except the first one lowercase, make the first letter uppercase, and remove any extra spaces and line breaks. After working for awhile, and looking around for help, I have a basic idea of what I have to do, but so far it doesn't seem to be working.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
#include <string>
using namespace std;
ifstream in_stream;
string line;
char file_name_input[21];
char firstchar;
int main()
{
cout << "Enter the file name of your input file (maximum of 20 characters):\n";
cin >> file_name_input;
in_stream.open(file_name_input);
if (in_stream.fail( ))
{
cout << "Input file opening failed.\n";
exit(1);
}
in_stream >> firstchar;
//cout << firstchar;
toupper(firstchar);
cout << firstchar;
if(in_stream.is_open())
{
while(in_stream)
{
getline(in_stream, line);
//tolower(line);
cout << line << endl;
}
cout << line.size();
}
in_stream.close ( );
return 0;
}
Right now, this is rather messy, but what I'm thinking is read in the first letter and convert it to uppercase using toupper, then read in the rest using getline, and use line.size() to determine the length of the string. Then, get the characters into an array and remove the spaces and then read back out. However, toupper and tolower are not working for me and line.size is not giving me the right number of characters. Please give me some comments, as I would like to understand these concepts before we get deeper into class. Thanks.