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.