My C++ console keeps returning the same value as the last.

omatamix

New Member
Feb 20, 2019
18
6
For some reason the second command I send to my C++ console it keeps returning the same value as the last.

C++:
std::string delimiter = ";;";
std::string line = "";
while (std::getline(std::cin, line)) {
    size_t pos = 0;
    while ((pos = line.find(delimiter)) != std::string::npos) {
        ttoken = line.substr(0, pos);
        // std::cout << ttoken << std::endl;
        if (count == 0) {
            index = ttoken;
        } else if (count == 1) {
            token = ttoken;
        } else if (count == 2) {
            scoreDt = ttoken;
        } else if (count == 3) {
            dataEn1 = ttoken;
        } else if (count == 4) {
            dataEn2 = ttoken;
        } else if (count == 5) {
            dataEn3 = ttoken;
        } else {
            dataEn4 = ttoken;
        }
        line.erase(0, pos + delimiter.length());
        count++;
    }
    if (index == "ready") {
        std::cout << "ok" << std::endl;
    }
    if (index == "search") {
        // set position var here.
        bestMove = search.findBestMove(position);
        lastBestScore = search.getLastBestScore();
        std::cout << bestMove.from << "|" << bestMove.to << "|" << bestMove.bit << "|" << lastBestScore << std::endl;
    }
    if (index == "quit") {
        break;
    }
}

I think the problem lies in this part of this code. If you need need more part of the code let me know.
 

Attachments

  • Capture1667722.PNG
    Capture1667722.PNG
    34.3 KB · Views: 11
Solution
C++:
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
#include <sstream>
#include <iostream>
#include <array>
#include <vector>
#include <map>

