0

I'm making an extremely crude command based text editor in C++. It works just fine in outputting a .txt's contents. But when writing it gets more complicated. It encounters no problems when writing a single word to a .txt but has this problem when writing input with one or more spaces. Firstly my code looks like this.

#include <iostream>
#include <fstream>
#include <string>

int         readorwrite;
std::string filename;

int main() {
    while (true) {
        std::cout << "Read or write a file (1/2)" << std::endl << " > "; // note that the " > " is just a user input tag to make the program look cooler
        std::cin >> readorwrite;
        if (readorwrite == 1) { // what to do when reading
            std::string fileread;
            std::cout << "What file will you be reading?" << std::endl << " > ";
            std::cin >> filename;
            std::ifstream filename(filename);
            while (std::getline(filename, fileread)) {
                std::cout << fileread << std::endl;
            }
            filename.close();
        }
        if (readorwrite == 2) { // what to do when writing
            std::string filewrite;
            std::cout << "What will you name your file?" << std::endl << " > ";
            std::cin >> filename;
            std::ofstream filename(filename + ".txt");
            std::cout << "What will you be writing to the file?" << std::endl << " > ";
            std::cin >> filewrite; // this may be where the error occurs, if not then the next line
            filename << filewrite;
            filename.close();
        }
    }
}

Say I choose to write and my input is NOSPACES, it encounters no issue and gets back to the beginning as normal. But when I input something like YES SPACES something seems to go wrong and it starts repeating the loops beginning line of code? The output will be

Read or write a file (1/2)
 > Read or write a file (1/2)
 > Read or write a file (1/2)
 > Read or write a file (1/2)
 > Read or write a file (1/2)

And it will continue outputting that very fast without waiting for any input. What is the problem and how might I fix it?

  • use `getline` instead of `std::cin` This may help : https://stackoverflow.com/questions/5838711/stdcin-input-with-spaces – The Philomath Jan 01 '21 at 09:12
  • Please, have a look at this: [operator<<,>>(std::basic_string)](https://en.cppreference.com/w/cpp/string/basic_string/operator_ltltgtgt). It's intended behavior to stop input on first whitespace after non-whitespace has been read. How do you intend to remark end-of-input? If you just want to read one line `std::getline()` may be an option. If you want to read multiple lines things can get a bit more complicated. (You could e.g. use an end-marker as it is usual in shell input redirection like e.g. in `cat >out << EOT` where a single line with `EOT` triggers the end of input.) – Scheff's Cat Jan 01 '21 at 11:59

1 Answers1

0

Two things,

  1. you should change your variable name from filename to something else cause it is hard to interpret what you mean with that.
  2. The issue you are having is that cin ignores spaces. a better approach would be to use std::getline(std::cin, filewrite); This will read till a null terminated \0 or a new line \n. To get this to read multiple lines put it in a while loop. Another thing to watch out for with this is any extraneous new line characters. To avoid this add a dummy variable string and getline for that string to ensure it is going to read properly.

NOTE: With std::cin, before the read in another option is to put std::cin >> std::noskipws >> varname, but this can sometimes have undefined behaviour

Chris Wilhelm
  • 141
  • 1
  • 6