0

When I run my program in Xcode it shows the build was successful, but in the console it cycles infinitely and it shows infinite rows of only three 0's ("0 0 0"). When I took the last variable of type double it showed the same problem but instead of rows of three 0's it was rows of two 0's. My guess is that it has something to do with the variables of type double. I tried the exact same code (copy-paste) in an online compiler (repl.it) and it did work accordingly.

I tried to put different numbers in the .txt file and also changed the names of the variables

#include <iostream>
#include <fstream>

using namespace std;

int main() {
    string sID, sDesc, sUMed, sFam, sClass, sTitulo, sClave;
    double dPrecio1, dVolumen, dLongitud; //dPrecioFinal;//dPrehilecio2, dPrecio3 ... dPrecioN

    ifstream inputFile;
    inputFile.open("ListaDePrecios.txt");

    while(inputFile >> sID >> sDesc >> sUMed >> sFam >> sClass >> dVolumen >> dLongitud >> dPrecio1){
        cout << sID << " " << sDesc << " "  << sUMed << " " << sFam << " "  << sClass << " " << dVolumen << " " << dLongitud << " " << dPrecio1 << endl;
    }
    return 0;
}

File "ListaDePrecios.txt" contains:

CRE-AP65 Descripcion ML CUB-IMP COMP 1.0 2.0 16484.70
CRE-AP75 Descripcion ML CUB-IMP COMP 1.0 2.0 15705.26
CRE-AP65SR Descripcion ML CUB-IMP COMP 1.0 2.0 16333.47
CRE-AP75SR Descripcion ML CUB-IMP COMP 1.0 2.0 12564.20
  • This doesn't address the question, but get in the habit of initializing objects with meaningful values rather than creating them with default values and immediately changing them. That is, change `ifstream inputFile; inputFile.open("ListaDePrecios.txt");` to `ifstream inputFile("ListaDePrecios.txt");`. – Pete Becker Dec 28 '18 at 20:12
  • Don't use `if (!inputFile.eof())` for loop control. Check that the input operation succeeded by putting the entire operation into the condition: `while (inputFile >> sId ...)`. – Pete Becker Dec 28 '18 at 20:15
  • @PeteBecker thanks! I tried what you said but it didn't fix it. Do you have any other idea of what might it be? – javier santisteban Dec 28 '18 at 20:22
  • Reopened. As the comment says, testing `inputFile.eof()` was not the problem. – Pete Becker Dec 28 '18 at 20:24
  • You'll need to show some sample input in order to get useful answers. – Pete Becker Dec 28 '18 at 20:25
  • @PeteBecker I already put what the .txt file contains. – javier santisteban Dec 28 '18 at 20:31
  • 1
    Could you update the code to reflect the exact change you made and tested in response to @PeteBecker's suggestion regarding the loop condition? Also check that the file actually got opened, i.e. `if(!inputFile) { cout << "Could not open file!\n"; return 1; }` after opening the file. –  Dec 28 '18 at 20:46
  • @user10605163 Done, I updated it. – javier santisteban Dec 28 '18 at 20:55
  • Have you made the check I recommended as well? Because besides that I don't know what could be going wrong here. –  Dec 28 '18 at 21:14

0 Answers0