can't fill an array using a file - c++

Good morning all.
I'm starting to learn c++ and i'm trying to make a prog that converts from one currency to another.
I made a text file "currency.txt" where i have all currencies, one after another, each with 4 lines:
Country, Currency description, Currency code, Exchange rate.
So far i made the following code:
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
struct currency {
string country;
string coin;
string code;
double rate;
};
int count_lines ( ifstream &myfile ){
string line;
int lines = 0;
if ( myfile.is_open() ){
while ( getline( myfile, line ) ){
++lines;
}
} else cout << "error";
myfile.seekg (0, ios::beg);
return lines;
}
void read_coins ( currency coins[], ifstream &myfile, int lines) {
string line;
if ( myfile.is_open() ){
for ( int n=0; n<lines; n++){
getline( myfile, line );
coins[n].country = line;
getline( myfile, line );
coins[n].coin = line;
getline( myfile, line );
coins[n].code = line;
getline( myfile, line );
stringstream(line) >> coins[n].rate;
}
} else cout << "error";
myfile.seekg (0, ios::beg);
}
int main(){
ifstream myfile ( "currency.txt" );
int lines;
lines = count_lines ( myfile )/4;
currency coins [lines];
read_coins (coins, myfile, lines);
for (int n=0; n<lines; n++){
cout << coins[n].country << '\t';
cout << coins[n].coin << '\t';
cout << coins[n].code << '\t';
cout << coins[n].rate << endl;
}
myfile.close ();
return 0;
}
But it's just not working. If i open the file inside all of the functions it works, but not like this.
I know there must be a problem for sure but i just can't figure it ou yet.
And i have another problem: the exchage rates have 10 decimal digits, but when i put it in my coins[n].rate, it comes only with 5 ou 6 decimal digits. Is there anyway to get all 10?
Can somebody help me please?
Thanks

