-5

I am currently learning how to use text files, but when i put a condition like the one in the code below, even if the condition is true it won't try the while part. Can anyone help out?

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    string s, d;
    ofstream k("t1.txt");
    int a;
    cin >> a;
    if (a > 3)
    {
        while (k.is_open())
        {
            getline(cin, s);
            k << s;
            k.close();
        }
    }
    ifstream r("t1.txt");
    while (r.is_open())
    {
        getline(r, d);
        cout << d;
        r.close();
    }
}
HolyBlackCat
  • 45,832
  • 5
  • 81
  • 134
Redi Ramaj
  • 13
  • 6
  • 1
    "_Can anyone help out?_" What, exactly, is the problem? How do you know that the condition is `true`? Did you step through your code with a debugger? – Algirdas Preidžius Jan 20 '18 at 13:15
  • And what did you learn while stepping through the code in your debugger? – Jesper Juhl Jan 20 '18 at 13:16
  • Is `k.is_open()` going to be true for each iteration of the loop? – Ed Heal Jan 20 '18 at 13:17
  • If i put a integer like for ex 4.. wont it be true? @AlgirdasPreidžius – Redi Ramaj Jan 20 '18 at 13:17
  • @EdHeal yes it is – Redi Ramaj Jan 20 '18 at 13:18
  • So what is the point having it as a condition in the loop – Ed Heal Jan 20 '18 at 13:18
  • [How to debug small programs](https://www.google.dk/amp/s/ericlippert.com/2014/03/05/how-to-debug-small-programs/amp/) – Jesper Juhl Jan 20 '18 at 13:18
  • @RediRamaj I will repeat my question, did you step through your code with a debugger? Everything depends on what you mean by "_it won't try the while part_". Related: https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction – Algirdas Preidžius Jan 20 '18 at 13:19
  • @EdHeal Technically, no, since the file is getting closed during the first iteration. – Algirdas Preidžius Jan 20 '18 at 13:19
  • There's no issue with the if part, demonstration here: https://onlinegdb.com/Bk87G6gBM . I don't understand what you are trying to achieve. – JensV Jan 20 '18 at 13:21
  • 1
    You are closing the file after the first iteration in while loop. So, second time the condition in while loop will be false. – Bhawan Jan 20 '18 at 13:21
  • 4
    The code is correct, in that it doesn't have syntax errors and there is a straightforward analysis of what it should do. The question needs work; it is not clear what the input is, whether either of the streams was successfully created, or how it was determined that "it won't try the while part". That's why it's getting some slightly snarky responses. There simply isn't enough information to enable anyone to say what's happening here. – Pete Becker Jan 20 '18 at 13:22
  • @PeteBecker if i input a integer like 4 the program ends. I just want it to continue to the first while – Redi Ramaj Jan 20 '18 at 13:24
  • @AlgirdasPreidžius - Sorry I missed that - i.e. Why – Ed Heal Jan 20 '18 at 13:25
  • @AlgirdasPreidžius and the debugger says nthn – Redi Ramaj Jan 20 '18 at 13:27
  • @RediRamaj How did you determine that `while` isn't entered? From what I can see, this is the workflow you are experiencing: You enter 4, file gets created, `while` loop gets entered, `getline` fetches the trailing newline character, while discarding it, writes the empty string to the file, and immediately closes it, ensuring that the second iteration of loop is not executed. Afterwards, the same file gets opened, nothing gets read, file gets closed immediately, and the code terminates. – Algirdas Preidžius Jan 20 '18 at 13:29
  • 1
    @RediRamaj Why should it say anything? You are supposed to step through the code with it, yourself. – Algirdas Preidžius Jan 20 '18 at 13:29
  • @AlgirdasPreidžius then what should i do? – Redi Ramaj Jan 20 '18 at 13:33
  • @Redi Ramaj the debugger is not a magic tool that tells you what is wrong. But it allows you to run the program step-by-step one line at a time and lets you inspect the values of variables at each step; this allows you to observe what's happening and figure out what is different from your expectations. It also allows you to set breakpoints so execution will halt at interesting points. This is usually enough to work out where a problem is. – Jesper Juhl Jan 20 '18 at 13:46

1 Answers1

0

I ran the code in gdb, it runs perfectly. It opens the file, gets the < cr > character from stdin, writes that to the first line which wipes out the first line, then tries to read the first line which is empty so there is no output. Good job 8).

You're just having trouble understanding your own expectations, your code works fine, so just think a little bit more about what you think it's supposed to do.

You'll also need to examine the contents of your text file before and after running the code, and try entering some text in the text file before you start the program, to see what happens to the text.

The getline where you try to read from the console is the issue, that returns without asking for input so it ends up giving you a blank line to write to the file.

From this link: http://www.cplusplus.com/forum/beginner/39549/

There's a similar question there, and the first of the comments mentions this about the behavior of getline: std::cin leaves the newline character in the buffer after pressing enter, and getline just grabs it and keeps going, that's why getline doesn't block to wait for input. .......

That's why it's not "doing anything" - it's not asking you for input, so the program has nothing to output and it looks like it's not working.

(this, by the way, is why programmers are snarky - we have to deal with stupid little behavioral issues of machines and we forget that PEOPLE are not normally like this and that PEOPLE hate being held to these kinds of standards)

I put a second getline in your code right after hte first one and now it asks for and outputs a string that I type in and sticks it in the file.

To run the program in gdb, do the following:

g++ -g < your cpp file > -o < whatever you want the binary to be called >

like this: g++ -g myfile.cpp -o myrunnableprogram

this creates a binary with symbols that you can open in gdb

then

gdb myrunnableprogram

then in gdb

start

next

next

next

....

these commands will start the program and pause it at the first breakable spot, then each time you type next you will step to the next command and you can poke around at variables to see what's going on.

read up on gdb, it's really useful.

  • Then how can i make a condition without wiping the first line? – Redi Ramaj Jan 20 '18 at 13:39
  • I'd suggest avoiding references to cplusplus.com since it's full of errors - I'd suggest (when possible) refering to https://cppreference.com/ instead. – Jesper Juhl Jan 20 '18 at 13:51
  • Hi Redi, if you want to avoid wiping out the first line you should think about how a stream operates - there's a pointer to the current character position and if you write at that position, whatever's already there will be erased. Think about how to move that pointer using the functions available in the ostream class. The magic word is "append" – user2744846 Jan 20 '18 at 13:56