0

I have 2 questions:

I am trying to take input from another file, initialize them into variables, then output the variables to another file. I am able to get all input into separate variables except for 1 line (the line should be a variable).

My input file contains this data:

557.9012043 0.673621489 7210984732 1.891092837
238 a 789.234 b
Yes Please
cannot wait for hawaii

The line "Yes Please" should be taken as a whole string, however, "Yes" and "Please" are getting separated.

My code for this is:

outFile << fixed << showpoint;
        outFile << setprecision(10);

        inFile >> w >> x >> y >> z >> int1 >> char1 >> float1 >> char2 >> string1
                >> word1 >> word2 >> word3 >> word4;

        outFile << w << endl;
        outFile << x << endl;
        outFile << y << endl;
        outFile << z << endl;
        outFile << float1 << endl;
        outFile << int1 << endl;
        outFile << char1 << endl;
        outFile << char2 << endl;
        outFile << string1 <<endl;
        outFile << word1 << endl;
        outFile << word2 << endl;
        outFile << word3 << endl;
        ouFile << word4 << endl;
}

When I run it, my outFile consists of:

557.9012043000
0.6736214890
7210984732.0000000000
1.8910928370
789.2340000000
238
a
b
Yes
Please
cannot
wait
for

How can I get the whole line, "Yes Please" assigned to my variable string1?

Question 2: My outFile has all of the extra 0's in the floating point variables because I set the precision. My goal, really, is to use the variables to solve equations. I would like the answers to the equations to only print 10 significant digits. How would I accomplish this task without the precision after the decimal being 10 digits?

I am very new to C++, so when if you answer, can you please explain why you gave the answer you did. I'm not trying to only receive answers, I want to learn. Thank you.

LogicStuff
  • 18,687
  • 6
  • 49
  • 70
ryanpback
  • 225
  • 2
  • 12
  • C++ does not read minds. Use `std::getline` for that line, but beware of [this](http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction). – LogicStuff Feb 03 '16 at 22:44
  • Possible duplicate of [reading a line from ifstream into a string variable](http://stackoverflow.com/questions/6663131/reading-a-line-from-ifstream-into-a-string-variable) – LogicStuff Feb 03 '16 at 22:47
  • Possible duplicate of "stackoverflow c++ read struct". – Thomas Matthews Feb 03 '16 at 23:16
  • @LogicStuff Sorry about that. I had read those, and I understand using getline(), I just don't understand how to use getline() in between inFile >> and another set of inFile >>. – ryanpback Feb 04 '16 at 19:25

3 Answers3

2

I solved the issue of getting a blank line after my getline() statement by adding myFile.ignore() before the getline() statement.

}
inFile >> w >> x >> y >> z >> int1 >> char1 >> float1 >> char2;
inFile.ignore();
getline(inFile, string1);
inFile >> word1 >> word2 >> word3 >> word4;
}

Now my string1 variable contains "Yes Please" as a whole string, they are no longer separated.

ryanpback
  • 225
  • 2
  • 12
0

I recommend using a struct for each record. Also implement an overloaded operator>> to extract the record from an input stream.

struct Record_Floats
{
  double values[4];
  friend std::istream& operator>>(std::istream& inp, Record_Floats& rf);
};
std::istream& operator>>(std::istream& inp, Record_Floats& rf)
{
  inp >> rf.values[0] >> rf.values[1] >> rf.values[2] >> rf.values[3];
  return inp;
};

struct Record_Number_Letter
{
  double number1;
  char   letter1;
  double number2;
  char   letter2;
  friend std::istream& operator>>(std::istream& inp, Record_Number_Letter& rnl);
};
std::istream&  
operator>>(std::istream& inp, Record_Number_Letter& rnl)
{
  inp >> rnl.number1;
  inp >> rnl.letter1;
  inp >> rnl.number2;
  inp >> rnl.letter2;
  return inp;
}

By overloading the stream extration operator>> you can do things like:

Record_Floats record1;
my_data_file >> record1; // Read an all numbers record.
Record_Number_Letter record2;
my_data_file >> record2; // Read a record of number letter number letter.

Edit 1: Inputting words and lines
The input of a word is accomplished by:

std::string word_text;
my_data_file >> word_text;  

The input of a text line is accomplished by:

std::string text_line;
std::getline(my_data_file, text_line);
Thomas Matthews
  • 52,985
  • 12
  • 85
  • 144
  • I'm not sure I understand structs at this point. I appreciate the time you have taken to explain your answer. I will do some research on them and reply. Sorry for the late response, work sure does get in the way sometimes. – ryanpback Feb 04 '16 at 19:51
0

Answer 1: Eventually all the data in the infile and the outfile are big strings. whether it writes to a word or a string, separators(space, new line tab, comma etc.) remain the same. Obviously if YesPlease were written without a space all would have been well but it's not and C++ cannot differ between the cases of the 3rd and 4th lines. In order to solve this you must get the entire line of Yes Please into the string so you need to use std::getline.

Answer 2:I'll just give you a general algorithm...implementing is not unfamiliar.

1.construct a function which gets a string and returns a fixed string.
2a. turns string it into an array of chars.
2b.in the array it finds the location of the floating point.
3.if there's no floating point->number is an integer or a word->leave as is
4.otherwise if the last char is '0' and its index is bigger than index of floating 
point then change last char to '\0'(truncate last zero)
5.Repeat 4 until last char is not '0'.if last char is '.' then truncate it as well.
6.return the new string without the zeros

7.prepare outFile for reading and outFile2 for writing
8.get line from outFile, apply function to line and put the returned string  at the end of 
outFile2.
9.repeat 8 for all rows in outFile

outFile2 will look like outFile but with all the redundant zeros truncated.

edit: I got stumbled here with the mixed use of >> and std::getline for question 1. question 2 algorithm remains valid since it's supposed to use only getline.

I know it's sort of cheating but it gives the desired result if you use the following code:

outFile << fixed << showpoint;
    outFile << setprecision(10);

    inFile >> w >> x >> y >> z >> int1 >> char1 >> float1 >> char2 >> 
            >> word1 >> word2 >> word3 >> word4 >> word5 >> word6;

    outFile << w << endl;
    outFile << x << endl;
    outFile << y << endl;
    outFile << z << endl;
    outFile << float1 << endl;
    outFile << int1 << endl;
    outFile << char1 << endl;
    outFile << char2 << endl;
    outFile << word1;
    outFile << word2 << endl;
    outFile << word3 << endl;
    outFile << word4 << endl;
    outFile << word5 << endl;
    outFile << word6 << endl;
}

last edit:totally forgot about the use of ignore()...I see you already found the answer

Ilan Kutsman
  • 429
  • 3
  • 9
  • Great, I will try to implement that into my code. I understand the use of std::getline, but would I go about using it within the inFile >> for the other insertions. How would I declare which line I want? – ryanpback Feb 04 '16 at 19:55
  • In other words, if I used inFile >> for variables "w thru char2", how do I specify that I want std::getline to read Yes Please, and then use inFile >> for variables "word1 thru word 4"? – ryanpback Feb 04 '16 at 20:03