Register User Information and Save It to a File in C++

In this C++ exercise, you will create a program that collects basic user information—specifically, their name and age—and writes it into a text file. This task introduces you to file output operations using the `ofstream` class from the `` library. You will learn how to open a file for writing, gather input from the user, and write formatted text into the file. This exercise is foundational for understanding data persistence and file manipulation in C++.

Group

File Management in C++

Objective

1. Prompt the user to enter their name and age.
2. Open a file in write mode using `ofstream`.
3. Write the collected information into the file.
4. Close the file once the writing operation is complete.
5. Inform the user that their information has been saved.

Create a program that records information into a file (such as name and age).

Example C++ Exercise

 Copy C++ Code
#include <iostream>      // Include for standard input and output
#include <fstream>       // Include for file output stream
#include <string>        // Include for using the string class

using namespace std;

int main() {
    string name;         // Variable to store the user's name
    int age;             // Variable to store the user's age

    // Prompt the user to enter their name
    cout << "Enter your name: ";
    getline(cin, name);  // Read the full name including spaces

    // Prompt the user to enter their age
    cout << "Enter your age: ";
    cin >> age;           // Read the user's age

    ofstream outFile("userdata.txt");  // Create and open a file named 'userdata.txt'

    // Check if the file was opened successfully
    if (outFile.is_open()) {
        // Write the name and age into the file
        outFile << "Name: " << name << endl;
        outFile << "Age: " << age << endl;

        outFile.close();  // Close the file after writing
        cout << "Information saved successfully to 'userdata.txt'." << endl;
    } else {
        // Display an error if the file could not be opened
        cout << "Error: Could not open file for writing." << endl;
    }

    return 0;  // End of program
}

 Output

//Output Example:
Enter your name: Alice Johnson
Enter your age: 28
Information saved successfully to 'userdata.txt'.

//Content of userdata.txt:
Name: Alice Johnson
Age: 28

Share this C++ Exercise


More C++ Programming Exercises of File Management in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.

  • Count Words in a Text File Using C++

    In this exercise, you will write a C++ program that reads a text file and counts the total number of words contained in it. This task will help you become familiar with file input ...

  • Copy File Contents Using C++

    This exercise involves writing a C++ program to copy the entire content of one file into another. By completing this task, you will practice using file streams in C++, specifically...

  • Find and Replace a Word in a File Using C++

    In this exercise, you will create a C++ program that searches for a specific word in a text file and replaces it with another word. This task will help you strengthen your understa...

  • Save and Load a List of Numbers from a File in C++

    In this exercise, you will create a C++ program that allows the user to input a list of numbers, save them to a text file, and then read and display those numbers from the file. Th...

  • Count the Number of Lines in a Text File Using C++

    This exercise guides you through the process of writing a C++ program that reads a text file and counts the number of lines it contains. This is a fundamental operation when analyz...

  • Sort the Lines of a Text File Using C++

    This exercise involves creating a C++ program that reads all lines from a text file, sorts them in alphabetical order, and then displays the sorted lines on the screen. Sorting fil...