Alphanian
Alphanian

Reputation: 9

How to use cin inside a function?

I am new to c++ and am trying to make a simple text based game. The code below is supposed to welcome the user to the game and ask whether or not the user wants to start the game. I don't get any build errors, but when I run it all it shows is a blank screen. What exactly am I doing wrong?

Code below:

string D1(string Original)
{
    cin >> Original;
    return Original;
}

int main ()
{
    string Decision1;
    cout << "Welcome to [game name]" << endl;
    cout << "Would you like to start the game?" << endl;
    cout << "Yes/No" << endl;
    string D1(Decision1);
    system("cls");
    if (Decision1 == "Yes")
    {
        cout << "this happens" << endl;
    }
    else if (Decision1 == "No")
    {
        cout << "that happens" << endl;
    }
}

Upvotes: 0

Views: 11571

Answers (4)

Ferenc Deak
Ferenc Deak

Reputation: 35408

string D1(string Original) should be string D1(string& Original) (ie: passing with reference)

and

string D1(Decision1); in main should be

D1(Decision1); because

string D1(Decision1); actually declares a new string variable, called D1 (same name as the function) and initializes it with the value of Decision1

Upvotes: 6

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

This statement

string D1(Decision1);

is a definitiom of an object of type std::string with name D1 that initialized by object Decision1. As the object Decision1 is empty then and object D1 is empty.

What you meant is the following

void D1( string &Original )
{
    cin >> Original;
}

//...

D1( Decision1 );

or the following

string D1()
{
    string Original;
    cin >> Original;
    return Original;
}

//...

Decision1 = D1();

Take into account that if you use two if statements

if (Decision1 == "Yes")
{
    cout << "this happens" << endl;
}
else if (Decision1 == "No")
{
    cout << "that happens" << endl;
}

then you have to add a third statement with else because the user can enter neither "Yes" nor "No". Also you should compare strings independently of their case (upper case or lower case). So you have to convert strings to some common case.

For example

#include <cctype>

//...

for ( char &c : Decision1 ) c = std::toupper( c );
if ( Decision1 == "YES" )
{
    cout << "this happens" << endl;
}
else if ( Decision1 == "NO" )
{
    cout << "that happens" << endl;
}
else
{
    //...
}

P.S. And here in comments there was suggested book "Programming -- Principles and Practice Using C++. " I do not advice this book for beginners. In fact it is a bad book for beginners. You will spend much time reading the book but your knowledge of C++ will be close to zero.

Upvotes: 3

scohe001
scohe001

Reputation: 15446

string D1(string Original)

Right now, the program will make a copy of the string you pass and place it into Original, so when you come out of the function, nothing will have happened to Decision1.

What you want it to do is pass the variable by reference.

Passing a variable by reference means the function isn't receiving a copy of what was passed, but the actual variable you passed (imagine a voodoo doll). Anything you do to the variable in the function will take effect on the variable passed.

Change the function header to:

string D1(string &Original)

Upvotes: 2

user3892448
user3892448

Reputation:

You have to call D1, not create a new variable named D1:

std::string D1()
{
    std::string Original;
    std::cin >> Original;
    return Original;
}

int main ()
{
    std::cout << "Welcome to [game name]" << std::endl;
    std::cout << "Would you like to start the game?" << std::endl;
    std::cout << "Yes/No" << std::endl;
    auto Decision1 = D1();
    system("cls");
    if (Decision1 == "Yes")
    {
        std::cout << "this happens" << std::endl;
    }
    else if (Decision1 == "No")
    {
        std::cout << "that happens" << std::endl;
    }
}

Upvotes: 7

Related Questions