String is not printing without new line character in C++

爷,独闯天下 提交于 2019-12-06 11:13:52

An std::ostream's output needs to be flushed. It is normally flushed automatically when a line-feed \n is written. If you want to force the stream to flush, you can use the std::flush manipulator like so:

std::cout << "foo" << std::flush;

Edit: Although my post clearly answers the question "Why does my line not show up unless I output a \n character?" You said this does not answer your question, so I will attempt some mind reading to try and answer your real question.

Since I have no idea what you really want know, I'll point out several things here that are wrong with your code and it might help you find your problem or clarify your question.

First, if you are using the file name input from std::cin, when argc<2, you will, a 100% guaranteed, cause a failure in your application. The reason is that the character buffer pointed to by inFileName contains a single byte, reserved for the terminating null character. If someone enters any text whatsoever, you will get a buffer overrun. If someone enters an empty string, your program will open no file and inFile.open(...); will return an error code that you don't check, so your program won't crash, but still won't work.

Second, the other line inputs are needlessly limited to 256 characters and are just as dangerous (i.e. lines longer that 256 characters will cause a bufer overrun). Since you eventually create std::string instances out of the content, you should just plainly use std::getline(). It is shorter to type, more general and safer.

Third, the description of your problem is that no output is generated unless you add a \n character. As I explained, this is perfectly normal. From re-reading your post, I can understand that you don't unhderstand why you should have to add one given that there was already one in the input file. The reason you need to add it is because the getline() functions discard the \n character. It is not inserted into your line's buffer.

I've cleaned up some of your code to show you some clear improvements. From this code you will be able to understand the structure of your program, which should also reflect the structure of your input.

#include "truthTable2.h"

int main(int argc, const char* argv[]){
  std::ifstream inFile;
  if(argc != 2){
    cout << "Enter an input file name: ";
    std::string inFileName;
    std::getline(std::cin, inFileName);
    inFile.open(inFileName.c_str());
  }
  else {
    inFile.open(argv[1]);
  }
  if ( !inFile.is_open() ) {
      // Did not successfully open a file. Print error message and exit!
  }
  TruthTable tTable;
  for (std::string variables; std::getline(inFile,variables); )
  {
    std::cout << variables << std::endl;
    tTable.setVariables(variable);
    std::string formula std::getline(formula);
    std::cout << formula << std::endl;
    tTable.setFormula(formula);
    tTable.printTable();
  }
  return 0;
}

From this, I have a question:how is your input structured? Is your input file only consisted of 2 lines? Are there multiple sets of these line pairs? Is there a single line with variables and a bunch of equations? These three cases will lead me to re-structure the program in one of the following fashions:

2 lines only:

ThruthTable table;
std::string variables, equation;
std::getline(file, variables);
std::getline(file, equation);
// ...

Multiple sets:

while ( !inFile.eof() )
{
    ThruthTable table;
    std::string variables, equation;
    std::getline(file, variables);
    std::getline(file, equation);
    // ...
}

Multiple equations:

ThruthTable table;
std::string variables;
std::getline(variables);
for ( std::string equation; std::getline(file, equation); )
{
    std::getline(file, equation);
    // ...
}

If what I am seeing is right, the output from printf is the one that is not showing. In that case, either use

fflush(stdout);

Or better, just go with a std::cout for that line since you're writing it in C++ (using the std::flush technique, of course.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!