Question:
Write a C++ program that reads data from a file whose name is input by the user, and that outputs the first word following each of the first three commas in the file. For example, if the file contains the text of this problem, then the program output
and
if
then
Assume that a comma appears within at least every 200 characters in the file.
1.) I used this exact question for the file and saved is as "commas".
2.) I saved the file initially with word, then wordpad, and notepad, still, no difference.
3.) My problem, nothing happens after i enter the filename, even when i press enter multiple times, the cursor keeps moving to the next line. I'm sure the program it's in a fail state, but i dont know how to correct the problem.
4.) Does it matter whether i use cin or getline to read the data?
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
ifstream infile;
string first_word, second_word, third_word;
string filename;
int main()
{
//prompt user to enter the file name
cout << "Enter file name:" << endl;
cin >> filename;
infile.open(filename.c_str()); //converting string to C string
infile.ignore(200, ',');
infile >> first_word;
cout << first_word;
infile.ignore(200, ',');
infile >> second_word;
cout << second_word;
infile.ignore(200, ',');
infile >> third_word;
cout << third_word;
cin.get(); cin.get();
return 0;
}

New Topic/Question
Reply




MultiQuote










|