Home > OS >  C Writing Data From User Input Into txt File
C Writing Data From User Input Into txt File

Time:09-14

i'm trying to write a simple program in the console, simple program to store data that is input from the user. simple numbers. the result i'm getting is 0 0 0. i've been trying to prompt the user to input a day, month, and year, save that data to a file using the fstream library, and then read it and print it to the screen, but failed to understand what i'm doing wrong. any help ?

#include "iostream"
#include "string"
#include "fstream"

/*  ********************@ Salary Calculator @********************
*   Store the date of the day - 01.12.22 example V
*   Store Name of the day   
*   Store Number of hours of day
*   
*/

using std::cout; using std::cin; using std::string; using std::endl; 
using std::ios; using std::ofstream; using std::fstream;


int getDate(int day, int month, int year, char key)
{
    cout << "\nEnter the Day : "; cin >> day;
    cout << "\nEnter the Month : "; cin >> month;
    cout << "\nEnter the Year : "; cin >> year;

    cout << "\nDate Entered : " << day << "." << month << "." << year;
    cout << "\nWould You Like To Change The Date ? (Y/N) ";
    cin >> key;
    if (key == 'y')
    {
        int main();
    }
    else if (key == 'n')
    {
        cout << "\nDate Saved";
    }
    return day, month, year;
}    

void writeToFile(int day, int month, int year)
{
    ofstream Data;
    Data.open("Data.txt", ios::app);
    Data << day << endl;
    Data << month << endl;
    Data << year << endl;
    Data.close();
}

void readFromFile()
{
    fstream Data;
    Data.open("Data.txt", ios::in);
    if (Data.is_open())
    {
        string line;
        // getline() reads a line of text from file object (fstream Data) and stores it in string 'line'
        while (getline(Data, line)) 
        {
            cout << line << '\n';
        }
    }
    Data.close();
}

int main()
{    

    char key{};
    int day{}, month{}, year{};

    getDate(day, month, year, key);

    writeToFile(day, month, year);
    readFromFile();
    return 0; system("pause>0");
}

CodePudding user response:

There are a few issues with your code, I tried to fix most of them.

  1. As others suggested you should pass by reference to getDate() function. Otherwise you are simply passing copies of the values of day, month, year, key and therefore you are not actually changing their values in the main function.

  2. You can't call main function inside getDate() function. What you probably want to do is stay in a loop and keep reading the date, until the user enters n.

E.g.:

#include "iostream"
#include "string"
#include "fstream"

/*  ********************@ Salary Calculator @********************
*   Store the date of the day - 01.12.22 example V
*   Store Name of the day   
*   Store Number of hours of day
*   
*/

using std::cout; using std::cin; using std::string; using std::endl; 
using std::ios; using std::ofstream; using std::fstream;


void getDate(int &day, int &month, int &year, char &key) // Notice that I pass variables by reference
{
    bool change_date = true;
    // Loop until user enters n and the change_date flag changes to false
    while(change_date) { 
        cout << "\nEnter the Day : "; cin >> day;
        cout << "\nEnter the Month : "; cin >> month;
        cout << "\nEnter the Year : "; cin >> year;
    
        cout << "\nDate Entered : " << day << "." << month << "." << year;
        cout << "\nWould You Like To Change The Date ? (Y/N) ";
        cin >> key;
        if (key == 'y')
        {
            change_date = true;
        }
        else if (key == 'n')
        {
            cout << "\nDate Saved";
            change_date = false;
        }
    }
    return ;
}    

void writeToFile(int day, int month, int year)
{
    ofstream Data;
    Data.open("Data.txt", ios::app);
    Data << day << endl;
    Data << month << endl;
    Data << year << endl;
    Data.close();
}

void readFromFile()
{
    fstream Data;
    Data.open("Data.txt", ios::in);
    if (Data.is_open())
    {
        string line;
        // getline() reads a line of text from file object (fstream Data) and stores it in string 'line'
        while (getline(Data, line)) 
        {
            cout << line << '\n';
        }
    }
    Data.close();
}

int main()
{    

    char key{};
    int day{}, month{}, year{};

    getDate(day, month, year, key);

    writeToFile(day, month, year);
    readFromFile();
    return 0; system("pause>0");
}

CodePudding user response:

Instead of using int getDate(int day, int month, int year, char key) you need to use references with '&' symbol. int getDate(int &day, int &month, int &year, char &key).

It's all because when you are calling function, you send copy of your variables. For example, it means that in main() instead of sending variable int day{} you just simply sending a copy of this variable to function getDate(...).

You can learn more about references here. Good luck with future learning!

  • Related