40.2k views
3 votes
Write a program that read first a user's given name followed by the user's age from standard input. Then use an ofstream object named outdata (which you must declare) to write this information separated by a space into a file called outdata. Assume that this is the extent of the output that this program will do. Declare any variables that you need.

User Eli Hooten
by
3.4k points

1 Answer

4 votes

Answer:

Following are the program to this question:

#include <iostream> //defining header file

#include <fstream>//defining header file

using namespace std;

int main()//defining main method

{ string name; //defining string variable

int age; //defining integer variable

cout << "Enter your name: "; //print message

getline(cin, name); //input value using getline method

cout << "Enter your age: "; //print message

cin >> age; //input value

ofstream outdata("outdata"); //using ofstream method

outdata << name << " " << age << endl; //print value

outdata.close(); //closing outdata.

return 0;

}

Output:

Enter your name: dev

Enter your age: 22

please find the attachment.

Step-by-step explanation:

Description of the above can be described as follows:

  • In the main method, two variable "name and age" is declared, in which name is string variable, and age is an integer variable, in both variable, we take user input, but in the name variable, the inline method is used, that take input from the user end.
  • In the next step, ofstream method is used, which creates an object "outdata", which prints user input value in the "outdata" file separated by a single space.
Write a program that read first a user's given name followed by the user's age from-example-1
User Frodon
by
3.2k points