Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Programming Q&A
My C++ console keeps returning the same value as the last.
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="patriarch" data-source="post: 472862" data-attributes="member: 105778"><p>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 [ICODE] ttoken = line.substr(0, pos); [/ICODE], 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.</p><p></p><p>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:</p><p></p><p></p><p>[CODE=cpp]#ifdef _WIN32</p><p>#include <Windows.h></p><p>#else</p><p>#include <unistd.h></p><p>#endif</p><p>#include <sstream></p><p>#include <iostream></p><p>#include <array></p><p>#include <vector></p><p>#include <map></p><p></p><p>int main()</p><p>{</p><p> std::string ttoken;</p><p> std::string index;</p><p> std::string token;</p><p> std::string dataEn1;</p><p> std::string dataEn2;</p><p> std::string dataEn3;</p><p> std::string dataEn4;</p><p> int count = 0;</p><p> std::string scoreDt = "";</p><p> std::string delimiter = ";;";</p><p> std::string line = "";</p><p> while (std::getline(std::cin, line)) {</p><p> if (line.empty())</p><p> continue;</p><p> size_t pos = 0;</p><p> while ((pos = line.find(delimiter)) != std::string::npos) {</p><p> ttoken = line.substr(0, pos);</p><p> // std::cout << ttoken << std::endl;</p><p> if (count == 0) {</p><p> index = ttoken;</p><p> }</p><p> else if (count == 1) {</p><p> token = ttoken;</p><p> }</p><p> else if (count == 2) {</p><p> scoreDt = ttoken;</p><p> }</p><p> else if (count == 3) {</p><p> dataEn1 = ttoken;</p><p> }</p><p> else if (count == 4) {</p><p> dataEn2 = ttoken;</p><p> }</p><p> else if (count == 5) {</p><p> dataEn3 = ttoken;</p><p> }</p><p> else {</p><p> dataEn4 = ttoken;</p><p> }</p><p> line.erase(0, pos + delimiter.length());</p><p> count++;</p><p> }</p><p> </p><p> if (ttoken == "ready") {</p><p> std::cout << "ok" << std::endl;</p><p> }</p><p> if (ttoken == "search") {</p><p> std::cout << "not ok!" << std::endl;</p><p> }</p><p> if (ttoken == "quit") {</p><p> std::cout << "bye" << std::endl;</p><p> break;</p><p> }</p><p> }</p><p> return 0;</p><p>}[/CODE]</p></blockquote><p></p>
[QUOTE="patriarch, post: 472862, member: 105778"] 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 [ICODE] ttoken = line.substr(0, pos); [/ICODE], 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: [CODE=cpp]#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; }[/CODE] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Programming Q&A
My C++ console keeps returning the same value as the last.
Top