double display when outputting...

I have an assignment in class that asked me to write and unknown number of records(but less than 20) to a file and read it back and display it.

i got this to work but i get end up displaying the last entry twice. i was wondering if there was a better way to fix this. and just so you i have already turn the assignment in.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
its was originally:
for(int i = 0; i<20 && !infile.eof();i++)
{
     // get info from file here
     // displace with cout statment here
}

my solution:
// get info from file here
for(int i = 0; i<20 && !infile.eof();i++)
{
     // displace with cout statment here
     // get info from file here
}


like i said, is there a better way to handle this?
Don't loop on EOF. Loop on good() immediately after attempting to read your data.

1
2
3
4
for (int i = 0; (i < 20) && (infile >> myrecord); i++)
  {
  // display myrecord to cout here
  }

Presumably you wrote an overloaded operator >> to read a record, right?

1
2
3
4
5
istream& operator >> ( istream& ins, MyRec& myrecord )
  {
  // Read the record info from the given input stream (ins) here
  return ins;
  }

Hope this helps.
no overloaded operator but i do know what you are getting at. this assignment isn't using object though.

does the first example where "(infile >> myrecord)" act as a bool and if there is a value then run the loop? will this still work for "(infile >> fname >> lname >> age)?
Yes.

The if (infile >> whatever) is the same as if ((infile >> whatever).good()). (Remember that operator>> returns the istream and if (infile) is the same as if (infile.good()).

Hope this helps.
ok thanks, i got it working but now i hav one more question:

my input needs to be: Firstname Initial Lastname Number

Enter info(end to stop): Hey u There 00000

but when i enter "end" to stop the program i still have to enter information for the other parts

how can i get a round this with out making multiple cin statements or "overload >>"?

1
2
3
4
5
6
7
8
9
ofstream outfile("empdata.txt");

	for(int i = 0; first != "end" && i < 20; i++){
		cout << "Enter the info(end to stop): ";
		cin >> first >> init >> last >> num;
		if(first == "end")
			outfile << first << " " << init << " " << last << " " << num << endl;
	
	}
Read one thing at a time. If the user does not type "end", then read the MI, Surname, Number.

Personally, I can't stand magic words in input. I don't remember where I read this, but I seem to recall that a military computer system was broken because the programmer used the name "Donald Duck" as a hard-coded test case in the GI software. Problem was, some people actually are named Donald Duck -- including one poor fellow who enlisted.

It is better to enter nothing if you are done. Read one line of input at a time. If it is empty, then your user is done. If not, use a std::stringstream to get it into your variables.

1
2
3
4
5
6
7
8
9
10
11
12
13
string s;
for (int i = 0; i < 20; i++)
  {
  cout << "Enter info (or enter nothing to stop): " << flush;
  getline( cin, s );
  if (s.empty()) break;  // user is done

  istringstream ss( s );
  ss >> firstname >> middleinitial >> surname >> number;
  if (ss.fail()) fooey();

  outfile << ... << endl;
  }

Hope this helps.
ok well i understand but the original assignment asked for the user to type 'end' to stop the program.

so does the istringstream variable make the string acts as if it is user input like cin? if so could i do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
ofstream outfile("empdata.txt");

	for(int i = 0; i < 20; i++){
		cout << "Enter the info(end to stop): ";
		getline(cin, s);
		if(s != "end" && s != "end\r"){
                        istringstream line(s);
			line >> first >> init >> last >> num;
                        outfile << first << " " << init << " " << last << " " << num << endl;
               else
                        break;
	
	}
i am getting an error here-- any ideas?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	ofstream outfile;
	ifstream infile;
	string filename = "empdata.txt", first = "" , last = "",s, line;
	char init;
	unsigned long num;

	outfile.open("empdata.txt");

	for(int i = 0; i < 20; i++){
		cout << "Enter the info(end to stop): ";
		getline(cin, s);
		if(s != "end" && s != "end\r"){

                istringstream line(s); //cannot convert from 'std::string' to 'int'
		line >> first >> init >> last >> int(num);
		outfile << first << " " << init << " " << last << " " << num << endl;}
        else
            break;
	
	}
	outfile.close();
Last edited on
You've got two variables named "line", one (a std::string) on line 11 and another (a std::istringstream) on line 22. Get rid of the one on line 11 and it should work for you.

Also, that cast to 'int' on line 23 looks suspicious. Why are you doing that? (Your variable is already an 'unsigned long'.)

Hope this helps.
i am getting this error.

error C2079: 'line' uses undefined class 'std::basic_istringstream<_Elem,_Traits,_Alloc>'
Oops, sorry I missed that. You also need to #include <sstream>
I knew something was missing!!! i couldn't find the right include to... include!lol

its works just like i wanted thanks Duoas
Topic archived. No new replies allowed.