0

so I'm working on a homework problem for my CS175 C++ course. We have a homework assignment where we have to make a shorting hat, kinda like what's in Harry Potter. I've got 99% of the code down, however the thing that is tripping me up is how to read in a string with white space.

We need to be able to input full names so obviously just using std::cin >> won't work. The problem is that I can't seem to get any of the methods to work that I've tried so far.

This is my code:

#include <iostream>
#include <string>

int main()
{
    int NumStudents;
    std::string NameStudents;
    int StartValue;
    int House;
    std::string HouseName;
    int NumCornfolk = 0;
    int NumEsophagus = 0;
    int NumBob = 0;


    //How many students are there?
    std::cout << "How many students are there? \n";
    std::cin >> NumStudents;


    for (StartValue = 0; StartValue < NumStudents; StartValue++) {
        std::cout << "Please enter the name of the next student. \n";
        std::cin >> NameStudents; \\**THE PROBLEM IS HERE**
        //Assings the House
        House = rand() % 100 + 1;
        if (House <= 19) {
            HouseName = "Cornfolk! \n";
            NumCornfolk++;
        }
        else if (House > 19 && House < 50) {
            HouseName = "Esophagus! \n";
            NumEsophagus++;
        }
        else if (House >= 50) {
            HouseName = "Bob! \n";
            NumBob++;
        }

        std::cout << NameStudents << " got " << HouseName << std::endl;


    }
    //Prints Results
    std::cout << "Number of Students in each House: \n";
    std::cout << "Cornfolk:" << NumCornfolk << " Esophagus:" << NumEsophagus << " Bob:" << NumBob;
}

The line of code that reads std::cin >> NameStudents; is what's causing the problem. I've seen methods that say to use something along the lines of "std::cin.getline (name,256);" but cin.getline throws an error at the period and won't compile.

Being able to read in the names correctly is only 2/11 points, so it's not that big of a deal, but I would like to know why the suggested methods are not working here.

Thank you. This question is different from ones asked before mods.

1 Answers1

1

Use std::getline, like this:

std::getline(std::cin, NameStudents);

Here's an example from https://en.cppreference.com/w/cpp/string/basic_string/getline:

std::string name;
std::cout << "What is your name? ";
std::getline(std::cin, name);
std::cout << "Hello " << name << ", nice to meet you.\n";
James
  • 270
  • 1
  • 10
  • That allows me to input white spaces, however it breaks the following code. Skipping one of the if statements entirely after pressing enter/return. – Eric Melendez Sep 16 '19 at 00:11
  • I'm not sure what you mean. It shouldn't skip any code. What are you seeing at this line "std::cout << NameStudents << " got " << HouseName << std::endl"? – James Sep 16 '19 at 04:16
  • After pressing return it outputs that line except for NameStudents. By hitting the return key, it doesn’t log the first name and skips all the if checks for the first run. – Eric Melendez Sep 17 '19 at 05:04