Okay, I am not very experienced with programming, but I have an assignment to create a c++ program that uses numerical methods to calculate the temperature of a mixture of three substances, based on the enthalpy and percent of each substance in the mixture. its basically a polynomial of h = a1*T + a2*T^2 + ... up to a6. These coefficents a1 through a6 are given in a table, for each of H20, H2, and O2. My program needs to be able to read the substance names and the values of the coefficients from a .dat file so that I can use the coefficients for my equations. That's what I need help with. How can I get the program to input the substance names and coefficient values into an array so I can use them in my equations? Sorry for the novel but I tried to give as much context as possible.
below is exactly what is in my .dat file, and what I am trying to put in an array. The substance name is first, followed by a1, a2, etc.
H2O 406598.40 440.77751 -.12006604 .000015305539 -.00000000072544769 -4475789700
H2 50815.714 9.9343506 -.000027849704 -.00000035332966 .000000000041898079 -14329128
O2 961091.64 199.15972 -.052736240 .00000897950410 -.00000000063609681 -318699310
this is my source code so far, but its not working, and I'm pretty lost.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
double myArray[21];
ifstream file("thermo2.dat");
if (file.is_open())
{
for (int i = 0; i < 21; ++i)
{
file >> myArray[i];
}
}
else
{
cout << "the file did not open";
}
for (int i = 0; i < 21; ++i)
{
cout << " " << myArray[i];
}
return 0;
}
thanks!
EDIT: started trying to work with an array of structs....I keep getting an error: no matching function for call to 'getline(std::ifstream&, double&, char)'. heres the code:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
struct Data
{
string species;
double a1, a2, a3, a4, a5, a6;
};
int main()
{
ifstream fin;
fin.open("thermo2.dat");
if (fin.fail())
{
cout << "Failed to open file" << endl;
}
Data * database = new Data[3];
string line;
for(int i = 0; i < 3; i++)
{
getline(fin, database[i].species, '\t');
getline(fin, database[i].a1, '\t');
getline(fin, database[i].a2, '\t');
getline(fin, database[i].a3, '\t');
getline(fin, database[i].a4, '\t');
getline(fin, database[i].a5, '\t');
getline(fin, database[i].a6, '\t');
}
system("pause");
return 0;
}
Declare your structure as:
struct Data
{
string species;
double a[6];
}
And read as below:
for(int i = 0; i < 3; i++) {
fin >> database[i].species;
for (int j = 0; j < 6; j++) {
fin >> database[i].a[j];
}
}
My suggestion:
Create a struct to hold the data for each material.
struct Material
{
std::string name;
double coeffcients[6];
};
Create a function to read one Material from a stream.
std::istream& operator>>(std::istream& in, Material& mat)
{
// Read the name.
in >> mat.name;
// If there was an error, return.
// Let the calling function deal with errors.
if (!in)
{
return in;
}
// Read the coefficients.
for (int i = 0; i < 6; ++i )
{
in >> mat.coefficients[i];
if (!in)
{
return in;
}
}
return in;
};
In the main function, write the driving code.
int main()
{
// Create a vector of materials.
std::vector<Material> materials;
// Open the input file.
ifstream file("thermo2.dat");
// Read Materials the file in a loop and
// add them to the vector.
Material mat;
while (file >> mat)
{
materials.push_back(mat);
}
// Now use the vector of Materials anyway you like.
// Done with main.
return 0;
}
Related
Hello and thanks to all who considered reading. I recently started learning c++ in a class and I am just now learning file input and output. I have the following program,
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
class Cups{
string owner;
public:
string type;
float size;
Cups(string owner, string type, float size){
this->owner = owner;
this->type = type;
this->size = size;
}
string get_owner(){
return owner;
}
};
vector<Cups> extract(string filename){
ifstream inFile;
string line;
string owner, type;
float size;
vector<Cups> o;
inFile.open(filename);
if(!inFile.is_open()){
cout<<"Error, file not found..."<<endl;
exit(1);
}
while(!inFile.eof()){
getline(inFile, line);
stringstream ss(line);
getline(ss, owner, ',');
getline(ss, type);
if(type.compare("Short")==0){
size = 1.0;
}
else if(type.compare("Tall")==0){
size = 1.5;
}
else if(type.compare("Grande")==0){
size = 2.0;
}
else if(type.compare("Venti")==0){
size = 2.5;
}
else{
cout<<"Error, "<<owner<<"'s cup size not found."<<endl;
exit(1);
}
o.push_back(Cups(owner, type, size));
}
inFile.close();
return o;
}
int main(int argc, char** argv){
if(argc < 2){
cout<<"Please compile with and orders file."<<endl;
exit(1);
}
int i;
vector<Cups> orders = extract(argv[1]);
for(i = 0; i < orders.size(); i++){
cout<<orders[i].get_owner()<<", "<<orders[i].type<<endl;
cout<<orders[i].size<<endl;
}
}
with the following file, input as an executable argument
Natassa,Grande
Demy,Tall
Elena,Short
The program outputs the warning when you compile without a file just fine, however when running it, it outputs
Natassa, Grande
2
Demy, Tall
1.5
Elena, Short
1
, Short
1
I've tried running it in gdbonline and there seems to be a problem with my extraction perhaps, but I can't figure it out. Thanks for all who bothered to read and a bigger thanks to any who help.
Edit: Added the not symbol ot the inFile.eof() check, but still getting the ghost line shown above. I would post to codereview but this code is still not working correctly.
This is an assignment that required me to use ifstream to stream a CSV file. this csv file contains 52 state names and amount of different resources used by each state. for example:
Alabama,410.20,715.70,169.40,18.00,44.90,309.10,11.90,417.30,64.50,167.40,23.70,0.10,0.40,0.00
then I need to prompt the user to type the state name and the output is the percentage of resources used.
I created a struct containing a string type and an array to store the value of each state and created an array of struct to store multiple state's data, but I am not sure whether my code is right, and I want to know how to access other data, such as the data store in my double array, when the user input a state name.
here is my code:
struct statData
{
string statename;
double StatDataNumber[14];
}DataStruc[51];
int main()
{
ifstream indata;
string line;
statData State;
State.statename;
statData Consumption;
Consumption.StatDataNumber;
indata.open("Data2016.csv"); //opening file
if (indata.fail()) //fail safe
{
cout << "Fail to open file";
exit(1);
}
getline(indata, line); //skipping the first line of the csv file
int i;
int N = 0;
int NLoop;
int Loop = 0;
string InvertValueBefore;
double InvertValueAfter;
char comma;
while (indata.eof()) // before file reache the end
{
for (NLoop = 0; NLoop < 51; NLoop++) // struct array loop
{
{
getline(indata, DataStruc[Loop].statename, ',');// getting statename
for (i = 0; i <= 12; i++) // each group of data, except last
{
indata >> DataStruc[Loop].StatDataNumber[N] >> comma;// storing data in struct
N++;
}
getline(indata, InvertValueBefore); // store last value as string
InvertValueAfter = stoi(InvertValueBefore); // convert it into double
InvertValueAfter = DataStruc[Loop].StatDataNumber[N]; // store it in array of struct
}
Loop++;
}
}
ReadData();
return 0;
}
void ReadData (ifstream& indata , statData DataStruc[] )
{
int i;
string input;
bool stayinloop = true;
cout << "Enter a statename or 'q' to quit\n";
getline(cin, input);
while (stayinloop == true)
{
if (input == "Alabama")
DataStruc[i].statename == "Alabama";
DataStruc[i].StatDataNumber[]
}
}
this code is not finished. Please let me know if you spot any other error. Thank you!
Your code is fine. However, certain points:
1. You just need to get rid of certain variables which are not required.
2. The "eof" function is used to identify if the end of file is reached. For which, you need to use while (!indata.eof()).
3. The "ReadData" method should appear before the main function, however, if you want to have your functions after the main function then first you need to define your function declaration before the main function (i.e. before main function, you can put "void ReadData (ifstream& indata , statData DataStruc[]);"), afterwards you can define your function.
Below is a working version of your requirements.
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
using namespace std;
struct statData
{
string statename;
double StatDataNumber[3];
}DataStruc[2];
void ReadData (ifstream& indata , statData DataStruc[])
{
string input;
bool stayinloop = true;
while (stayinloop)
{
cout << "\nEnter a statename or 'q' to quit\n";
getline(cin, input);
for (int i = 0 ; i < 2; i++)
{
if (input == DataStruc[i].statename)
{
for(int j = 0 ; j < 3; j++)
{
cout << DataStruc[i].StatDataNumber[j] << ',';
}
}
else if(input == "q")
{
stayinloop = false;
}
}
}
}
int main()
{
ifstream indata;
string tempData = "";
string line;
string InvertValueBefore = "";
double InvertValueAfter = 0.0;
char comma = ',';
indata.open("test.csv"); //opening file
if (indata.fail()) //fail safe
{
cout << "Fail to open file";
}
getline(indata, line); //skipping the first line of the csv file
while (!indata.eof()) // before file reach the end
{
for (int NLoop = 0; NLoop < 2; NLoop++) // struct array loop
{
{
getline(indata, DataStruc[NLoop].statename, comma);// getting statename
for (int i = 0; i < 2; i++) // each group of data, except last
{
getline(indata, tempData, comma);
DataStruc[NLoop].StatDataNumber[i] = atof(tempData.c_str());
}
getline(indata, InvertValueBefore); // store last value as string
InvertValueAfter = atof(InvertValueBefore.c_str()); // convert it into double
DataStruc[NLoop].StatDataNumber[2] = InvertValueAfter;
}
}
}
ReadData(indata, DataStruc);
return 0;
}
I'm currently writting a program where I try to filter extra spaces so if there are more than 1 spaces in a row, I discard the rest leaving only one
But this is only the first step because the aim of the program is to parse a txt file with mips assembly instructions.
So far I've opened the file, stored the content in a vector and then stored the vector content in an array. Then I check, if you find a char 2 times in a row shift the array to the left.
The problem is that the code works well for any other letter, except for the space character. (On the code below I test it with the 'D' character and it works)
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;
class myFile {
vector<string> myVector;
public:
void FileOpening();
void file_filter();
};
void myFile::FileOpening() {
string getcontent;
ifstream openfile; //creating an object so we can open files
char filename[50];
int i = 0;
cout << "Enter the name of the file you wish to open: ";
cin.getline(filename, 50); //whatever name file the user enters, it's going to be stored in filename
openfile.open(filename); //opening the file with the object I created
if (!openfile.is_open()) //if the file is not opened, exit the program
{
cout << "File is not opened! Exiting the program.";
exit(EXIT_FAILURE);
};
while (!openfile.eof()) //as long as it's not the end of the file do..
{
getline(openfile, getcontent); //get the whole text line and store it in the getcontent variable
myVector.push_back(getcontent);
i++;
}
}
void myFile::file_filter() {
unsigned int i = 0, j = 0, flag = 0, NewLineSize, k, r;
string Arr[myVector.size()];
for (i = 0; i < myVector.size(); i++) {
Arr[i] = myVector[i];
}
//removing extra spaces,extra line change
for (i = 0; i < myVector.size(); i++) {
cout << "LINE SIZE" << myVector[i].size() << endl;
for (j = 0; j < myVector[i].size(); j++) {
//If I try with this character for example,
//it works (Meaning that it successfully discards extra 'Ds' leaving only one.
// But if I replace it with ' ', it won't work. It gets out of the loop as soon
//as it detects 2 consecutive spaces.
if ((Arr[i][j] == 'D') && (Arr[i][j + 1] == 'D')) {
for (k = j; k < myVector[i].size(); k++) {
Arr[i][k] = Arr[i][k + 1];
flag = 0;
j--;
}
}
}
}
for (i = 0; i < myVector.size(); i++) {
for (j = 0; j < myVector[i].size(); j++) //edw diapernw tin kathe entoli
{
cout << Arr[i][j];
}
}
}
int main() {
myFile myfile;
myfile.FileOpening();
myfile.file_filter();
}
My question is, why does it work with all the characters except the space one, and how do I fix this?
Thanks in advace.
Wow. Many lines of code. I can only recomend to learn more about the STL and algorithms.
You can read the complete file into a vector using the vectors "range"-constructor and std::istream_iterator. Then you can replace one or more spaces in a string by using a std::regex. This is really not complicated.
In the below example, I do all the work, with 2 lines of code in function main. Please have a look:
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <string>
#include <fstream>
#include <regex>
using LineBasedTextFile = std::vector<std::string>;
class CompleteLine { // Proxy for the input Iterator
public:
// Overload extractor. Read a complete line
friend std::istream& operator>>(std::istream& is, CompleteLine& cl) { std::getline(is, cl.completeLine); return is; }
// Cast the type 'CompleteLine' to std::string
operator std::string() const { return completeLine; }
protected:
// Temporary to hold the read string
std::string completeLine{};
};
int main()
{
// Open the input file
std::ifstream inputFile("r:\\input.txt");
if (inputFile)
{
// This vector will hold all lines of the file. Read the complete file into the vector through its range constructor
LineBasedTextFile text{ std::istream_iterator<CompleteLine>(inputFile), std::istream_iterator<CompleteLine>() };
// Replace all "more-than-one" spaces by one space
std::for_each(text.begin(), text.end(), [](std::string& s) { s = std::regex_replace(s, std::regex("[\\ ]+"), " "); });
// For Debug purposes. Print Result to std::out
std::copy(text.begin(), text.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
}
return 0;
}
I hope, I could give you some idea on how to proceed.
My name is Faith and I am a beginner programmer in C++. I am working on a project where I have to read in two file and be able to separate the items in each file to its own variable. The first file has two pieces of information separated by "," and the second has three pieces of information. So far, I think I've done well with reading the files and getting each line in the file. Also I've separated each item by "," now I am trying to store those items in its on variable. Please Help! I've tried starting this project over multiple times and this is the only method I know how to implement to do this.
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdlib.h>
using namespace std;
class Manufactuer{
string upcode;
string company_name;
public:
void setupcode(string value){
upcode= value; }
void setcompany_name(string value){
company_name= value; }
string getupcode(){ return upcode;}
string getcompany_name(){return company_name;}
};
class Products{
string product_num;
string quantity;
string product_name;
public:
void setproduct_num(string value){
product_num= value; }
void setproduct_name(string value){
product_name= value; }
void setquantity(string value){
quantity = value;}
string getproduct_num(){ return product_num;}
string getquantity(){return quantity;}
string getproduct_name(){return product_name;}
};
int main(int argc, const char * argv[]) {
//opens the csv file
std::ifstream mccodesfile;
std::ifstream salesfile;
mccodesfile.open("mccodes.csv");
if(!mccodesfile){// file couldn't be opened
cout<<"Failed: file could not be opened"<<endl<<"Press Enter to Close:";
cin.get();
return 0;
}else
cout<<"Successfully opened file!"<<endl;
salesfile.open("sales.csv");
if(!salesfile){// file couldn't be opened
cout<<"Failed: file could not be opened"<<endl<<"Press Enter to Close:";
cin.get();
return 0;
}else
cout<<"Successfully opened file!"<<endl;
Manufactuer* upccodes;
int count; //how many elements in the array
int size; //how large the array
count = 0;
size = 2;
upccodes= (Manufactuer*)malloc(size*sizeof(Manufactuer)); //Malloc dynamatically reserve memory for variable pointer
string line;
while (getline(mccodesfile,line)) {// Taking every line from the file and putting in the variable line
Manufactuer newcode = Manufactuer(); //creating a upcode object
istringstream ss(line);
string token; //setting up split values
bool haveReadUPCode = false;
while (getline(ss,token,',')){// Separated values by comma
if(!haveReadUPCode){
newcode.setupcode(token);
haveReadUPCode = true;
}else{
newcode.setcompany_name(token);
}
}
if (count == size){
size = size * 2;
upccodes= (Manufactuer*)realloc(upccodes,size*sizeof(Manufactuer)); //Double the size while keeping the same elements
}
upccodes[count++]= newcode;
// cout<<line<<endl;
}
for (int i = 0; i<count; i++){
// cout<<upccodes[i].getcompany_name()<<endl; //Prints Manufactuers name--Works!
}
for (int i=0; i<count; i++) {
// cout<<upccodes[i].getupcode()<<endl; //Prints UPCcodes--Works
}
Products* product;
product= (Products*)malloc(size*sizeof(Products)); //Malloc dynamatically reserve memory for variable pointer
while(getline(salesfile, line)){{// Taking every line from the file and putting in the variable line
Products newProduct = Products(); //creating a product object
istringstream ss(line);
string token; //setting up split values
bool haveReadProduct = false;
while (getline(ss,token,',')){// Separated values by comma
if(!haveReadProduct){
newProduct.setproduct_num(token);
haveReadProduct = true;
} else{
newProduct.setproduct_name(token);
newProduct.setquantity(token);
}
}
if (count == size){
size = size * 2;
product= (Products*)realloc(product,size*sizeof(Products)); //Double the size while keeping the same elements
}
product[count++]= newProduct;
//cout<<line<<endl; //LINES ARE Printing!
}
}
for (int i = 0; i<count; i++){
// cout<<product[i].getproduct_name()<<endl; //Prints Product name--Works
}
for (int i = 0; i<count; i++){
//cout<<product[i].getproduct_num()<<endl; //Prints Product number--Works
}
}
I save a boolean matrix(ROW*ROW) to .txt file (0,1 format).
How could I read the matrix from the file? I wrote code below, but I compare the read results with the file, and the array results don't match the file. Could anyone tell me where I wrote wrong? Or are there any easier ways to read a matrix file?
bool **results = new bool*[ROW];
for(int i=0;i<ROW;i++){
results[i] = new bool[ROW];
}
ifstream infile;
infile.open ("FRM.txt");
string line;
int row=0;
if (infile.is_open())
{
while ( infile.good() )
{
getline(infile,line);
istringstream iss(line);
int col=0;
while(iss)
{
string word;
iss >> word;
int number = atoi(word.c_str());
if(number==1){
results[row][col] = true;
}
else{
results[row][col] = false;
}
col++;
}
row++;
}
}
infile.close();
Representing a matrix as a nested array is usually a bad idea. Use a linear array instead and do the index-juggling through a macro or inline function.
I would go for something like this:
#include <algorithm>
#include <cmath>
#include <cstdint> // uint8_t requires C++11
#include <iterator>
#include <vector>
// don't use vector<bool>, it creates all sorts of problems
std::vector<uint8_t> results;
{
std::ifstream infile('FRM.txt', std::ifstream::in);
if(infile.is_open() && infile.good())
{
std::istream_iterator<uint8_t> first(infile), last;
copy(first, last, std::back_inserter(results));
}
}
// only if you know the matrix is square! error checking strongly advised!
size_t nrows = size_t(std::sqrt(results.size()));
// accessing element (i,j) is then as easy as
size_t i = 2, j = 3; // assuming nrows > i,j
bool a_ij = results[i*rows+j];