I am new to C++ and I would like some help with creating a function that adds expenses into a text file. This is what would be inside of the text file?
Type of Expenses:Amount:Date
Listed below is an example of the code I am using but I get an error saying that too few arguments to function void addExpense(char,int,int)
.
Here's the code I am using:
#include <iostream>
#include <fstream>
using namespace std;
void addExpense(char expense, int amount, int date){
cout << "Type of Expense: " << endl;
cin >> expense;
cout << "Amount: " << endl;
cin >> amount;
cout << "Date: " << endl;
cin >> date;
}
int main(int argc, char** argv) {
addExpense();
return 0;
}
According to declaration, the method addExpense()
accepts 3 arguments:
void addExpense(char expense, int amount, int date);
But while calling, you are passing 0 argument:
addExpense();
Instead, do this:
addExpense(expense, amount, date);
But, in main()
since you do not have any variables, do this:
void addExpense(char expense, int amount, int date){
//implement the logic to write to file
}
int main(int argc, char** argv) {
char expense;
int amount, date;
cout << "Type of Expense: " << endl;
cin >> expense;
cout << "Amount: " << endl;
cin >> amount;
cout << "Date: " << endl;
cin >> date;
addExpense(expense, amount, date);
return 0;
}
You need to provide the arguments:char expense, int amount, int date.
For example:
#include <iostream>
#include <fstream>
using namespace std;
void addExpense(char expense, int amount, int date){
cout << "Type of Expense: " << endl;
cin >> expense;
cout << "Amount: " << endl;
cin >> amount;
cout << "Date: " << endl;
cin >> date;
}
int main(int argc, char** argv) {
char expense;
int amount;
int date;
addExpense(expense, amount, date);
return 0;
}
According to function declaration, the method addExpense() accepts 3 arguments:
void addExpense(char expense, int amount, int date);
But in the function calling, you are passing 0 argument:
addExpense();
Instead, try this:
char expense = e; int amount = 50, int date = 20150119; addExpense(expense, amount, date);
I think the main thrust of the question (beyond the compiler error) goes towards how to write into or rather append to a text file.
The mechanism typically used to interact with files is a filestream (here: ofstream = output-filestream). Try adding a filename argument to your addExpense() function like this (and since you read the variables from user input inside the function, I would also declare them there and not make them arguments):
void addExpense(const std::string& filename);
and add the following code inside the function:
void addExpense(const std::string& filename){
//local variable declarations (no need to pass them):
char expense;
int amount;
int date;
//user input
cout << "Type of Expense: " << endl;
cin >> expense;
cout << "Amount: " << endl;
cin >> amount;
cout << "Date: " << endl;
cin >> date;
//new: append a line to the file
std::ofstream out(filename, ios::append);
out << expense << ":" << amount << ":" << date << std::endl;
out.close(); //important for making sure that everything is actually written to file ("flush" the stream)!
}
The function is then called like this:
addExpense("c:/myfile.txt");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.