1 Replies - 2497 Views - Last Post: 09 May 2011 - 12:18 PM Rate Topic: -----

#1 chadihuoma   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 19-June 08

Input failure

Posted 09 May 2011 - 12:03 PM

Code is below, but first, the question.

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 would output

and
if
then

Assume that a comma appears within at least every 200 characters in the file.

1. I used the exaxt text above and saved it as "commas"
2. My problem is nothing happens after i enter the file name. After i press enter multiple times, the cursor just moves to the next line. Initially, i saved the file as a word document, then with wordpad, and then notepad. Ran them all separately but still nothing happens. I think it's in a fail state but i dont know how the fix the problem. Thanks.

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;
}


This post has been edited by JackOfAllTrades: 09 May 2011 - 12:07 PM
Reason for edit:: Removed question from code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Input failure

#2 jimblumberg   User is online

  • member icon

Reputation: 5789
  • View blog
  • Posts: 17,700
  • Joined: 25-December 09

Re: Input failure

Posted 09 May 2011 - 12:18 PM

You should always check if your file open worked. If the file does not open then the rest of this program will not produce any outputs. Also you should probably use the getline() function to retrieve the file name from the user. As the extraction operator ( >> ) stops at white space, and it is possible to have a file name with spaces.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1