0

Was testing this function in another cpp but it didn't work so I put it in a new cpp for testing, below is the code:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>
#include <sstream>

using namespace std;

typedef vector<string> vstring;
vector<vstring> data;

void createDisplay(); 

bool checkFile(); //checks if the txt file is empty or not, if yes display "no data"

int main()
{
    checkFile();
    createDisplay();

    cout << "The End";     //this doesn't shows up

    return 0;
}

void createDisplay() //writes data from a txt file to 2d vector
 {                   //(12 rows fixated, total data are all multiples of 12) 
                    //and display all of its contents
    int i, j, k ,l;

    ifstream file;
    file.open("data.txt");

    while (!file.eof()) {
        for (int i = 0; i < 3; i++){
            vector<string> tmpVec;
            string tmpString;

                for (int j = 0; j < 12; j++){
                getline(file, tmpString);
                tmpVec.push_back(tmpString);
                }
            data.push_back(tmpVec);
        }
    }   


    string line, tempStr;

    while (getline(file, line)) 
    {

        data.push_back(vstring());
        istringstream strm(line);
        while (strm >> tempStr)
        data.back().push_back(tempStr);
    }


    string list[12] =        //list of items, tried splitting into two functions from here
    {
        "[1]    A: ",
        "[2]    B: ",
        "[3]    C: ",
        "[4]    D: ",
        "[5]    E: ",
        "[6]    F: ",
        "[7]    G: ",
        "[8]    H: ",
        "[9]    I: ",
        "[10]   J: ",
        "[11]   K: ",
        "[12]   L: "
    };

    if (checkFile() == true)
    {
        for(k=0; k<12; k++)
        {
            cout << list[k] << "No data" << endl;
        }
    }
    else 
    {
        for(k=0;k<data[l].size();k++)
        {
            cout << list[k] ;

            for(l=0;l<data.size();l++)
            {
                cout << left << setw(25) << data[l][k]  ;
            }
            cout <<endl;
        }
    }
}

bool checkFile()  //checks if the txt file is empty or not, if yes display "no data"
{
    ifstream file;
    file.open("data.txt");

    if (file.peek() == ifstream::traits_type::eof())
        return true;
    else
        return false;
}

Tested some cases,

Case 1: There is data in the txt file, data is successfully written into the vector, all data are displayed nicely, then programs stuns for a while, "The End" does not appear and the program ends.

Case 2: There is no data in the txt file, checkFile() works and displays "no data", "The End" appears.

I tried splitting createDisplay() into 2, one writing data into the vector, one displays content of vector, this round the program just stuns for a moment and ends without showing anything

yun1001
  • 13
  • 3
  • 3
    Try putting `std::endl` to your print statement. This may be caused by missing IO flushing. –  Sep 26 '18 at 15:25
  • 2
    It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – NathanOliver Sep 26 '18 at 15:26
  • I think your program simply crashes, as suggested by NathanOliver you need to use a debugger / set breakpoints to understand what is happening. A possibility is that you have exceptions enabled and an exception occured (use a try / catch block in main to check that). – VB_overflow Sep 26 '18 at 15:28
  • 2
    When I compile the code, the compiler (clang++) is outputting 5 warnings and 8 errors. That is disconcerting, and may be related to the problems encountered. I'm not sure how you got this code to compile. Try using a debugger and stepping through your executable. – Eljay Sep 26 '18 at 15:44
  • 3
    `while (!file.eof()) {` is a common mistake. https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – drescherjm Sep 26 '18 at 16:08
  • 3
    Compiling your code I see a problem. `gh.cpp:77:9: warning: variable 'l' is used uninitialized whenever 'if' condition is false` An unititalized variable read is UB. This means your code is broken. You should turn the compiler warning level up and treat warnings as errors. – Martin York Sep 26 '18 at 16:30
  • 1
    Side note: UB is shorthand for Undefined Behaviour. Undefined Behaviour is pretty much what it sounds like, behaviour that the C++ standard does not specify. When you give the compiler code that is syntactically correct but can't possibly work, the C++ standard often doesn't specify what the program should do because anything it tries to do is likely the wrong choice or detecting that it happened is an unnecessary tax on the resources of a program that is written correctly. One of the reasons C++ is often faster than other common languages is it assumes you got the code right, not wrong. – user4581301 Sep 26 '18 at 17:14
  • Thanks for all the help, I changed (!file.eof()) and removed the data.size[] in the loop which prints the vector, it works nicely now. – yun1001 Sep 27 '18 at 03:15

1 Answers1

0

As pointed out you could append << endl to the cout << "The End"; statement and should output to the console. The reason for this is that typically endl flushes the contents of the stream. That would also add the '\n' character; which in this case should not matter. You can always append << flush to the statement and should output the contents of the stream to the console.