I'm working on a formula calculator. In it, I've designated user controls (it's in a console). The script keeps track of the user inputs by assigning all user inputs concerning navigation into a string labeled 'ans'. However, I can't seem to get the 'if' and 'else' function to work with it. C++:

#include <conio.h>
#include <cstdlib>
using namespace std;
#include <iostream>
#include <string>;
#include <string.h>;
#include <sstream>;

int main()
{
    begin:
    cout << '\n';
    cout << "Meh";
    cout << '\n';
    string ans;   // U e  I p t
    cin >> ans;
    cout << "input is "<<ans;
    cout << '\n';
    getline (cin, ans); //  s r  n u
    if (ans == "begin"){
        cout << "You typed begin";
        _getch();
        goto begin;
    }
    else{
        cout << "You didn't type begin";
    }
    _getch();
    goto begin;
    return 0;
}

This is just a test script, not the full script. Using cout I've determined that it knows and remembers the 'ans' value is begin when entered, but the 'if' command says it isn't. Any help would be greatly appreciated, thanks!

Edited 3 Years Ago by Dani: Formatting fixed

I thought i did. Whoops. Ah well, time to try and figure out how to delete the other one

You place a cout statement to check for a value and then as for input to change that value again before using it. What is the reason of your call to getline ?

if (ans == "begin")

you can use strcmp from string.h library

ans is an std::string object, the == operator is properly overloaded, so this test is correct. The problem, I suspect, is that the code requests input twice and the OP has incorrect expectations.

Any help would be greatly appreciated, thanks!

No offense, but your code is awful. goto shouldn't be used where a simple loop works, and you don't have any conditions for breaking out of the loop. Notice that in both the "begin" and not "begin" conditions, the behavior is the same: print a message, call _getch(), and restart at the beginning. This can easily be accomplished with a loop:

#include <conio.h>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string ans;
    
    while (true) {
        cout << "Input: ";
        
        // Otherwise the loop is infinite
        if (!getline(cin, ans))
            break;
        
        if (ans == "begin")
            cout << "You typed 'begin'\n";
        else
            cout << "You didn't type 'begin'\n";
            
        _getch();
    }
}

And assuming the "begin" case is for restarting at the beginning and all other cases terminate the loop, you can now add a break or set a flag for the loop's condition in the else clause:

#include <conio.h>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    bool done = false;
    string ans;
    
    while (!done) {
        cout << "Input: ";
        
        // Otherwise the loop is infinite
        if (!getline(cin, ans))
            break;
        
        if (ans == "begin")
            cout << "You typed 'begin'\n";
        else {
            cout << "You didn't type 'begin'\n";
            done = true;
        }
            
        _getch();
    }
    
    cout << "All done\n";
}
This article has been dead for over six months. Start a new discussion instead.