Click here to Skip to main content
16,017,265 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So before I get started, I will state this is part of my homework. I have to ask the user to input information that will get inserted into a text file in which it will be presented back to the user. The issue I'm having is that one of the variable outputs must be an addition of itself.

Example, lets say the problem is, creating a small money management account.
We need to insert the amount of money we had before purchase, after purchase, and total, as well as other forms of information. I'll post the snippet I've come up with so far.

I've inserted the code. My question is this, how can I have it add the second column (Cost) from every input we give it? Its not looping right now but ideally I want every time I insert a cost, at the end it will add all the
C++
COST
from all the
C++
accountCost 
inserted.

Any help is appreciated, even if you just give me a link elsewhere, it is appreciated.

PS. My professor wants the use of CSTRINGS not C++ Strings, hence why im using char aStringVar[SIZE] = ""; over a typical std::string aStringVar[SIZE] = "";

What I have tried:

C++
#include <iostream>
#include <fstream>
#include <cstdlib>

const int SIZE = 256;

int main(void){

ifstream readFile;
ofstream writeFile;
char accountName[SIZE] = "";
char cost[SIZE] = "";
char Total[SIZE] = "";
char Tax[SIZE] = "";
int accountCost = 0;
int accountTotal = 0;
int accountTax = 0;

/*
user inputs all the information into the variables with cout and cin functions.
Not inserting it since its pretty basic and I don't want to spend all my lunch break (work) writing this part of the code.
*/

//This part will simply save the input into the text file according to the format I specified. EX: Jimmy,120,450,2
writeFile.open("bank.txt", ios::app | ios::ate);
writeFile << accountName << "," << Cost << "," << Total << "," << Tax << "\n";
writeFile.close();

//Here we will output
readFile.open("bank.txt");
readFile.getline(accountName, SIZE, ',');
readFile.getline(Cost, SIZE, ',');
readFile.getline(Total, SIZE, ',');
readFile.getline(Tax, SIZE); //didn't use delimeter since its the last one.

accountCost = atoi(Cost);
accountTotal = atoi(Total);
accountTax = atoi(Tax);

cout << "Account Cost: " << accountCost << endl;
cout << "Account Total: " << accountTotal << endl;
cout << "Account Tax: " << accountTax << endl;


}
Posted
Updated 1-Jul-17 22:33pm
Comments
Richard MacCutchan 2-Jul-17 3:06am    
You have not done any calculations on the values entered by the user. You need to go back to pencil and paper and write out the logical steps of the problem. For example, what comes first (creating the basic structures file headers etc.), next you most likely need a loop that does a number of steps (read the account records, calculate the new totals, write the updated information). Finally you need to close all files and tidy up any report output etc.

1 solution

The usage is normally
C++
std::string aStringVar = "happy coding";//one string
std::string aStringVar1[SIZE] = "";// an array
aStringVar1[0] = "hello world";//access the first string

You can write in an open file til it is closed. Extending your code
C++
writeFile.open("bank.txt", ios::app | ios::ate);
writeFile << accountName << "," << Cost << "," << Total << "," << Tax << "\n";
writeFile << "Further writing in file";
writeFile.close();


Think carefully what and how you write in the file, because some code or human will read it. Testing is important!!!

Because you have some missing knowledge of the std::string read this tutorial.
 
Share this answer
 
Comments
JonathanAVazquez 2-Jul-17 5:43am    
The above comment is great for C++ Strings. However, i can only use C-style Strings which is comprised of an array of char, which will equate to a single string only.

I think that i figured out what i needed. So i decided to include another array that will collect the value of 1 of those variables. Each number the user inputs will place it into the array. At the end i will use a for loop, to loop through the array and each loop it will add itself, at the end it will be a total value. I include no code since im still working on that part. Once i figure it out i will include it here. Still in the testing stages.

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