If you're not using C++11, seekg doesn't reset the end-of-file state of the file.
Add myfile.clear() before seeking to the beginning.
The output of floating point numbers depends on the current precision of the stream.
The default is six (6).
Add
#include <iomanip>
and
std::cout << std::setprecision(10);
before output.
(But remember that floating-point is inexact in itself, so you won't necessarily get exactly the same number as in the file - only the closest one possible.)

Related

Using the getline function in C++ to extract certain characters

I need to get only specific characters from a text file. I am using the getline() function in C++. my compiler keeps giving me the error that there is no matching member function call for getline(), how can I fix that?
I'm trying to pull last names and scores from the file.
the file looks like:
Weems 50 60
Dale 51 60
Richards 57 60
...
Here's the code I'm trying:
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
int main ()
{
//input variables
float GradeScore;
float TotalPoints;
float GradePercent;
string LastName;
ifstream myFile;
//open file
myFile.open ("/Users/ravenlawrence/Documents/TestGrades.rtf",ios::in);
// if file is open
if (myFile.is_open()) {
while(!myFile.eof()) {
string data;
getline(myFile,data); //reading data on line
myFile.getline(LastName, ' ');//storing data in LastName
myFile.getLine(GradeScore,' ');//storing data in GradeScore
myFile.getLine(TotalPoints,' ');//storing data in Total Points
cout << LastName << endl;
// cout<<data<<endl; //print it out
}
}
return 0;
}
Begin with a design, breaking down the work into small steps:
open file
loop, reading line from file while more lines
split line into fields
convert fields into variables
display variables
Now tackle each step
// open file
ifstream myFile ("/Users/ravenlawrence/Documents/TestGrades.rtf",ios::in);
if( ! myFile ) {
cerr << "cannot open file\n";
exit(1);
}
//loop, reading line from file while more lines
string data;
while( getline( myFile, data ) ) {
// split line into fields
std::stringstream sst(data);
std::string a;
std::vector<string> vfield;
while( getline( sst, a, ' ' ) )
vfield.push_back(a);
// ignore lines that do not contain exactly three fields
if( vfield.size() != 3 )
continue;
//convert fields into variables
LastName = vfield[0];
GradeScore = atof( vfield[1].c_str() );
TotalPoints = atof( vfield[2].c_str() );
// display
...
}
You do not need to use the function getline here, you can read file word by word.Secondly, you need to close the file after it reaches eof. Here is the code:
int main()
{
//input variables
float GradeScore;
float TotalPoints;
float GradePercent;
string LastName;
ifstream myFile;
//open file
myFile.open("check.txt", ios::in);
// if file is open
if (myFile.is_open()) {
while (!myFile.eof()) {
myFile >> LastName;//storing data in LastName
myFile >> GradeScore;//storing data in GradeScore
myFile >> TotalPoints;//storing data in Total Points
cout << LastName << endl;
// cout<<data<<endl; //print it out
}
myFile.close();
}
system("pause");
return 0;
}
Rather than checking if file is open or not a better approach is to check if file exist or not:
if(!myfile)
{
cout<<"error!file donot exist";
}

C++ reading data into array of structures

I found a way to read the data into my array structs but i cannot control how many it reads. It will read all 14 entries in the input file but after it has read the 14 it continues for a couple lines then stops. However, if i place the code to read data inside any loop whether while or for, it starts printing huge numbers instead of what is contained in the file. Does anyone know why this happens? Is there a way around it
Input file looks as follows( exclude the dot):
There are 14 sets of data in the file. So, as shown below, set 1 would include georgia 76.4 10, then north carolina 95.5 20, and so on till 14 sets.
Data inside input file looks like this:
georgia
76.4 10
north carolina
95.5 20
and so on.
Need to print data to the screen like:
georgia 76.4 10
north carolina 95.5 20
etc.
The problem lies in my code were i attempt and horribly fail to read into the input file.
#include <iostream>
#include <fstream>
#include <cstdlib>
struct ATdata
{
string state;
double miles;
int shelters;
};
int readData( ifstream& input, struct ATdata data[] );
int main()
{
ifstream input;
char filename[256];
ATdata data[14];
int i;
cout << "Enter input file name: ";
cin >> filename;
input.open( filename );
if ( input.fail() )
{
cout << "Input file does not exist." << endl;
exit(1);
}
readData( input, data );
cout << i << endl;
return(0);
}
int readData( ifstream& input, struct ATdata data[] )
{
int i, j;
getline( input, data[i].state, '\t' );
input.ignore( 14, '\n' );
input >> data[i].miles;
input.ignore( 1, ' ' );
input >> data[i].shelters;
for ( j = 0; j < 14; j++ )
{
cout << data[j].state << data[j].miles << data[j].shelters << endl;
}
}
I apologize for all the info trying to be as clear as can be as to not look more idiotic.
Thank you have a nice day.
getline() takes an ostream& and a string. Since your types are not string, it gives you an error. I recommend reading in your input like this:
for (int i = 0; i < 14; i++ )
{
input >> data[i].state >> data[i].miles >> data[i].shelters;
}
BTW, your state is a char, that's 1 character. Make it an std::string.
The best way to get a whole line is to use std::getline. However, if you try to mix unformatted input with formatted input then it gets a bit tricky. You need lots of ignore calls and other guff. I think it is always clearer to read a whole line at a time, and then use a stringstream to parse that line.
Try this:
size_t readData( ifstream& input, struct ATdata data[], size_t max_items )
{
size_t i;
for ( i = 0; i < max_items; )
{
ATdata item;
getline(input, item.state);
string line_2;
if ( ! getline(input, line_2) )
break;
istringstream iss(line_2);
if ( ! (iss >> item.miles >> item.shelters) )
{
// skip malformed entry (or you could abort instead)
continue;
}
data[i++] = item;
}
return i;
}
Call it with:
size_t num_items = readData(input, data, sizeof data / sizeof data[0]);

error reading data from input file to array

The input file contains 14 state initials (TN,CA,NB,FL,etc..) that is to be rad into the array. The code below clears compiler but when i tell the program the filename it shoots out a bunch of blank spaces with two spaces containing some fuzz and a third contain a '#' symbol. i assume the problem is with my function not entirely sure what specifically though any help greatly appreciated!
input file set up with state initials one on top of the other:
TN
PA
KY
MN
CA
and so on
void readstate( ifstream& input, string []);
int main()
{
string stateInitials[14];
char filename[256];
ifstream input;
cout << "Enter file name: ";
cin >> filename;
input.open( filename );
if ( input.fail())
{
cout << " file open fail" << endl;
}
readstate ( input, stateInitials);
input.close();
return (0);
}
void readstate ( ifstream& input, string stateInitials[])
{
int count;
for ( count = 0; count <= MAX_ENTRIES; count++)
{
input >> stateInitials[count];
cout << stateInitials[count] << endl;
}
}
You are treating a character array as though it were a string array.
While you can hack put the strings next to each other inside the same char array, that is not the standard way it is done. Here is a modified version of your code, which creates one char[] to hold each initial.
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <string.h>
#define MAX_ENTRIES 14
using namespace std;
void readstate( ifstream& input, char* []);
int main()
{
char** stateInitials = new char*[14];
char filename[256];
ifstream input;
cout << "Enter file name: ";
cin >> filename;
input.open( filename );
if ( input.fail())
{
cout << " file open fail" << endl;
}
readstate ( input, stateInitials);
// After you are done, you should clean up
for ( int i = 0; i <= MAX_ENTRIES; i++) delete stateInitials[i];
delete stateInitials;
return (0);
}
void readstate ( ifstream& input, char* stateInitials[])
{
int count;
string temp_buf;
for ( count = 0; count <= MAX_ENTRIES; count++)
{
stateInitials[count] = new char[3];
input >> temp_buf;
memcpy(stateInitials[count], temp_buf.c_str(), 3);
cout << stateInitials[count] << endl;
}
}

Program not counting number of records [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I wrote a program that reads to one file, writes to another and prints to the screen. In other words, it is supposed to do three things (i.e. read, write and print).
The program is also supposed to count the number of records in a file named "Chap_11_employee_data.txt". It is just displaying the following, however.
**Total Records 0**
The End
When it writes to a file, it also displays weird numbers such as -858993460. I have tried almost everything, which led me to register an account here. My code is listed below as well as the file I am trying to read from and the file I am trying to write to.
Thank you very much for your guy's time.
Code:
#include <iostream>
#include <fstream>
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;
using std::setw;
using std::ios;
using std::ifstream;
using std::ofstream;
const int EMPLOYEES = 20;
const int MAX = 21;
int ReadData( ifstream &inFile, ofstream &outFile, char name[][MAX], int age[] );
void WriteOutputFile( ofstream &outFile, char name[ ][MAX], int age[ ], int counter );
void PrintTotalsAndSummary( ofstream &out, int totalRecords );
int main()
{
char name[EMPLOYEES][MAX];
int age[EMPLOYEES];
int record_counter(0);
ifstream inFile;
ofstream outFile( "Chap_11_Report.txt" );
inFile.open( "Chap_11_employee_data.txt" );
if ( inFile.is_open() )
{
record_counter = ReadData( inFile, outFile, name, age );
inFile.close();
if( outFile.is_open() )
{
WriteOutputFile( outFile, name, age, record_counter );
PrintTotalsAndSummary( outFile, record_counter );
outFile.close();
}
else
{
cout << "Trouble Opening File";
cout << "\n\n\t\t ** About to EXIT NOW! ** ";
}
}
else
{
cout << "Trouble Opening File";
cout << "\n\n\t\t ** About to EXIT NOW! ** ";
}
return 0;
}
int ReadData( ifstream & inFile, ofstream & outFile, char name[][MAX], int age[] )
{
int counter = 0;
inFile >> name[counter] >> age[counter]; // Priming Read
while ( !inFile.eof() )
{
cout << setiosflags( ios::left ) << setw( 25 )
<< name[counter] << resetiosflags( ios::left )
<< setw( 4 ) << age [counter] << endl;
counter++;
inFile >> name[counter] >> age[counter];
}
return counter;
}
void WriteOutputFile( ofstream &outFile, char name[][MAX], int age[], int counter)
{
outFile << " Here is the Output File" << endl;
for ( int r = 0; r <= counter; r++ )
{
outFile << setiosflags( ios::left ) << setw( 25 )
<< name[r] << setw( 4 )
<< resetiosflags( ios::left ) << age[r]
<< endl;
}
}
void PrintTotalsAndSummary( ofstream &outFile, int totalRecords )
{
// To screen
cout << "\n\n\t** Total Records: " << totalRecords << " **\n"
<< "\t\t The End \n";
// To file
outFile << "\n\n\t** Total Records: " << totalRecords << " **\n"
<< "\t\t The End \n";
}
File I am reading from (Chap_11_employee_data.txt):
"Alexis","Blough",1-1921,"CEO"
"Bill","Pay",1-7711,"Accounting"
"Willy","Makit",4-1595,"Sales"
"Marie","Murray",1-4986,"MIS"
"Cal","Caldwellowinski",5-0911,"MIS"
"Jamie","Johanasola",5-9999,"Marketing"
File I am writing to (Chap_11_Report.txt):
Here is the Output File
-858993460
** Total Records: 0 **
The End
Here is your input statement...
inFile >> name[counter] >> age[counter];
And the output file format is this...
"Alexis","Blough",1-1921,"CEO"
"Bill","Pay",1-7711,"Accounting"
The input statement reads the name till it encounters a space or an endline character. So in the first case it would read the whole line as there is no space in between the words. Next on, it would take input of age which is an int. It would start taking input of age from the second line, which is full of characters from the beginning. A character in integer stream is undefined behavior. That is the reason you are getting wrong results.
The problem is the mismatch of the file format with the input format. Try to synchronise them.
For reading white spaces as well, use inFile.get() or inFile.getline().
Your read has one problem:
while ( !inFile.eof() )
The problem is that the stream can go bad in a way that is not eof(). If this happens you will enter an infinite loop. So really you should check for bad().
while ( !inFile.bad() ) // check for eof and other error cases.
Normally this test is completely wrong. But you have managed to get around this by doing the following:
<Get Input>
while ( !inFile.bad() )
{
< DO WORK>
<Get Input>
}
The same affect can be achieved (in a more idiomatic way) with:
while ( <Get Input> )
{
< DO WORK>
}
The read (using >> or std::getline()) returns a reference to a stream. When this is used in a boolean context (like a while loop test) it is converted to bool (actually bool like for the language lawyers) that represents the state of the stream which is obtained by using bad. Thus the loop body is only entered if the read actually worked.
The input stream operators are very simplistic and any failure on input will set an internal failure bit inside the stream causing them to sieze up and not provide further input until the failure bit is reset.
Given the input you have provided this will fail:
inFile >> name[counter] >> age[counter];
The variable name[counter] represents a C-String and the the input operator >> when applied to a C-String will read until the first white space character (in this case newline). So here we assign:
"Alexis","Blough",1-1921,"CEO"
to this value.
We then try and continue reading using age[counter] an integer value. This tries to read an integer from:
"Bill","Pay",1-7711,"Accounting"
This will fail as the character '"' is not a digit.
What you should be doing is:
std::string name;
std::getline(inFile, name, ','); // Read upto the ',' character (and discard it)
// You may want to remove the quotes here.
int age = 0;
// Age is not defined in the input so set it manually.
std::string line;
std::getline(inFile, line); // Read and ignore the rest of the line.

Detect newline byte from filestream

I'm trying to collect information from a textfile which contains names of organisations (without spaces) and floating integers. I want to store this information in an array structure.
The problem I'm having so far is collecting the information. Here is a sample of the textfile:
CBA 12.3 4.5 7.5 2.9 4.1
TLS 3.9 1 8.6 12.8 4.9
I can have up to 128 different numbers for each organisation, and up to 200 organisations in the textfile.
This is what my structure looks like so far:
struct callCentre
{
char name[256];
float data[20];
};
My main:
int main()
{
callCentre aCentre[10];
getdata(aCentre);
calcdata(aCentre);
printdata(aCentre);
return 0;
}
And the getdata function:
void getdata(callCentre aCentre[])
{
ifstream ins;
char dataset[20];
cout << "Enter the name of the data file: ";
cin >> dataset;
ins.open(dataset);
if(ins.good())
{
while(ins.good())
{
ins >> aCentre[c].name;
for(int i = 0; i < MAX; i++)
{
ins >> aCentre[c].data[i];
if(ins == '\n')
break;
}
c++;
}
}
else
{
cout << "Data files couldnt be found." << endl;
}
ins.close();
}
What I'm trying to achieve in my getdata function is this: store the organisation name first into the structure, then read each float into the data array until the program detects a newline byte. However, so far my check for the newline byte isn't working.
Assume that variables c and MAX are already defined.
How should I go about this properly?
The >> operator treats whitespace as a delimiter, and that includes newlines, so it just eats those and you never see them.
You need to read lines and then chop the lines up. The following bit of hackery illustrates the basic idea:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
string line;
while( getline( cin, line ) ) {
istringstream is( line );
string cs;
is >> cs;
double vals[10];
int i = 0;
while( is >> vals[i] ) {
i++;
}
cout << "CS: " << cs;
for ( int j = 0; j < i; j++ ) {
cout << " " << vals[j];
}
cout << endl;
}
}
char byte = ins.peek();
Or
if(ins.peek() == '\n') break;
(Edit): You'll want to also check for an eof after your peek(), because some files may not have a ending newline.
I'd like to point out that you might want to consider using a vector<callCentre> instead of a static array. If your input file length exceeds the capacity of the array, you'll walk all over the stack.
I would read the file, one line after another and parse each line individually for the values:
std::string line;
while (std::getline(ins, line)) {
std::istringstream sline(line);
sline >> aCentre[c].name;
int i = 0;
while (sline >> aCentre[c].data[i])
i++;
c++;
}