8 Replies - 11709 Views - Last Post: 11 May 2011 - 07:00 AM Rate Topic: -----

#1 chadihuoma  Icon User is offline

  • New D.I.C Head

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

Input Failure

Posted 09 May 2011 - 11:37 AM

Code is below, but here is the question first.

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



Is This A Good Question/Topic? 0
  • +

Replies To: Input Failure

#2 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Input Failure

Posted 09 May 2011 - 02:31 PM

A question, not a tutorial. Moved to the C++ forum. :)
Was This Post Helpful? 0
  • +
  • -

#3 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Input Failure

Posted 09 May 2011 - 02:38 PM

so what issues are you having?
Was This Post Helpful? 0
  • +
  • -

#4 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Input Failure

Posted 09 May 2011 - 02:38 PM

is the file in the same directory as your program? if not, make it so.
are you appending .txt to the end of what you are entering? if not, make it so.
:)
Was This Post Helpful? 0
  • +
  • -

#5 RevTorA  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 76
  • View blog
  • Posts: 251
  • Joined: 22-April 11

Re: Input Failure

Posted 09 May 2011 - 02:42 PM

View Postchadihuoma, on 09 May 2011 - 11:37 AM, said:

Code is below, but here is the question first.

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



1) Keep your use of global variables to a minimum, or preferably don't use them at all. It's a bad habit to use them a lot and you'll end up with a lot more errors down the road that are really hard to figure out.

2) open() can take a regular string for the argument, no need to convert it to a c string.

3) You should always check to make sure that the file opened correctly with if(!infile) cout << "Some error message";. If the file wasn't opened successfully, then the rest of the code that tries to read from it isn't going to return anything, and as such all of your couts aren't going to print anything.

4) Yes it matters if you use cin vs. getline. The insertion operator stops inserting after any whitespace, whereas getline stops inserting after a newline character (like an enter).

5) Try the above and let us know how it went :)
Was This Post Helpful? 0
  • +
  • -

#6 chadihuoma  Icon User is offline

  • New D.I.C Head

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

Re: Input Failure

Posted 10 May 2011 - 11:53 AM

code below. In response to the suggestions i received, the program still didn't work. Here's the adjustments

1. I moved the declarations inside of the main function.
2. changed the file name from "commas" to "commas.txt.
3. Initially, had the function c_str() to convert the string to c string but, took it out and it still didn't work.
the reason i used the c_str() was because the book suggests so, if you're to input the file name from the user.
4. I've been told to check if the file opened successfully, but doing that, with the constructs of the if statement is not till the next chapter. I don't want to jump ahead, even though i've reviewed the chapter. I feel as the book has the problem in ch 4(program input and the software design process), i should still make it work.
5. Any other suggestions? Is my code even correct?

here's the question and the code below:

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.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{

ifstream infile;
string first_word, second_word, third_word;
string filename;

	//prompt user to enter the file name
	cout << "Enter file name:" << endl;
	cin >> filename;
	

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


MOD EDIT: Added code tags. When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 10 May 2011 - 01:19 PM

Was This Post Helpful? 0
  • +
  • -

#7 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Input Failure

Posted 10 May 2011 - 12:09 PM

first, please use code tags! If you have written the code out first then you can just highlight it and click the code option above your text.
In your code, where have you opened the file?

And in your last post (where you should have posted this) the only thing that didn't work as you wanted was that you either weren't adding .txt (or whatever extension) to the file, or the file was not in the right directory. Because it worked for me. When in doubt, you usually put it where your cpp file(s) is and where the executable is.
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,520
  • Joined: 23-August 08

Re: Input Failure

Posted 10 May 2011 - 01:20 PM

Merged duplicate topics. Please do not open a NEW topic when you already have one on the same subject/problem!
Was This Post Helpful? 0
  • +
  • -

#9 RevTorA  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 76
  • View blog
  • Posts: 251
  • Joined: 22-April 11

Re: Input Failure

Posted 11 May 2011 - 07:00 AM

The point of checking if the file has been opened with the if statement at the moment is more just to see if you've put the file in the right directory. Sometimes it's hard to find the working directory when debugging a program. For instance, VC++ (at least, 2010), defaults the work directory to the project directory, not where the executable actually resides (in a Debug or Release folder for instance). The reason for that being you don't want to have to have a copy of your data for each build type, release or debug.

If you're using VC++, then I suggest either making sure the output file is in the project's directory (one directory back from where the executable was built), or changing the work directory to ($OutDir) in the Debugging section of the project's properties.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1