none
Read text line in file and display it in console RRS feed

  • Question

  •              ifstream inFile;
                 inFile.open("myfile.txt");
                 string line;
                 while (getline(inFile, line));
        {
                   cout << line << endl;
        }
                 inFile.close();

    Deletes the line of text in the file and does not display it on the screen of console application. Something is missing perhaps.

    None of the suggestions offered below have worked right for me.
    I have a file, mytext.txt, containing one sentence, for example "John is in the kitchen."
    I would like that my program reads what is written in this file and disploays: "John is in the kitchen."
    Thursday, June 13, 2019 9:13 AM

Answers

  • I have tested all above suggestions, including the latest of Rupesh Shukla.
    They all compile successfully and execute, however, they delete the file and do not display, on screen, in console application, the text contained in the file. The text is one sentence, one line, in Natural Language words. At first, my code, which I took from the Internet,
                 ifstream inFile;
                  inFile.open("myfile.txt");
                  string line;
                  while (getline(inFile, line));
         {
                    cout << line << endl;
         }
                  inFile.close();

    was working OK, displaying the contents of myfile.txt.
    I have changed something, I do not know what exactly, and since then it deletes myfile.txt (emties it) and displays nothing on screen.

    My intention is to write, in myfile.txt "John is in the kitchen", with C/C++ code for taking my typed input and when the sentence, I have typed in, is not a question, to save the above sentence in myfile.txt. I have code that recognizes whether the sentence is a question or not a question - a simple statement. Then, I need code that will read the sentence in myfile.txt and display it, on screen, so that the user sees where is John. At present, when asked, "Where is John?", my program answers "Perhaps John is at home", a probable answer, since the program does not know that John is in the kitchen. Any new suggestions?

    Of course, your old (but wrong) code was working ok, because there is only one line in the file. Still you should take away the ; behind the while-loop. If not, you can discard the { } brackets of your cout.

    If the file is empty, nothing is shown on the screen, of course.

    But you don't show us the whole code which deals with the file. You say, the file is deleted because you changed something. An ifstream-file cannot be written or deleted by code. The function getline will never delete the file contents. You can only read the file. You must use an ofstream-file to write into the file.

    So show us the whole code that deals with the file.

    Regards, Guido

    • Marked as answer by Languageware Wednesday, June 19, 2019 6:12 PM
    Wednesday, June 19, 2019 10:10 AM
  • The OP's posted code might produce the stated results if the one line in the input file was NOT terminated by a carriage return.

    myfile.txt without carriage return

    Results from running OP posted code -

    Now, terminate the one line with a carriage return

    And now the posted code doesn't produce any output

    As far as the file being deleted or emptied goes, that would not be caused by any of the code posted in this thread so far.

    Run an antivirus scan

    • Marked as answer by Languageware Thursday, June 20, 2019 11:15 AM
    Wednesday, June 19, 2019 10:37 AM
  • The OP's posted code might produce the stated results if the one line in the input file was NOT terminated by a carriage return.

    Hm, just thinking... if the file had a carriage return at the end of the first line, then there would be a second unseen line in the file. Then getline will read two times and the last empty line is shown.

    But now I have the idea, that the OP has a file with the carriage return... And since nothing is shown on the screen, he thinks the file is empty. The OP should open the file in a text editor because his code is still wrong. Nothing is deleted in the file. Only the code is wrong.

    So now we know what the OP changed that he had forgotten. He has changed the text file and added a carriage return at the end...

    Regards, Guido


    • Edited by Guido Franzke Wednesday, June 19, 2019 10:50 AM
    • Marked as answer by Languageware Wednesday, June 19, 2019 6:11 PM
    Wednesday, June 19, 2019 10:48 AM
  • But now I have the idea, that the OP has a file with the carriage return... And since nothing is shown on the screen, he thinks the file is empty. The OP should open the file in a text editor because his code is still wrong. Nothing is deleted in the file.
    That's certainly a possibility.  Appearances can be deceiving.
    • Marked as answer by Languageware Wednesday, June 19, 2019 6:11 PM
    Wednesday, June 19, 2019 10:51 AM
  • // Below is my code, compiles without error, main.cpp contains all what is needed, this *.cpp file also contains

    // what is needed. The Project is big, it has several dozen *.cpp files and *.h files, *.inl files, etc.

    // As you see, I operate with word meanings, not with actual words.

        

    case 210: // question must end with ?

    // writes the sentence to noquest.txt, but makes me repeat twice entering the same sentence - code taken Online      

    if (wrd->syn != '#' || wrd->syn != '?') {

             ofstream outfile;

             outfile.open("noquest.txt");

         

    if (type != '?') {

    // printf("\n This is not a question. A question ends with ?\n");


             cout << "Enter the same sentence again: ";

             cin.getline(data, 100);

             outfile << data << endl;

             outfile.close(); 

    printf(

    "A question ends with ?\n");

          }

    }

          i = mpos;

    continue;

    case 212: // where is ... ?


      file = fopen(

    "answer.txt","a+"); // stores the questions asked


    if (type == '?') {

       

    if (wrdm->E.W.clothes == 1) {

    printf("perhaps in the wardrobe.\n");

      fprintf(file,"%s","perhaps in the wardrobe.\n");

    else

    if (wrdm->E.W.human == 1 && wrdm->E.W.work_er != 1 && wrdm->E.W.crime != 1 && wrdm->E.W.kill_er != 1 && wrdm->E.W.science != 1 && wrdm->E.W.religion != 1 &&

    wrdm->E.W.musicinst != 1 && wrdm->E.W.military != 1 && wrdm->E.W.illness != 1 && wrdm->E.W.not_specified2 != 1 && wrdm->E.W.education != 1 &&

    wrdm->E.W.chief != 1 && wrdm->E.W.sport != 1 && wrdm->E.W.vehicle != 1 && wrdm->E.W.tame != 1 && wrdm->E.W.negative != 1 && wrdm->E.W.help != 1 &&

    wrdm->E.W.writing != 1 && wrdm->E.W.say != 1 && wrdm->E.W.seeing != 1 && wrdm->E.W.jurist != 1) {

    // exclude all other professions of John (any human) where John might be

    printf(" perhaps at home.\n");

      fprintf(file,"%s","perhaps at home.\n");


    // added 2019 for dialogue, input question "Where is John?" - must read it in the file below and display it.


                 ifstream inFile;

                 inFile.open("noquest.txt");

                 string line;            

    while (getline(inFile, line))

    {

                   cout << line << endl;

    }

                 inFile.close(); 

    // should close the inFile after reading its content

    //          fopen("noquest.txt","w");

    // when the code was working, I had to delete the sentence in the file, to free it for the answer of the next question

    // end 2019   

      }

    }

    fclose(file);

    i = mpos;

    continue;

    • Marked as answer by Languageware Thursday, June 20, 2019 11:15 AM
    Wednesday, June 19, 2019 12:52 PM

  •              ifstream inFile;

                 inFile.open("noquest.txt");

                 string line;            

    while (getline(inFile, line))

    {

                   cout << line << endl;

    }

                 inFile.close(); 


    This is different from the code that was included in the initial post.  It does not have a semicolon (;) after the while statement.

    In the simplest case related to the small snippet of originally posted code, did you bother to check whether the input file of one line was terminated by a carriage return?

    Did you check whether the input file was actually "empty" by opening it in a text editor?

    • Marked as answer by Languageware Wednesday, June 19, 2019 6:10 PM
    Wednesday, June 19, 2019 1:06 PM
  • I have tested all above suggestions, including the latest of Rupesh Shukla.
    They all compile successfully and execute, however, they delete the file and do not display, on screen, in console application, the text contained in the file. 

    I am not sure whether i get your point or not, how is the file getting deleted. can't see anything w.r.t deletion of a file so far in the code. Second when you post your code use code block for proper alignment .In last did you tried to debug your code considering all the suggestion .  Will say debug your code just verify whether you are able to perform read on file or not and where that file is getting deleted . Once you figured out that part thing will be straight forward. If still issue post here.

    Thanks


    Rupesh Shukla


    • Edited by Pintu Shukla Wednesday, June 19, 2019 2:04 PM
    • Marked as answer by Languageware Thursday, June 20, 2019 11:15 AM
    Wednesday, June 19, 2019 2:04 PM
  • The problem was in the carriage return, as you have suggested above. Case 210 writes the sentence in the file, the first line is OK, with the actual sentence. The second line is empty. I have opened the file with text editor, deleted the empty line and tested, with a question. The answer to the question was displayed by case 212, correctly.

    Now, my questions are
    - how do I make case 210 to write the sentence without carriage return;
    - how do I make case 210 to ask only once to enter the same sentence.
    Here is the input and output of case 210:
    John is the kitchen. // input
    Enter the same sentence again: John is the kitchen.
    A question ends with ? // output
    Enter the same sentence again: John is the kitchen.
    A question ends with ? // output
    John is the kitchen. // back to what was written as input

    Here is the input and output of vase 212:
    Where is John? // input
     perhaps at home. // output
    John is the kitchen. // output
    Where is John? // back to what was asked.

    • Marked as answer by Languageware Thursday, June 20, 2019 11:15 AM
    Wednesday, June 19, 2019 3:35 PM

  • if (wrd->syn != '#' || wrd->syn != '?') {


    This looks wrong. That test will always be true.

    If you run a code Analysis on it you will likely get a message to the effect
    that there is a mutual exclusion causing a constant non-zero result.

    For negative (!=) compound conditionals you usually need to use a logical AND
    operation (&&).

    C6289
    https://docs.microsoft.com/en-us/visualstudio/code-quality/c6289?view=vs-2019

    - Wayne

    • Marked as answer by Languageware Thursday, June 20, 2019 11:15 AM
    Wednesday, June 19, 2019 6:13 PM
  • The signs # and ? mark unrecognized Natural Language words. A word that is not recognized cannot have two signs at once. Some unrecognized words bear the sign # others bear the sign ?.
    So, it is either # or ?, never both &&. That is well tested and used often in our code. Thank you, anyway.
    • Marked as answer by Languageware Thursday, June 20, 2019 11:14 AM
    Thursday, June 20, 2019 6:40 AM
  • The signs # and ? mark unrecognized Natural Language words. A word that is not recognized cannot have two signs at once. Some unrecognized words bear the sign # others bear the sign ?.
    So, it is either # or ?, never both &&. That is well tested and used often in our code. Thank you, anyway.

    I think you have missed the issue. The test:

    >if (wrd->syn != '#' || wrd->syn != '?')

    will *always* evaluate as true. It can't do otherwise. So the code in the block
    just after it will *always* be executed. Therefore the conditional doesn't
    actually test anything.

    Think about it. For what value of wrd->syn do you think it will test false?

    If wrd->syn is # then the expression wrd->syn != '#' will be false, but the
    expression wrd->syn != '?' will be true, so the conditional will test true.

    If wrd->syn is ? then the expression wrd->syn != '#' will be true, so the
    conditional will test true.

    If wrd->syn is any other character then the expression wrd->syn != '#' will
    be true, so the conditional will test true.

    How the conditional *should* be written depends on what you are trying to do
    in the test. Under what condition should the code in the block immediately
    after the if() be executed, and when should it not be executed (and any "else"
    block be executed instead)?

    >So, it is either # or ?, never both &&.

    Well of course a single character variable can't have two different values at
    the same time. But you aren't testing for equality (==) you're testing for
    inequality (!=).

    >That is well tested and used often in our code.

    Then you may have many logical bugs in your code.

    But don't take my word for it. Consider this simple example which replicates
    the testing you're doing:

    #include <iostream>
    
    int main()
    {
        char wrd_syn;
        std::cin >> wrd_syn;
        //if (wrd->syn != '#' || wrd->syn != '?')
        if (wrd_syn != '#' || wrd_syn != '?')
          std::cout << "Hello World!\n";
    }
    

    VS 2017 Code Analysis shows:

    warning C6289: Incorrect operator:  mutual exclusion over || is always a non-zero constant.
    Did you intend to use && instead?
    

    Cppcheck shows this:

    - Wayne

    • Marked as answer by Languageware Thursday, June 20, 2019 11:14 AM
    Thursday, June 20, 2019 7:52 AM
  • Thank you. I will check that, but I will not change it, because the program functions exactly as it should in all other cases, exceptin case 210. I will remove this condition in case 210 and leave only

    if (type != '?') {  // if it is not a sentence ending with a question mark.

    • Marked as answer by Languageware Thursday, June 20, 2019 11:14 AM
    Thursday, June 20, 2019 9:55 AM
  • I will check that, but I will not change it, because the program functions exactly as it should in all other cases.

    As you wish. But it's imperative that you understand why the conditional in question
    actually does nothing (always evaluates true). This is a very common mistake that
    is made by many programmers and has been addressed countless times in forums and
    newsgroups. See for example:

    Not equal (!=) operator not working with correctly char in c++
    https://stackoverflow.com/questions/31219577/not-equal-operator-not-working-with-correctly-char-in-c

    The confusion seems to be related to the tendency for programmers to write code
    that closely reflects how we would describe the relationship in English. For
    example we might say: "If wrd_syn is not '#' or '?' ..." But expressing that
    in code as

    if (wrd_syn != '#' || wrd_syn != '?')
    

    gives the wrong result as already explained.

    To check if the value is not # or ? in my example we can write:

    if (!(wrd_syn == '#' || wrd_syn == '?'))
    
    

    or

    if (wrd_syn != '#' && wrd_syn != '?')
    

    - Wayne

    • Marked as answer by Languageware Thursday, June 20, 2019 11:14 AM
    Thursday, June 20, 2019 11:01 AM
  • Wayne,  You were absolutely right. I am sorry! I simply was absent minded when I read your reply.
    • Marked as answer by Languageware Thursday, June 20, 2019 11:14 AM
    Thursday, June 20, 2019 11:11 AM
  • As evidence that I was wrong, I quote again case 212, see

    if (wrdm->E.W.human == 1 && wrdm->E.W.work_er != 1 && wrdm->E.W.crime != 1 && wrdm->E.W.kill_er != 1 && wrdm->E.W.science != 1 && wrdm->E.W.religion != 1) {

    // where I have if human, but not worker, not criminal, not assassin, not scientest, not a priest, ...etc.

    // I do not have or  || because the case will not work right. I have sometimes made this mistake, unadvertently.

    • Marked as answer by Languageware Thursday, June 20, 2019 11:20 AM
    Thursday, June 20, 2019 11:19 AM

All replies

  • Hello,

    you must delete the ; behind the while.

    ifstream inFile;
    inFile.open("myfile.txt");
    if (!inFile.is_open())
    {
       // error handling
    }
    string line;
    while (getline(inFile, line))
    { 
       cout << line << endl;
    }
    inFile.close();

    Regards Guido


    Edit: in an ifstream, nothing is deleted in the file. You don't write into the file.
    Thursday, June 13, 2019 9:30 AM
  • Hello,

    >> inFile.open("myfile.txt");

    Do you have this text file in your project folder? Is the document name correct? I found that there's no problem with the result after testing. Your code is correct, and the possible cause of the error is the file path. If you don't write the file path completely, the system will go to the folder of the project to find the file.

    Best Regards,

    Suarez Zhou

    Thursday, June 13, 2019 9:58 AM
  • Your code is correct, and the possible cause of the error is the file path.

    No. The code is not correct. Look at the while-loop:

    while (getline(inFile, line));
        {
                   cout << line << endl;
        }

    The file is read line by line. After the file is totally read, the cout is called. The last line of the file will be shown. But the OP wants to output all lines of the file, so the ; is wrong.

    Of course, error handling must be done if the file cannot be opened. Just look at the code in my answer before.

    Regards, Guido

    • Proposed as answer by Pintu Shukla Monday, June 17, 2019 6:49 PM
    Thursday, June 13, 2019 10:36 AM
  • std::fstream fs; fs.open ("test.txt", std::fstream::in | std::fstream::out | std::fstream::app); if(fs.is_open() ) { string strText; while( getline( fs, strText ) ) { cout << strText << endl; } fs.close(); } else cout << "Error opening file " ;

    Thanks

    Rupesh Shukla

    Monday, June 17, 2019 6:54 PM
  • I have tested all above suggestions, including the latest of Rupesh Shukla.
    They all compile successfully and execute, however, they delete the file and do not display, on screen, in console application, the text contained in the file. The text is one sentence, one line, in Natural Language words. At first, my code, which I took from the Internet,
                 ifstream inFile;
                  inFile.open("myfile.txt");
                  string line;
                  while (getline(inFile, line));
         {
                    cout << line << endl;
         }
                  inFile.close();

    was working OK, displaying the contents of myfile.txt.
    I have changed something, I do not know what exactly, and since then it deletes myfile.txt (emties it) and displays nothing on screen.

    My intention is to write, in myfile.txt "John is in the kitchen", with C/C++ code for taking my typed input and when the sentence, I have typed in, is not a question, to save the above sentence in myfile.txt. I have code that recognizes whether the sentence is a question or not a question - a simple statement. Then, I need code that will read the sentence in myfile.txt and display it, on screen, so that the user sees where is John. At present, when asked, "Where is John?", my program answers "Perhaps John is at home", a probable answer, since the program does not know that John is in the kitchen. Any new suggestions?

    Wednesday, June 19, 2019 9:59 AM
  • I have tested all above suggestions, including the latest of Rupesh Shukla.
    They all compile successfully and execute, however, they delete the file and do not display, on screen, in console application, the text contained in the file. The text is one sentence, one line, in Natural Language words. At first, my code, which I took from the Internet,
                 ifstream inFile;
                  inFile.open("myfile.txt");
                  string line;
                  while (getline(inFile, line));
         {
                    cout << line << endl;
         }
                  inFile.close();

    was working OK, displaying the contents of myfile.txt.
    I have changed something, I do not know what exactly, and since then it deletes myfile.txt (emties it) and displays nothing on screen.

    My intention is to write, in myfile.txt "John is in the kitchen", with C/C++ code for taking my typed input and when the sentence, I have typed in, is not a question, to save the above sentence in myfile.txt. I have code that recognizes whether the sentence is a question or not a question - a simple statement. Then, I need code that will read the sentence in myfile.txt and display it, on screen, so that the user sees where is John. At present, when asked, "Where is John?", my program answers "Perhaps John is at home", a probable answer, since the program does not know that John is in the kitchen. Any new suggestions?

    Of course, your old (but wrong) code was working ok, because there is only one line in the file. Still you should take away the ; behind the while-loop. If not, you can discard the { } brackets of your cout.

    If the file is empty, nothing is shown on the screen, of course.

    But you don't show us the whole code which deals with the file. You say, the file is deleted because you changed something. An ifstream-file cannot be written or deleted by code. The function getline will never delete the file contents. You can only read the file. You must use an ofstream-file to write into the file.

    So show us the whole code that deals with the file.

    Regards, Guido

    • Marked as answer by Languageware Wednesday, June 19, 2019 6:12 PM
    Wednesday, June 19, 2019 10:10 AM
  • The OP's posted code might produce the stated results if the one line in the input file was NOT terminated by a carriage return.

    myfile.txt without carriage return

    Results from running OP posted code -

    Now, terminate the one line with a carriage return

    And now the posted code doesn't produce any output

    As far as the file being deleted or emptied goes, that would not be caused by any of the code posted in this thread so far.

    Run an antivirus scan

    • Marked as answer by Languageware Thursday, June 20, 2019 11:15 AM
    Wednesday, June 19, 2019 10:37 AM
  • The OP's posted code might produce the stated results if the one line in the input file was NOT terminated by a carriage return.

    Hm, just thinking... if the file had a carriage return at the end of the first line, then there would be a second unseen line in the file. Then getline will read two times and the last empty line is shown.

    But now I have the idea, that the OP has a file with the carriage return... And since nothing is shown on the screen, he thinks the file is empty. The OP should open the file in a text editor because his code is still wrong. Nothing is deleted in the file. Only the code is wrong.

    So now we know what the OP changed that he had forgotten. He has changed the text file and added a carriage return at the end...

    Regards, Guido


    • Edited by Guido Franzke Wednesday, June 19, 2019 10:50 AM
    • Marked as answer by Languageware Wednesday, June 19, 2019 6:11 PM
    Wednesday, June 19, 2019 10:48 AM
  • But now I have the idea, that the OP has a file with the carriage return... And since nothing is shown on the screen, he thinks the file is empty. The OP should open the file in a text editor because his code is still wrong. Nothing is deleted in the file.
    That's certainly a possibility.  Appearances can be deceiving.
    • Marked as answer by Languageware Wednesday, June 19, 2019 6:11 PM
    Wednesday, June 19, 2019 10:51 AM
  • // Below is my code, compiles without error, main.cpp contains all what is needed, this *.cpp file also contains

    // what is needed. The Project is big, it has several dozen *.cpp files and *.h files, *.inl files, etc.

    // As you see, I operate with word meanings, not with actual words.

        

    case 210: // question must end with ?

    // writes the sentence to noquest.txt, but makes me repeat twice entering the same sentence - code taken Online      

    if (wrd->syn != '#' || wrd->syn != '?') {

             ofstream outfile;

             outfile.open("noquest.txt");

         

    if (type != '?') {

    // printf("\n This is not a question. A question ends with ?\n");


             cout << "Enter the same sentence again: ";

             cin.getline(data, 100);

             outfile << data << endl;

             outfile.close(); 

    printf(

    "A question ends with ?\n");

          }

    }

          i = mpos;

    continue;

    case 212: // where is ... ?


      file = fopen(

    "answer.txt","a+"); // stores the questions asked


    if (type == '?') {

       

    if (wrdm->E.W.clothes == 1) {

    printf("perhaps in the wardrobe.\n");

      fprintf(file,"%s","perhaps in the wardrobe.\n");

    else

    if (wrdm->E.W.human == 1 && wrdm->E.W.work_er != 1 && wrdm->E.W.crime != 1 && wrdm->E.W.kill_er != 1 && wrdm->E.W.science != 1 && wrdm->E.W.religion != 1 &&

    wrdm->E.W.musicinst != 1 && wrdm->E.W.military != 1 && wrdm->E.W.illness != 1 && wrdm->E.W.not_specified2 != 1 && wrdm->E.W.education != 1 &&

    wrdm->E.W.chief != 1 && wrdm->E.W.sport != 1 && wrdm->E.W.vehicle != 1 && wrdm->E.W.tame != 1 && wrdm->E.W.negative != 1 && wrdm->E.W.help != 1 &&

    wrdm->E.W.writing != 1 && wrdm->E.W.say != 1 && wrdm->E.W.seeing != 1 && wrdm->E.W.jurist != 1) {

    // exclude all other professions of John (any human) where John might be

    printf(" perhaps at home.\n");

      fprintf(file,"%s","perhaps at home.\n");


    // added 2019 for dialogue, input question "Where is John?" - must read it in the file below and display it.


                 ifstream inFile;

                 inFile.open("noquest.txt");

                 string line;            

    while (getline(inFile, line))

    {

                   cout << line << endl;

    }

                 inFile.close(); 

    // should close the inFile after reading its content

    //          fopen("noquest.txt","w");

    // when the code was working, I had to delete the sentence in the file, to free it for the answer of the next question

    // end 2019   

      }

    }

    fclose(file);

    i = mpos;

    continue;

    • Marked as answer by Languageware Thursday, June 20, 2019 11:15 AM
    Wednesday, June 19, 2019 12:52 PM

  •              ifstream inFile;

                 inFile.open("noquest.txt");

                 string line;            

    while (getline(inFile, line))

    {

                   cout << line << endl;

    }

                 inFile.close(); 


    This is different from the code that was included in the initial post.  It does not have a semicolon (;) after the while statement.

    In the simplest case related to the small snippet of originally posted code, did you bother to check whether the input file of one line was terminated by a carriage return?

    Did you check whether the input file was actually "empty" by opening it in a text editor?

    • Marked as answer by Languageware Wednesday, June 19, 2019 6:10 PM
    Wednesday, June 19, 2019 1:06 PM
  • Ok,
    in case 210 you open the file for writing. Only if type != '?' you write something into the file and close it. And you write endl after the line into the file (so your first code was always showing nothing). If type=='?' then the file is implicitly closed by the closing } and the file is empty. Now you make a "continue". But we don't see what happens next. When does case 212 happen? Always after a real question was correctly written in the file?
    You should make breakpoints in case 210 und 212 to know when something is written into the file and when it is read. And you should check the contents of the file in a text editor.

    Nevertheless, you delete the wrong-placed ; at the while getline-loop as I mentioned before.

    Regards, Guido

    Wednesday, June 19, 2019 1:12 PM
  • I have tested all above suggestions, including the latest of Rupesh Shukla.
    They all compile successfully and execute, however, they delete the file and do not display, on screen, in console application, the text contained in the file. 

    I am not sure whether i get your point or not, how is the file getting deleted. can't see anything w.r.t deletion of a file so far in the code. Second when you post your code use code block for proper alignment .In last did you tried to debug your code considering all the suggestion .  Will say debug your code just verify whether you are able to perform read on file or not and where that file is getting deleted . Once you figured out that part thing will be straight forward. If still issue post here.

    Thanks


    Rupesh Shukla


    • Edited by Pintu Shukla Wednesday, June 19, 2019 2:04 PM
    • Marked as answer by Languageware Thursday, June 20, 2019 11:15 AM
    Wednesday, June 19, 2019 2:04 PM
  • The problem was in the carriage return, as you have suggested above. Case 210 writes the sentence in the file, the first line is OK, with the actual sentence. The second line is empty. I have opened the file with text editor, deleted the empty line and tested, with a question. The answer to the question was displayed by case 212, correctly.

    Now, my questions are
    - how do I make case 210 to write the sentence without carriage return;
    - how do I make case 210 to ask only once to enter the same sentence.
    Here is the input and output of case 210:
    John is the kitchen. // input
    Enter the same sentence again: John is the kitchen.
    A question ends with ? // output
    Enter the same sentence again: John is the kitchen.
    A question ends with ? // output
    John is the kitchen. // back to what was written as input

    Here is the input and output of vase 212:
    Where is John? // input
     perhaps at home. // output
    John is the kitchen. // output
    Where is John? // back to what was asked.

    • Marked as answer by Languageware Thursday, June 20, 2019 11:15 AM
    Wednesday, June 19, 2019 3:35 PM

  • if (wrd->syn != '#' || wrd->syn != '?') {


    This looks wrong. That test will always be true.

    If you run a code Analysis on it you will likely get a message to the effect
    that there is a mutual exclusion causing a constant non-zero result.

    For negative (!=) compound conditionals you usually need to use a logical AND
    operation (&&).

    C6289
    https://docs.microsoft.com/en-us/visualstudio/code-quality/c6289?view=vs-2019

    - Wayne

    • Marked as answer by Languageware Thursday, June 20, 2019 11:15 AM
    Wednesday, June 19, 2019 6:13 PM
  • The signs # and ? mark unrecognized Natural Language words. A word that is not recognized cannot have two signs at once. Some unrecognized words bear the sign # others bear the sign ?.
    So, it is either # or ?, never both &&. That is well tested and used often in our code. Thank you, anyway.
    • Marked as answer by Languageware Thursday, June 20, 2019 11:14 AM
    Thursday, June 20, 2019 6:40 AM
  • The signs # and ? mark unrecognized Natural Language words. A word that is not recognized cannot have two signs at once. Some unrecognized words bear the sign # others bear the sign ?.
    So, it is either # or ?, never both &&. That is well tested and used often in our code. Thank you, anyway.

    I think you have missed the issue. The test:

    >if (wrd->syn != '#' || wrd->syn != '?')

    will *always* evaluate as true. It can't do otherwise. So the code in the block
    just after it will *always* be executed. Therefore the conditional doesn't
    actually test anything.

    Think about it. For what value of wrd->syn do you think it will test false?

    If wrd->syn is # then the expression wrd->syn != '#' will be false, but the
    expression wrd->syn != '?' will be true, so the conditional will test true.

    If wrd->syn is ? then the expression wrd->syn != '#' will be true, so the
    conditional will test true.

    If wrd->syn is any other character then the expression wrd->syn != '#' will
    be true, so the conditional will test true.

    How the conditional *should* be written depends on what you are trying to do
    in the test. Under what condition should the code in the block immediately
    after the if() be executed, and when should it not be executed (and any "else"
    block be executed instead)?

    >So, it is either # or ?, never both &&.

    Well of course a single character variable can't have two different values at
    the same time. But you aren't testing for equality (==) you're testing for
    inequality (!=).

    >That is well tested and used often in our code.

    Then you may have many logical bugs in your code.

    But don't take my word for it. Consider this simple example which replicates
    the testing you're doing:

    #include <iostream>
    
    int main()
    {
        char wrd_syn;
        std::cin >> wrd_syn;
        //if (wrd->syn != '#' || wrd->syn != '?')
        if (wrd_syn != '#' || wrd_syn != '?')
          std::cout << "Hello World!\n";
    }
    

    VS 2017 Code Analysis shows:

    warning C6289: Incorrect operator:  mutual exclusion over || is always a non-zero constant.
    Did you intend to use && instead?
    

    Cppcheck shows this:

    - Wayne

    • Marked as answer by Languageware Thursday, June 20, 2019 11:14 AM
    Thursday, June 20, 2019 7:52 AM
  • Thank you. I will check that, but I will not change it, because the program functions exactly as it should in all other cases, exceptin case 210. I will remove this condition in case 210 and leave only

    if (type != '?') {  // if it is not a sentence ending with a question mark.

    • Marked as answer by Languageware Thursday, June 20, 2019 11:14 AM
    Thursday, June 20, 2019 9:55 AM
  • I will check that, but I will not change it, because the program functions exactly as it should in all other cases.

    As you wish. But it's imperative that you understand why the conditional in question
    actually does nothing (always evaluates true). This is a very common mistake that
    is made by many programmers and has been addressed countless times in forums and
    newsgroups. See for example:

    Not equal (!=) operator not working with correctly char in c++
    https://stackoverflow.com/questions/31219577/not-equal-operator-not-working-with-correctly-char-in-c

    The confusion seems to be related to the tendency for programmers to write code
    that closely reflects how we would describe the relationship in English. For
    example we might say: "If wrd_syn is not '#' or '?' ..." But expressing that
    in code as

    if (wrd_syn != '#' || wrd_syn != '?')
    

    gives the wrong result as already explained.

    To check if the value is not # or ? in my example we can write:

    if (!(wrd_syn == '#' || wrd_syn == '?'))
    
    

    or

    if (wrd_syn != '#' && wrd_syn != '?')
    

    - Wayne

    • Marked as answer by Languageware Thursday, June 20, 2019 11:14 AM
    Thursday, June 20, 2019 11:01 AM
  • Wayne,  You were absolutely right. I am sorry! I simply was absent minded when I read your reply.
    • Marked as answer by Languageware Thursday, June 20, 2019 11:14 AM
    Thursday, June 20, 2019 11:11 AM
  • As evidence that I was wrong, I quote again case 212, see

    if (wrdm->E.W.human == 1 && wrdm->E.W.work_er != 1 && wrdm->E.W.crime != 1 && wrdm->E.W.kill_er != 1 && wrdm->E.W.science != 1 && wrdm->E.W.religion != 1) {

    // where I have if human, but not worker, not criminal, not assassin, not scientest, not a priest, ...etc.

    // I do not have or  || because the case will not work right. I have sometimes made this mistake, unadvertently.

    • Marked as answer by Languageware Thursday, June 20, 2019 11:20 AM
    Thursday, June 20, 2019 11:19 AM