Hello crackedice,
I think using namedpsace:std is required at this point. |
I would say that your instructor is lazy and does not want to teach what you should be learning now instead of later when you have to learn everything at one time.
At least the instructor hasn't taught the other method yet. |
And may never teach you what you need because it is not in the design of what they are told to teach, but this should not hinder you from learning everything that you can where you can. Here you can get all you need to learn for free. All you have to do is read and ask the right questions. If you learn how to qualify what is in different name spaces it will make you look better for learning something that is not taught in class.
I see how the program works now that I have tested it. In main the use of the variable name "sentence" is misleading. I think of a variable name "sentence" as something that will hold several words or what would be an entire sentence. In main you can change "sentence" to "word" and that will not affect the use of "word" in the function because they are different scopes.
Since "cin >>" will read up to the first white space or new line which ever comes first. That why, along with the variable "sentence", I was a little confused.
The program works one word at a time, but you have done nothing to read from a file. You need to include the header file "<fstream>" and actually open a file stream to use a file.
This code is more than you need, but I found it useful when first learning to use a file stream.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
std::string iFileName{ "" }; // <--- Put file name here.
std::ifstream inFile;
inFile.open(iFileName);
if (inFile.is_open())
{
std::cout << "\n File " << iFileName << " is open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2)); // <--- Needs header files chrono" and "thread".
}
else
{
std::cout << "\n File " << iFileName << " did not open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3)); // <--- Needs header files chrono" and "thread".
exit(1); // <--- If it is not open there is no need to continue.
}
| |
In the if statement you can comment out these lines when you do not need them any more. Then in place of the for loop I would use:
while (inFile >> word)
. to read the whole file. This will allow you to read a whole file of unknown length. When you have learned more about using file streams this code can be shortened. When you learn how to do that you will have learned something useful.
In main I replaced the for loop with a while loop to allow reading the whole file. Inside the loop I used the code
if (inFile.peek() == '\n') std::cout << '\n' << std::endl;
to check for a new line (\n) and if true it prints a new line and a blank line before the next line. the "peek" function is a way of looking at the input stream without extracting the character from the input buffer. Although you could just as easily extract the new line from the buffer and print it.
The while loop looks like:
1 2 3 4 5 6
|
while (inFile >> word)
{
reformat(word);
std::cout << word << ' ';
if (inFile.peek() == '\n') std::cout << '\n' << std::endl;
}
| |
Hope that helps,
Andy