#include <iostream>
int main()
{
int x{ 19 };
std::cout << "Hola!" << '\n';
std::cout << "Me llamo Kay\n";
std::cout << "And I am " << x << " years old\n";
std::cout << "Who are you?\n";
int y{};
std::cin >> y;
std::cout << "You are " << y << "?" << '\n';
return 0;
}
So i want the code to run a program that goes:
- Hola!
- Me llamo Kay
- And I am 19 years old
- Who are you?
- [user enters whatever]
- You are [user entered]?
But instead what I get is:
- Hola!
- Me llamo Kay
- And I am 19 years old
- Who are you?
- [user enters whatever]
- You are 0?
You declared y as an integer. This means y can only be used to contain a number. In your case, you want a to contain a std::string. This means any kind of text, like the text the user has entered. So simply change
int y{}intostd::string y;. And don't forget you can only declare a variable once in c++, so you'll have to remove one of the declarations for y.