Can anyone help me figure out what I'm doing wrong. I'm new to programming and for my class we had to modify the program so that it reads its input from a text file and the output of the program should go to the screen -- only the input is to be file-based.

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

using namespace std; 
 
int main()
{
  // open aboutYou.txt for input
  ifstream fin;
  fin.open("aboutYou.txt", ios::app);
  if (!fin.good()) throw "I/O error";

  // read an int value from one line of an input file
  int age;
  fin >> age;
  fin.ignore(1000, 10);

  // read a double value from one line of an input file
  double gpa;
  fin >> gpa;
  fin.ignore(1000, 10);

  // read a string value from one line of an input file
  string name;
  getline(cin, name);

  // read a char value from one line of an input file
  char gender;
  fin >> gender;
  fin.ignore(1000, 10);

  // print output
  ofstream fout;
  fout.open("aboutYou.txt", ios::app); 
  fout << endl;
  fout << age << endl;
  fout << gpa << endl;
  fout << name << endl;
  fout << gender << endl;
  
  fin.close();
  return 0;
}

Recommended Answers

All 9 Replies

post the input file. You probably don't need those ignore() lines, unless there is other text on each of the lines after the number. The >> operator will ignore all spaces and the '\n' line terminating character(s).

I deleted the ignore lines, but I still can't get an output. I'll enter the command, press enter, and nothing will show up after.

// print output
  ofstream fout;
  fout.open("aboutYou.txt", ios::app); 
  fout << endl;
  fout << age << endl;
  fout << gpa << endl;
  fout << name << endl;
  fout << gender << endl;

You told me that output should go to the screen, right? Then just tell me why are you outputting to fout? use cout instead.

cout << endl;
cout << age << endl;
cout << gpa << endl;
cout << name << endl;
cout << gender << endl

I changed it to cout and there still is no output =/.
I'm confused!

This is what my teacher asked:
"Modify the program so that it reads its input from a text file named aboutYou.txt. Note that the user prompts should be removed.

The file aboutYou.txt should have one value per line, for a total of 4 lines, like the itsAboutYou2.cpp program from chapter 5. The file should be placed in your "working directory", which for command-line compiling is the same folder where the source file is stored.

The output of the program should go to the screen -- only the input is to be file-based.

Compile and run the program. It should read 4 items of data from the text file aboutYou.txt. It should then summarize the 4 input values with labels and the values you put in the text file. "

I really need help on this. It seems really basic and I'm screwing up!
Here's the itsAboutYou2 code that goes along with the exercise:

#include <fstream>
#include <iostream>
#include <string>
using namespace std;
 
int main()
{
  // open aboutYou.txt for input
  ifstream fin;
  fin.open("aboutYou.txt");
  if (!fin.good()) throw "I/O error";

  // read an int value from one line of an input file
  int age;
  fin >> age;
  fin.ignore(1000, 10);

  // read a double value from one line of an input file
  double gpa;
  fin >> gpa;
  fin.ignore(1000, 10);

  // read a string value from one line of an input file
  string name;
  getline(fin, name);

  // read a char value from one line of an input file
  char gender;
  fin >> gender;
  fin.ignore(1000, 10);
 
  fin.close();
  return 0;
}

First make sure you have the file on your disk in proper format.
Now look closely, at the itsAboutYou2 code, you already have read the file to all the variables right?
Now you need to display on screen. So you can use cout.
Just add

cout << endl;
cout << age << endl;
cout << gpa << endl;
cout << name << endl;
cout << gender << endl

after fin.close() and it will run.(it is running on my pc atleas)

I got it to work!
I used cout and it wasn't working, then I saw that the code to open my file for output was wrong. Now it's working!

Thank you both for the help! Hopefully I can do the rest of these exercises without any problems.

But you must have added cout anyways.
without cout, you cannot output it to console.
Anyways, happy that it is working for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.