Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 9th, 2006, 6:22 PM   #1
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
More fileopen close stuff extending from my last thread!

I'm back quickly!

Well, if you read the last massive thread i started, then you'll be on track for where this is going. Basically, I'm doing a program that will read the file i made in the last thread, and then give you some options to change data in it. Before I can get into the fun part of parsing lines and what not, I at least need to get the data from the file. The following is what i have thus far:

#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <iomanip>
#include <vector>
#include <cstdlib>
using namespace std;


int main()
{
	int i=0,j=0,id[10];
	vector<int> years;
	char sex[10];
	float wage[10];
	int yearsworked;
    ifstream infile;
    ofstream outfile;
    string disp1, disp2, filename;
    float holder[10], line;

    cout << "Please input the name of the file you wish to open: " ;
    getline(cin, filename);
    
    infile.open(filename.c_str());
    if(infile.fail())
    {
                      cout <<"\nThe file was not successfully opened." << endl;
                      exit(1);
    }
    getline(infile, disp1);
    getline(infile, disp2);
    
    while(getline( infile, line))
    {
         holder[i] = line;
         i++;
    }
    infile.close();
    
    cout << "\nThe file contained the following:" << endl;
    
    cout << disp1 << endl << disp2 << endl;
    for(j=0; j<10; j++)
    {
             cout << holder[i] << endl;
    }
     
    
    
	system("PAUSE");
	return 0;
}

It gives me the error:
no matching function for call to `getline(std::ifstream&, float&)'
which corresponds to:
while(getline( infile, line))

I was hoping to read in the first to lines which are just labels for the table, and then store the next 10 lines in an array. From there i would extract data from each line and into a corresponding array like the last thread, and alter it that way, but i'll do that part AFTER i can successfully compile this code and read in the file..lol... I'm betting this one is simple.
codylee270 is offline   Reply With Quote
Old Apr 9th, 2006, 7:02 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
I believe the getline function will only read into a std::string class, though I could be wrong on that. What we definitely know is that it won't read into a float. However, that's what the iostream operators are for:

while (infile >> line)
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 9th, 2006, 7:09 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Just a small comment on your terminal remark in the last thread: see this check, in your new code?
    if(infile.fail())
    {
                      cout <<"\nThe file was not successfully opened." << endl;
                      exit(1);
    }
cin is an input stream also. There's no reason not to give it the same attention. It's cheap. If you check my examples, I don't do exhaustive error analysis, but I write a small function, something like this:
int OMGBBQ!!!!!1111 (string catastrophe)
{
   cerr << catastrophe << endl;
   return 1;
}
and call it something like this:
   cin >> someVariableRequiringCorrectInput;
   if (!cin.good ()) return OMGBBQ!!!!!1111 ("Bad user!  Bad user!");
   ...
   // more code

Now, as to your current question: did you read the types of arguments required by the various versions of 'getline', and what its return is? You can't coerce your libraries to have things they don't have just out of pure desire. If the writers didn't provide them, and you want them that badly, you have to write the yourself.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 9th, 2006, 7:57 PM   #4
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
what exactly is cerr. I've seen it, but we haven't been over it.
codylee270 is offline   Reply With Quote
Old Apr 9th, 2006, 8:09 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Standard error, akin to stderr. One gets 3 file handles/pointers by default when the program opens. Standard error typically goes to the console, same as stdout, but they may be separately redirected (as to a log file and an output file), particularly in the case of batch files.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 9th, 2006, 8:29 PM   #6
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
I've changed your code to use the 'stringstream' object. While it's still not robust it should give you an idea of how to extract numeric input. The major changes are in bold:

#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <iomanip>
#include <vector>
#include <cstdlib>

#include <sstream>

using namespace std;


int main()
{
	int id[10];
	vector<int> years;
	char sex[10];
	float wage[10];
	int yearsworked;
	ifstream infile;
	ofstream outfile;
	string disp1, disp2, filename, line;

	cout << "Please input the name of the file you wish to open: " ;
	getline(cin, filename);
	
	infile.open(filename.c_str());
	if(infile.fail())
	{
					  cout <<"\nThe file was not successfully opened." << endl;
					  exit(1);
	}
	getline(infile, disp1);
	getline(infile, disp2);
	
	vector<float> holder;
	float temp;
	
	while( getline(infile, line) )
	{
		 stringstream ss ( stringstream::in | stringstream::out );
		 ss << line;
		 ss >> temp;
		 if ( !ss ) { continue; }
		 holder.push_back( temp );
	}
	infile.close();
	
	cout << "\nThe file contained the following:" << endl;
	
	cout << disp1 << endl << disp2 << endl;
	for( int i = 0; i < holder.size(); ++i )
	{
			 cout << holder[i] << endl;
	}
	
	cout << "Press ENTER to exit program. . .";
	cin.sync();
	cin.get();
	return 0;
}
Cache is offline   Reply With Quote
Old Apr 9th, 2006, 8:27 PM   #7
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
Well,
I made Ooble's change above, and it now reads the files, and gives me the first 2 lables of the table correctly, but when i try to store the rest to an array, it gives me junk. How should i declare that array and the "line" variable that each line is first stored to so that it gives me exactly what the file contains?

and actually, i changed some code to look like this:

while(infile >> holder[i])
    {
                 i++;
    }
codylee270 is offline   Reply With Quote
Old Apr 9th, 2006, 8:36 PM   #8
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
hey cache,
I still get junk for output, not the actual lines from the file.
codylee270 is offline   Reply With Quote
Old Apr 9th, 2006, 8:39 PM   #9
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
Like I said, that should give you an idea. I don't know what's in your file.
Cache is offline   Reply With Quote
Old Apr 9th, 2006, 8:42 PM   #10
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
this is what's in the file:
Employee ID       Employee Sex         Numeric Wage        Years with Company
-----------       ------------         ------------        ------------------
       6162                  F         $      12.23                       355
    4654654                  M         $      23.60                         6

It easy to get the labels and ----- out and displayed, but I wanted to get the other lines, and get the crap out of em them and store them in separate arrays. But when try to get those lines from a file, it gives me junk.
codylee270 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:22 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC