I'm writing a console application for managing and tracking characters, monsters, turn order and conditions applied to make my battle run faster in DnD. The following code works perfectly on Windows but when I tried to compile it on my laptop, which runs Linux, it no longer works.
I input a name, then the initiative, then max health, then when I go to add another character it just reads a blank string and sets that as the name. I'm at a loss...
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
void gValid_Input(std::string& var, std::string question = "Add the carriage return manually if you want it...") {
using namespace std;
do {
cin.clear();
cin.sync();
cout << question;
} while (!getline(cin, var));
}
template <typename t>
void gValid_Input(t& var, std::string question = "Add the carriage return manually if you want it...") {
using namespace std;
do {
cin.clear();
cin.sync();
cout << question;
} while (!(cin >> var));
}
void gValid_Option(char& response, std::vector<char> valid_Responses = {'y','n'}){
using namespace std;
const char diff = 'a' - 'A';
do{
cin.clear();
cin.sync();
cin >> response;
if (response >= 'A' && response <= 'Z'){
response += diff;
}
} while (find(valid_Responses.begin(), valid_Responses.end(), response) == valid_Responses.end());
}
void gValid_Option(char& response, std::string question, std::vector<char> valid_Responses = {'y','n'}){
using namespace std;
const char diff = 'a' - 'A';
do{
cin.clear();
cin.sync();
cout << question;
cin >> response;
if (response >= 'A' && response <= 'Z'){
response += diff;
}
} while (find(valid_Responses.begin(), valid_Responses.end(), response) == valid_Responses.end());
}
SOLVED
void gValid_Input(std::string& var, std::string question = "Add the carriage return manually if you want it...") {
using namespace std;
do {
cin.clear();
cin.sync();
cout << question;
if (cin.peak() == '\n'){
cin.ignore(1, '\n');
}
} while (!getline(cin, var));
}