int main()
{
    std::string ttoken;
    std::string index;
    std::string token;
    std::string dataEn1;
    std::string dataEn2;
    std::string dataEn3;
    std::string dataEn4;
    int count = 0;
    std::string scoreDt = "";
    std::string delimiter = ";;";
    std::string line = "";
    while (std::getline(std::cin, line)) {
        size_t pos = 0;
        while ((pos = line.find(delimiter)) != std::string::npos) {
            ttoken = line.substr(0, pos);
            // std::cout << ttoken << std::endl;
            if (count == 0) {
                index = ttoken...

patriarch

New Member
Jan 23, 2022
4
1
I'm going to be quite honest, I don't see how the code provided is related to the console screenshot, if you could show more of the code it would be easier to see where the issue is :)
 

omatamix

New Member
Feb 20, 2019
18
6
I'm going to be quite honest, I don't see how the code provided is related to the console screenshot, if you could show more of the code it would be easier to see where the issue is :)
C++:
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
#include <sstream>
#include <iostream>
#include <array>
#include <vector>
#include <map>

int main()
{
    std::string ttoken;
    std::string index;
    std::string token;
    std::string dataEn1;
    std::string dataEn2;
    std::string dataEn3;
    std::string dataEn4;
    int count = 0;
    std::string scoreDt = "";
    std::string delimiter = ";;";
    std::string line = "";
    while (std::getline(std::cin, line)) {
        size_t pos = 0;
        while ((pos = line.find(delimiter)) != std::string::npos) {
            ttoken = line.substr(0, pos);
            // std::cout << ttoken << std::endl;
            if (count == 0) {
                index = ttoken;
            } else if (count == 1) {
                token = ttoken;
            } else if (count == 2) {
                scoreDt = ttoken;
            } else if (count == 3) {
                dataEn1 = ttoken;
            } else if (count == 4) {
                dataEn2 = ttoken;
            } else if (count == 5) {
                dataEn3 = ttoken;
            } else {
                dataEn4 = ttoken;
            }
            line.erase(0, pos + delimiter.length());
            count++;
        }
        if (index == "ready") {
            std::cout << "ok" << std::endl;
        }
        if (index == "search") {
            //.
        }
        if (index == "quit") {
            break;
        }
    }
    return 0;
}

I am still learning C++, so don't judge xD.

The program returns what sould be returned when I call each command, It's when i call a scond command it keeps outputting the same value as the first command. So if i do ready;; it returns ok as it should and then if i do quit;; it returns ok which it should not. But if i restart the console and do the quit command first it stops the program as it should.

I just updated the code above so It would be easier to see what is wrong.

You can compile this yourself now and you will see the problem I am having.
 
Last edited:

patriarch

New Member
Jan 23, 2022
4
1
C++:
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
#include <sstream>
#include <iostream>
#include <array>
#include <vector>
#include <map>

int main()
{
    std::string ttoken;
    std::string index;
    std::string token;
    std::string dataEn1;
    std::string dataEn2;
    std::string dataEn3;
    std::string dataEn4;
    int count = 0;
    std::string scoreDt = "";
    std::string delimiter = ";;";
    std::string line = "";
    while (std::getline(std::cin, line)) {
        size_t pos = 0;
        while ((pos = line.find(delimiter)) != std::string::npos) {
            ttoken = line.substr(0, pos);
            // std::cout << ttoken << std::endl;
            if (count == 0) {
                index = ttoken;
            } else if (count == 1) {
                token = ttoken;
            } else if (count == 2) {
                scoreDt = ttoken;
            } else if (count == 3) {
                dataEn1 = ttoken;
            } else if (count == 4) {
                dataEn2 = ttoken;
            } else if (count == 5) {
                dataEn3 = ttoken;
            } else {
                dataEn4 = ttoken;
            }
            line.erase(0, pos + delimiter.length());
            count++;
        }
        if (index == "ready") {
            std::cout << "ok" << std::endl;
        }
        if (index == "search") {
            //.
        }
        if (index == "quit") {
            break;
        }
    }
    return 0;
}

I am still learning C++, so don't judge xD.

The program returns what sould be returned when I call each command, It's when i call a scond command it keeps outputting the same value as the first command. So if i do ready;; it returns ok as it should and then if i do quit;; it returns ok which it should not. But if i restart the console and do the quit command first it stops the program as it should.

I just updated the code above so It would be easier to see what is wrong.

You can compile this yourself now and you will see the problem I am having.

I didn't want to fiddle around with your code too much, I'll speak as redundant as I can so you can try follow up. Whenever you wrote something to the console it went through ttoken = line.substr(0, pos); , to remove the "delimiter", now according to the counter you've placed inside the while loop, it will increment every time you run some new input. This ultimately is the simple issue that is repeating the same output every time, i.e. the definition of the string index which occurs at first input.

So all this being said, the variable index wasn't being updated (as it should be), since I didn't want to mess around with the code too much, I simply used a variable that was being updated accordingly, here is the code:


C++:
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
#include <sstream>
#include <iostream>
#include <array>
#include <vector>
#include <map>

int main()
{
    std::string ttoken;
    std::string index;
    std::string token;
    std::string dataEn1;
    std::string dataEn2;
    std::string dataEn3;
    std::string dataEn4;
    int count = 0;
    std::string scoreDt = "";
    std::string delimiter = ";;";
    std::string line = "";
    while (std::getline(std::cin, line)) {
        if (line.empty())
            continue;
        size_t pos = 0;
        while ((pos = line.find(delimiter)) != std::string::npos) {
            ttoken = line.substr(0, pos);
            // std::cout << ttoken << std::endl;
            if (count == 0) {
                index = ttoken;
            }
            else if (count == 1) {
                token = ttoken;
            }
            else if (count == 2) {
                scoreDt = ttoken;
            }
            else if (count == 3) {
                dataEn1 = ttoken;
            }
            else if (count == 4) {
                dataEn2 = ttoken;
            }
            else if (count == 5) {
                dataEn3 = ttoken;
            }
            else {
                dataEn4 = ttoken;
            }
            line.erase(0, pos + delimiter.length());
            count++;
        }
    
        if (ttoken == "ready") {
            std::cout << "ok" << std::endl;
        }
        if (ttoken == "search") {
            std::cout << "not ok!" << std::endl;
        }
        if (ttoken == "quit") {
            std::cout << "bye" << std::endl;
            break;
        }
    }
    return 0;
}
 
Solution

Users who are viewing this thread

Top