开发者

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

I'm opening a file, and getting lines from it. The first line should say how many variables there are, and what their names are. The second line should be a logic equation using these variables. The assignment is to have it print out a truth table for the variables and equation.

The first line the program is taking in is not printing without me inserting a new line character. I tried converting to a string and using both printf and cout.

Main file that inputs everything:

#include "truthTable2.h"

int main(int argc, const char* argv[]){
  ifstream inFile;
  if(argc != 2){
    cout << "Enter an input file name: ";
    char *inFileName = "";
    cin >> inFileName;
    inFile.open(inFileName);
  }
  else
    inFile.open(argv[1]);
  TruthTable tTable;
  while(!inFile.eof()){
    char variableLine[256];
    inFile.getline(variableLine, 256);
    printf("%s ", variableLine);
    string variable(variableLine);
    tTable.setVariables开发者_如何转开发(variable);
    char formulaLine[256];
    inFile.getline(formulaLine, 256);
    cout << formulaLine << "\n";
    string formula(formulaLine);
    tTable.setFormula(formula);
    tTable.printTable();
  }
  inFile.close();
  return 0;
}

Sample input:

2 x y
( \wedge x ( \not y ) )

Output from this:

 ( \wedge x ( \not y ) )

I think whatever is causing this is giving me problems throughout the rest of the program as well. After I tokenize the variableLine it does not print without a new line character and it does not find the second variable when evaluating the formula.


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.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