-3

I'm working on a simplish game (this isn't the whole code, just the bit that I'm having issues with) and I've run into this issue; After the condition is furfilled, it goes back to the start and it offers me to reenter the string, however, whatever I enter, I just get 'Not Valid'. Does anyone know why? I'm using the GNU C++ Compiler.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string command;
mainscreen:
    cout << "blab";
getlinething:
    cin.ignore();
    getline(cin, command);
    if (command == "task")
    {
        goto mainscreen;
    }
    else
    {
        cout << "Not valid.";
        goto getlinething;
    }
    return 0;
}
Whealty_
  • 1
  • 1
  • 1
    Please don't use `goto`. It is very confusing to read for anyone else and bad practice. You should use a `while` loop to do what you're trying to do. – TheValyreanGroup Nov 19 '16 at 21:57
  • Thing is that this is only a subprogram in the complete thing, I was pondering at the idea with goto, but I can't really figure out how to connect the subprograms without using goto, even if i used while. – Whealty_ Nov 19 '16 at 22:01
  • @Whealty_ Use std::wstring and wcout and wcin. – Vlad from Moscow Nov 19 '16 at 22:09
  • @VladfromMoscow then it can't compile. It reports an error 'no match for operator= operand types are 'wstring' and 'const char [4]'. GNU GCC Compiler – Whealty_ Nov 19 '16 at 22:35
  • @Whealty_ Use L"task" and so on that is use prefix L before string literals. – Vlad from Moscow Nov 19 '16 at 22:38
  • Nope, tried it with L"task" and it doesn't work. Same issues as described in my comment below. – Whealty_ Nov 19 '16 at 22:44
  • 1
    Have you actually verified that the code you have posted HERE actually exhibits the behaviour you describe? If it is occurring with similar code within a larger program, and you leave out relevant code from that other program, nobody can reasonably be expected to help you. – Peter Nov 19 '16 at 23:13
  • @Peter I don't know why are you asking that, but yes I have verified that the code posted here has the same issue as the code used in the application. – Whealty_ Nov 20 '16 at 07:40

4 Answers4

1

When I run your code with a debug print it shows that each time you read a new command you are loosing the first char of the string. In fact, when I remove your cin.ignore() it works fine. Also, have a look at this while to see if it fits your needs:

cout << "blab";
while(1){
    getline(cin, command);
    if(command == "task"){
        cout << "blab";
        getline(cin, command);
    }
    else{
        cout << "Not valid.";
    }

}
Drimades Boy
  • 437
  • 1
  • 5
  • 19
0

For debugging purpose at least, why not do

cout << "'" << command << "' Not valid" << endl ;
user3344003
  • 20,574
  • 3
  • 26
  • 62
0

Alright, I tested it out. Without cin.ignore(), I cannot enter the data into the string at all. The first time I enter it captures everything. So if I wrote task, the string would say 'task', however the second time I entered it, it would say 'ask'. I don't really know why it's doing that.

Whealty_
  • 1
  • 1
0

The cin.ignore() line will always discard one character by default (unless it encounters EOF, which would be a fairly deliberate act on cin).

So, let's say the user enters task and then hits the enter key. The cin.ignore() will discard the 't', and the command string will contain "ask". If you want to get a match, the first time through, the user will need to enter ttask. The newline will be discarded, in either case. The same will happen until a match is encountered.

Peter
  • 35,646
  • 4
  • 32
  • 74
  • I'm not really sure, because the first time I enter 'task' it goes through, and the string says 'task' – Whealty_ Nov 20 '16 at 10:50