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?

Re: Input Failure

Posted 09 May 2011 - 02:42 PM

chadihuoma, 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?

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).

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.

Re: Input Failure

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.

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.