MHB How can I use GetUserInfo to get a user's age and name?

  • Thread starter Thread starter Teh
  • Start date Start date
  • Tags Tags
    Function
Click For Summary
The discussion revolves around using a function to retrieve user information and display it correctly. The function GetUserInfo is designed to take user input for age and name. The main issue highlighted is the need to call this function properly within the main program to ensure that the variables userAge and userName are populated with user input. The expected output format is specified, demonstrating that if the user inputs "20" for age and "Holly" for name, the program should output "Holly is 20 years old." The key takeaway is the importance of correctly invoking the GetUserInfo function with the right parameters to achieve the desired output.
Teh
Messages
47
Reaction score
0
I am not sure if should i use a for loop or if statement...may anyone give me a hint what i am suppose to do?Use function GetUserInfo to get a user's information. If user enters 20 and Holly, sample program output is:

Holly is 20 years old.
Code:
#include <iostream>
#include <string>
using namespace std;
void GetUserInfo(int& userAge, string& userName) {
   cout << "Enter your age: " << endl;
   cin >> userAge;
   cout << "Enter your name: " << endl;
   cin >> userName;
   return;
}
int main() {
   int userAge = 0;
   string userName = "";
   /* Your solution goes here  */
   
   cout << userName << " is " << userAge << " years old." << endl;
   return 0;
}
Testing with inputs 20 and Holly
Expected output: Holly is 20 years old.
Your output: is 0 years old.
 
Technology news on Phys.org
All you have to do is call [m]GetUserInfo[/m] with the appropriate parameters.
 

Similar threads

Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 66 ·
3
Replies
66
Views
5K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K