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.

Respuesta :

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.

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.
Ver imagen codiepienagoya