0

Here's my problem. I have to read a line from the console, for example:

Name,Area,Cost

Then using the comma as the delimiter character, it has to name, area and cost and place them into their corresponding variable to be placed in a hash table. Then using a loop the program needs to continuously do this until the user types the exit statement to exit the loop. Which it adds to the variables just fine, but when I type exit, it just waits for what I assume is the delimiter character. But even then it doesn't exit.

Here's the whole code so far:

while (true) {

        getline(cin, name, ',');
        if (name == "exit") {
            break;
        }
        getline(cin, area, ',');
        cin >> cost;
        
        //code to see if the variables are placed
        cout << "results" << endl;
        cout << name << endl;
        cout << area << endl;
        cout << cost << endl;
        
}
    //code to check for exit
cout << "Loop has exited" << endl;

Mason Jar
  • 13
  • 3
  • What do you mean _exit_, going out of a loop (`break;`), or exiting the program completely (`exit(0);`)? – πάντα ῥεῖ Oct 06 '20 at 20:28
  • Exiting the loop – Mason Jar Oct 06 '20 at 20:30
  • do you mean the last line of input is "exit" and then you want to stop reading input? – 463035818_is_not_an_ai Oct 06 '20 at 20:30
  • Then `break;` should be fine. What's the problem actually? Post a [mcve] as required here please. – πάντα ῥεῖ Oct 06 '20 at 20:31
  • What is the value of `name` after you input the word "exit"? – jarmod Oct 06 '20 at 20:31
  • Yes! Sorry, I've been working on this for hours now and my brain is mush – Mason Jar Oct 06 '20 at 20:32
  • then take a break. The question is unclear. I don't understand how the title relates to the code and we need a [mcve] – 463035818_is_not_an_ai Oct 06 '20 at 20:34
  • 2
    @MasonJar Can you show us what you give as input at the console please? I am not so sure what you mean with _"clearing the buffer"_. Your `getline()` statements all have a `','` specified as delimiter, so you'll need to enter a `','` after `exit` to get the `getline()` triggered. – πάντα ῥεῖ Oct 06 '20 at 20:35
  • Seems like a good time to learn [what a debugger is and how it can help you](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). Run your program and check if the values of the variables are what you expect. But of course take your break first to get some fresh thoughts ;) – Lukas-T Oct 06 '20 at 20:37

1 Answers1

2

By default, std::getline() reads until '\n' (Enter) or EOF (Ctrl-Z on Windows, Ctrl-D on *Nix, etc) is encountered. If you specify a delimiter other than '\n', it will not stop reading on '\n' anymore. So, getline(cin, name, ','); will not stop reading when the user types in exit and presses Enter, it will stop only when the user types in a ',' character (or Ctrl-Z/Ctrl-D).

You should first use std::getline() with its default '\n' delimiter to read the user's entire input into a std::string until Enter is typed, then check if that string is "exit", and if not then you can use a std::istringstream to parse the string as needed, eg:

#include <string>
#include <sstream>

std::string line, name, area;
double cost;

while (std::getline(std::cin, line) && (line != "exit"))
{
    std::istringstream iss(line);
    std::getline(iss, name, ',');
    std::getline(iss, area, ',');
    iss >> cost;
        
    //code to see if the variables are placed
    std::cout << "results" << std::endl;
    std::cout << name << std::endl;
    std::cout << area << std::endl;
    std::cout << cost << std::endl;

    //add to heap command
}

//code to check for exit
std::cout << "Loop has exited" << std::endl;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Ooooohhh. Read the whole line and THEN parse it. My next stop was just using char arrays and like a million loops, so Thank you! – Mason Jar Oct 06 '20 at 21:00