Need help with beginner level C++. "A user types a word and a number on a single line. Read them into the provided variables. Then print: word_number. End with newline. Example - if user entered Amy 5, the output should be: Amy_5 ." Anyone able to explain how to do this?

#include
#include
using namespace std;

int main() {
string userWord;
int userNum;

// Put your solution here.



return 0;
}

Respuesta :

tonb
Often it is unwanted that cin doesn't read until a newline character, but in stead separates input at the space. For this exercise that's actually very convenient:

cin >> userWord;
cin >> userNum;
cout << userWord << "_" << userNum;

This will do the trick.

You will want to include <iostream> and <string>.

#include <iostream>

#include <string>

using namespace std;

int main() {

string word = "";

int userNum = 0;  

cout<<"Enter a word: ";

cin>>word;

cout<<"Enter a number: ";

cin>> userNum;

cout<<word<<"_"<<userNum<<endl;

return 0;

}