int month;
int day;
int year;
};
class Session
{
public:
// file pointer scoped to session class (property of class)
//OPENS AND WRITES TO CSV FILE (USING USER INPUT) https://linuxhint.com/cpp-write-csv-file/
fstream file; // declare fout variable using fstream,which is the datatype that denotes the file stream and allows us to perform read,write, update operations on files
void openFile()
{
// EACH TIME A NEW CSV FILE IS CREATED ASSOCIATED WITH THE USER OBJECT
// open() opens the csv file - automatically opens at the start of each session
file.open("Transfers.csv", ios::out); //iosout - used to open the file for writing purposes
file << "Date" << "," << "Description" << "," "Income" << "," << "Expense" << "," << "Net balance" << "\n"; // csv column headers
//file << "Expense" << "," << "Income" << "," << "Balance" << "\n"; // csv column headers
};
void createTransfer() // void - returns nothing, no return statement. all functions require a return type
{
void openFile(); // call function that opens file
Date mydate; // create an instance of the struct
int date;
double income, expense, running_balance;
string description;
cout << "Enter in the following details in order to create the transfer." << endl;
cout << "Day (integer only) : " << endl;
cin >> mydate.day;
// only enter between 0 -31
cout << "month (integer only) : " << endl;
cin >> mydate.month;
// make it so they can only enter between 0 - 12
cout << "Year (integer only) : " << endl;
cin >> mydate.year;
// make it so they can only enter until 2023
// format the information properly
date = mydate.day << "/" << mydate.month << "/" << mydate.year; ```
#date struct, type issue
28 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
@deep harness
I understand that I have them all listed as ints in the struct
and I understand the line which has the issue
I see. So you're just wondering how to concatenate all that information and represent as a string, correct?
These are the errors btw if u didn't see them earlier :
I can't use the '+' operator I know that
I'm assuming type conversion?
Not necessarily. It depends on the design mostly
Kk I just want to know the simplest way so I can just do it for that one line
Does it necessarily have to be a string?
can I make it all an int?
It doesn't
An int stores a number. From what I understand from that piece of code, you want to store a preformatted date
Which would be a string (no matter if we mean a C-string, std::string, or something else. Just a sequence of characters)
There are many ways to solve the problem. I'll list two
i declared the 'date' variable as an int though should I take that out?
The first solution would be to use statically allocated arrays of characters. For example, if you know all your dates will take at most 10 characters (2 for the day, 2 for the month and 4 for the year + slashes between them), you could allocate an array of 11 characters (because the null character – '\0 – must be included at the end). Then you could do something like itoa (https://cplusplus.com/reference/cstdlib/itoa/) that encodes a number
int n = 33;
char my_number[5];
char* end = itoa(n, my_number, 10);
assert(end - my_number == 2);
Then you could use it to encode the day, month and year one by one, and manually add /s between the numbers. However, aside from it being a C-like solution, it has also other disadvantages. First of all, you can't return a C-array from a function. You need to wrap it in a struct. Second, if you decided to somehow change the format of the thing you store, chances are you might need to significantly change your logic.
Another solution is to use std::string. In that case, it simplifies a lot to something like
std::to_string(mydate.day) + "/" + std::to_string(mydate.month) + "/" + std::to_string(mydate.year);
I'll use the second thanks
No worries
But keep in mind the result needs to be stored as std::string, not as an int
wait so I set all that equal to date and I set the date variable now equal to a string so it's no longer an int
Oh, one more thing that may be useful for you. If you decided to start the string with an /, then you would need to turn it into a string:
// Wrong!
"/" + std::to_string(mydate.date) + /* ... */
// Correct
std::string("/") + std::to_string(mydate.date) + /* ... */
// Also correct
using std::literals::string_literals::operator""s;
"/"s + std::to_string(mydate.date) + /* ... */
Could you elaborate?
The type on the left-hand side needs to be an std::string too
std::string result = std::to_string(mydate.day) + "/" + std::to_string(mydate.month) + "/" + std::to_string(mydate.year);
@plush blade Has your question been resolved? If so, run !solved :)
!solved