This is supposed to be a program that is supposed to read in strings from an input file and output the first word of each line. There are several ways to solve this problem. I'm supposed to solve it in two ways,
1.) once with an input function or operator,
2.) the other with one or more string functions.
cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
ifstream infile;
string line, inputfilename;
cout << "Enter the file name ";
getline (cin, inputfilename);
infile.(inputfilename.c_str())
// some code needed here to input from the file
while (infile)
{
// some code here to output the desired word
// some code needed here to input from the file
}
infile.close();
return 0;
}
Example: an input file called xyz
This is the story of
a cat and a
dog who were
friends
Example Output should be:
This
a
dog
friends
I have written some code for this, but I can't ever seem to get out of the loop. If anyone can help, it would be appreciated. I've also got another similar problem:
This program should read in 6 numbers from the keyboard and then indicate whether any of them were larger than 500 or smaller than 10. It is possible that both events occurred, that neither did or that only one of the two did. And it is possible that the same event happens more than once.
First, write the program so that it reads in 6 numbers and echoes them to the screen. You must use a loop.
Then add in a flag that will detect numbers larger than 500. Use a bool variable.
Then add in another flag that will detect numbers smaller than 10. Again use a bool variable.
The feedback to the user should happen following (outside) the end of the loop, not during the input phase. The user is not interested in how many times an event occurred, only whether it happened or not.
Here is the code I've written, and like most of the things I do in C++, it doesn't work.
#include <iostream>
using namespace std;
int main()
{
int a,b,c,d,e;
bool over500 = false; //Bool flag for testing if numbers are over 500
bool under10 = false; //Bool flag for testing if numbers are under 10
// Displays command for user to enter input of integers then reads into variable location
cout<<"Enter the first number: "<<endl;
cin >> a;
cout<<"Enter the second number: "<<endl;
cin >> b;
cout<<"Enter the third number: "<<endl;
cin >> c;
cout<<"Enter the fourth number: "<<endl;
cin >> d;
cout<<"Enter the fifth number: "<<endl;
cin >> e;
while (a)
{
if (a > 500 || b > 500 || c > 500 || d > 500 || e > 500 )
over500 = true;
if (a < 10 || b < 10 || c < 10 || d < 10 || e < 10 )
under10 = true;
}
if (over500 == true)
cout << "One of the numbers is greater than 500. "<<endl;
if (under10 ==true)
cout << "One of the numbers is less than 10. "<<endl;
return 0;
}
Again, anyone that help me out with these problems, it would be greatly appreciated. Thanks.
** Edit **