-1

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");
}
  • In C++ it's not allowed to call [the `main` function](https://en.cppreference.com/w/cpp/language/main_function) from inside the program itself. That leads to *undefined behavior*. If you need a loop, use actual loops. – Some programmer dude Sep 14 '22 at 06:35
  • Also please read [What is the difference between #include and #include "filename"?](https://stackoverflow.com/questions/21593/what-is-the-difference-between-include-filename-and-include-filename) For standard and system header files use `<>`. – Some programmer dude Sep 14 '22 at 06:36
  • @Someprogrammerdude the program doesn't call `main` though, – n. m. could be an AI Sep 14 '22 at 06:36
  • 1
    And `return day, month, year;` will not work as you expect. It seems you need to invest in [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), and learn some of the basics you have missed. – Some programmer dude Sep 14 '22 at 06:37
  • That would be about a book chapter worth of material to write. I'm a bit busy right now but if the pay is right I might consider doing that. Of course just fixing your code is much easier and someone will surely do that in the next five minutes, but you are not going to learn anything from that. – n. m. could be an AI Sep 14 '22 at 06:46
  • Totally agree with you my friend !, i don't want to copy i want to understand better. could you at least point me to the specific subjects you think i need to go over ? – matan izhak Sep 14 '22 at 06:54
  • you already got many useful links, including this one https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – 463035818_is_not_an_ai Sep 14 '22 at 07:55

2 Answers2

2

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");
}
Aristotelis V
  • 396
  • 1
  • 9
0

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!

IncPink
  • 34
  • 4