0

I am learning basic C++ and I am trying to prompt the user for two inputs. The first is using cin for an int and the second for a string. The first input works perfectly; however, the second immediately exits. I tried searching online but they were only regarding multiline inputs.

Here is my code:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    std::cout << endl << "Basic example of char/int input: " << endl;
    int age;
    std::cout << "Enter your age: ";
    // std::cin >> age;  This can be used if it is the only input
    std::getline(std::cin, age);
    std::cout << "You are " << age << " years old!" << endl << endl;

    std::cout << "However, if we want a string, we don't use cin..." << endl;
    string name;
    std::cout << "Enter your name: ";
    std::getline(std::cin, name);

    std::cout << "Hello " << name << "!" << endl;
        
    return 0;
}
Polydynamical
  • 216
  • 2
  • 16

0 Answers0