Mike Sherwin wrote: ↑Tue Oct 06, 2020 7:05 am
I don't know if I had an epiphany or just another one of my many duh moments. So, when I was contemplating the situation I was staring at a cout line. Then it hit me, if there is a cout then there must be a cin. Problem solved! I spent hours and hours because I forgot that cin existed. Dumb Donkey strikes again.
I'm so tired by not being able to remember.
Turns out that I had neither an epiphany or a duh moment. I looked at dozens and dozens of replies to this simple question, 'how do I parse an unknown number of inputs in C++'. And no one had the answer. All the replies required hitting enter more than once. I was about to quit looking. But, I decided to change the google search phrase and look again. Once again there were no good answers. I was again going to call it quits but decided to click on one more search result. I looked through the answers and found one that was different that looked like it would work. And it did. And it was so simple even I could understand it. That demonstrates what is so wrong with C++. When all those pretend experts that answer peoples questions can't get it right then how does a hobbyist or amature or someone like me have a snowball's chance in hell to figure out how to do something? I wish that I would have stuck with C code. Here is a simple and correct solution.
Code: Select all
void GetCmd(Thread* t) {
s32 match, i, j = 0, fs, ts;
u64 n, cnt, epbit;
string cmd, data, movstr;
char mvstr[20];
match = false;
epbit = epbb[1];
PrintBoard(t);
cout << "Enter Command: ";
getline(cin, cmd);
istringstream iss(cmd);
iss >> data;
cnt = data.length();
if (cnt == 4 || cnt == 5) {
if (data[0] >= 'a' && data[0] <= 'h' &&
data[1] >= '1' && data[1] <= '8' &&
data[2] >= 'a' && data[2] <= 'h' &&
data[3] >= '1' && data[3] <= '8') {
epbb[0] = epbit;
n = GenMoves(t, &move[0]);
for (i = OO; i < n; i++) {
if (cnt == 5) {
if (data[5] == 'b') j = 0;
if (data[5] == 'r') j = 1;
if (data[5] == 'n') j = 2;
if (data[5] == 'q') j = 3;
if (promoType[wtm][j] != move[i].type) continue;
}
fs = move[i].fs;
ts = move[i].ts;
sprintf_s(mvstr, 20, "%c%d%c%d\n", (fs & 7) + 'a', (fs >> 3) + 1, (ts & 7) + 'a', (ts >> 3) + 1);
string movstr = mvstr;
if (!data.compare(movstr)) {
gameMoves[gamePly].fs = fs;
gameMoves[gamePly].ts = ts;
gameMoves[gamePly].type = move[i].type;
MakeMove(t, &gameMoves[gamePly]);
epbb[0] = epbit;
gamePly++;
ply--;
return;
}
}
return;
}
}
if (!data.compare("go")) {
bricabrac = BRICABRAC;
return;
}
if (gamePly && (!data.compare("u") || !data.compare("undo"))) {
gamePly--;
ply++;
TakeBack(t, &gameMoves[gamePly]);
return;
}
if (!data.compare("sd")) {
iss >> data;
sd = stoi(data, nullptr);
}
}
And this is what only that one poster offered.
istringstream iss(cmd); // sets cmd to be accessed by istringstream
iss >> data; // gets the first parameter
then.
if (!data.compare("sd")) {
iss >> data; // gets the next parameter
sd = stoi(data, nullptr);
}