Click here to Skip to main content
15,881,801 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add users input to a txt file with the 'fstream' library. Im trying to make a budget tracker. I tried it but it didn't work, ill leave the code here so you can see what's the problem.
C++
#include <iostream>
#include <vector>
#include <cctype>
#include <cstring>
#include <fstream>

using namespace std;

int main(){

  fstream newfile;
  newfile.open("newfile.txt");
  newfile <<"Hello";
    
  int expenses_general{};


  cout << "Budget Tracker" << endl;
  cout << "This will give you a txt file with your costs in it" << endl;

  cout << endl;

  cout << "Enter your general expenses for this month" << endl;
  cin >> expenses_general;

  return 0;
}


What I have tried:

I added the code in the describe the problem part.
Posted
Updated 5-Sep-22 20:11pm
v2
Comments
Richard MacCutchan 5-Sep-22 15:28pm    
I think there is a lot of code that you still need to write. What you have here is nowhere near a bdget tracker. You need to create some classes to mange the data and a file to hold the details. One class would be Account, which contains a name, starting balance, and a list of transactions. you then need methods to read the account records into memory, update with any new transactions, save the updated details, print reports, etc.
slavek slavekk 5-Sep-22 15:33pm    
I'm not that advanced with c++, just trying to start with adding text and numbers into a .TXT file.
Richard MacCutchan 5-Sep-22 16:06pm    
You already know how to write "Hello" to the file:
newfile <<"Hello";

So it is similar to that. But you need also to write endl markers at the end of each line.
BernardIE5317 5-Sep-22 16:51pm    
May I say I do not agree w/ your approach to "helping" this chap/chapette . I believe in the principle of least action when "helping" another . I would have "helped" him/her re/ streaming to newfile by simply inquiring "Why are you not utilizing newfile?" or perhaps better "Why did you declare newfile?" rather than informing the poster the precise manner to do so . The receiver of "help" has to do _some_ work otherwise it is not true help .
Richard MacCutchan 6-Sep-22 3:20am    
Well you are free to post better answers, if you know them.

If you want to open a stream an append text to in then you can use : ios_base::openmode - C++ Reference[^]. This page discusses the issues : fstream::open - C++ Reference[^]
 
Share this answer
 
Read a tutorial on basic file I/O in C++. Try, for instance: 23.6 — Basic file I/O – Learn C++[^]
 
Share this answer
 
Comments
slavek slavekk 6-Sep-22 15:22pm    
Adding text to a file is not a problem but I wanna put numbers in it by using 'cin' to that txt file.
Code:

#include <iostream>
#include <fstream>

using namespace std;

int cost{};

int main(){
cout << "This is a budget tracker!" << endl;
ofstream infile;
infile.open("infile.txt");
infile << cost << endl;
infile.close();

int cost{};
cout << "Please give us your cost" << endl;
cin >> cost;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900