How to Register A User Typing Something

Hello, I've just started programming this week.

I'm writing a basic Command Console program that I hope to someday make into a basic simulator. I would like to make it so that in the line "Type [A] to test money" you could type "A" in and it would trigger the "You gained $2,500" prompt. I've experimented with if's and else's, but as I said, I am new. If it's not too much to ask, I would like some help with this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

int main()


{
    char playerName[25];

    cout << "Enter your name: ";
    cin >> playerName;
    cout << "Hello, "<<playerName<<".\n";

    cout << "Type [A] to test money.\n";

    cout << "You gained $2,500.\n";

    return 0;
}
I'm new at this too, but this is one way of doing it:



#include <iostream>
#include <string>
using namespace std;

int main()
{
string playerName;
int test;

cout << "Enter your name: ";
cin >> playerName;
cout << "Hello, "<<playerName<<".\n";

cout << "Type [1] to test money.\n";
cin >> test;
if (test = 1)

{cout << "You gained $2,500.\n";}

return 0;
}
Last edited on
Try something like

1
2
3
4
5
6
7
8
char option;
cout << "Type [A] to test money." << endl;
cin >> option;

if( option == 'a' || option == 'A' ) //if you entered a or A
{
    cout << "You gained $2,500." << endl;
}


You may also want to check out sub functions and classes/structures.


Here's a quick demo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
//std:: is scoping to the std namespace.
struct Player
{
    std::string name;
    int money;
};

int main()
{
    Player p1{ "Giblit" , 2500 } , p2;

    std::cout << "Your name is " << p1.name << std::endl;
    std::cout << "You have $" << p1.money << " dollars." << std::endl;

    std::cout << "Enter player 2 name: ";
    std::cin >> p2.name; //getline if you want full name
    std::cout << "Enter your money " << p2.name << ": ";
    std::cin >> p2.money;


    std::cout << "Player 2 name: " << p2.name << std::endl;
    std::cout << p2.name << " has $" << p2.money << " dollars." << std::endl;

    return( 0 );
}



*had a missing semi-colon.
Last edited on
Thanks to both of you!

@giblit

I was actually going to make a "Player" class today, but I just wanted to do the "Test money" thing to learn how to do player input.

Last edited on
I'm new to this forum guys.. how can I create my own topic
at the top there is a button called "new topic" press it.
Topic archived. No new replies allowed.