3

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));
}
user103722
  • 31
  • 2
  • Greetings, this code is broken for Linux, this site is not for broken code. I would suggest stackoverflow. Once the code is no longer broken, you should definitely come back, I see some good code reviewing feedback in there. – tomdemuyt Jul 21 '16 at 15:43
  • @konijn is correct, stackoverflow is a better place to ask this. Are you using Visual Studio, and if so did you specify C++ standard? – pacmaninbw Jul 21 '16 at 15:47
  • 3
    I have a felling it may be [this](http://stackoverflow.com/questions/16380966/non-const-reference-bound-to-temporary-visual-studio-bug) but without a [mcve] and a description of what *no longer works* is, it is hard to tell. – NathanOliver Jul 21 '16 at 15:53
  • @pacmaninbw using code::blocks 16.01 using the supplies mingw compiler and set to compile according to c++14 – user103722 Jul 21 '16 at 15:58
  • I suggest using `tolower()` or `toupper()` rather than using `'a' - 'A'`. Your letter subtraction may be based on a false assumption. – Thomas Matthews Jul 21 '16 at 17:14
  • @ThomasMatthews No assumptions made, C++ uses ascii I looked it up on the ascii chart. – user103722 Jul 21 '16 at 21:20
  • @NathanOliver good guess on the first one, but I'm using g++ on both systems to compile my code.... what happens is I get a string, then an int, then an unsigned int, then a char then when I try to get another string it sets a blank string. But only on linux. – user103722 Jul 21 '16 at 21:22

0 Answers0