|
-
January 7th, 2023, 03:20 PM
#1
Need help with
Hello,
As I'm trying to make a project that saves costs by user input into a CSV. File, I'm facing a problem that I can't resolve. In the first lines of the code I'm creating the part where I ask for the user input and store it in a CSV file. The problem is whenever I run the program again, it adds the new user input beside each other and not in the next cell below it. As you can see in the screenshot it says 1710 but 17 was the first input with the first run and 10 was the second input with the second run. I don't want this beside each other, but with each run a new cell under the previous input.
If you have any improvements, please share with me as i'm still learning.
If someone can help me with this. 
Code:
Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int cost;
int main(){
cout << "Hello, this is a tracker! \n" << endl;
cout << "Start by choosing your activity: \n" << endl;
cout << "1. Add costs: \n" << endl;
cout << "2. Show the content of the file: \n" << endl;
cout << "3. Exit \n " << endl;
int choice;
cin >> choice;
if ( choice == 1) {
cout << "Please give me your costs: \n" << endl;
cin >> cost;
cout << "Your cost will be added to your file! \n" << endl;
ofstream costfile;
costfile.open ("costfiles.csv", ios::app);
costfile << cost;
costfile.close();
}
if ( choice == 2) {
cout << "Ill give you the content, hold on! \n" << endl;
ifstream openfile;
openfile.open("costfiles.csv", ios::app);
while (openfile >> cost) { // This cost is for storing each value in the file
cout << cost << endl; // Displaying the values that has been saved in the variable 'cost'.
}
}
return 0;
}
Schermafbeelding 2023-01-08 040046.png
Last edited by 2kaud; January 8th, 2023 at 05:16 AM.
Reason: Added code tags
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|