problem with seperating string.

Hello guys,
I have to create a cash register problem for class.
It takes values out of an input file, listed below, and has to calculate them.
The problem I have is taking the first value out of the string after I getBegBal.

and then after, seperating the numbers from transaction type to transaction value.

File: 2000 1 1225.72 1 463.81 3 200 1 632 2 1500 1 300 2 1800

The $2000 is the beginning balance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
  #include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
#include <stdlib.h>
using namespace std; 

// Function Prototypes
	void DisplayTitle();
	float GetBegBal(string);
	void DisplayBal(double);
	string GetData(ifstream&);
	double ProcessCheck(double, double);
	double ProcessDeposit(double, double);
	double ProcessATM(double, double);
	double ProcessSvcChg(double);


//Global Constants
const double	CHARGE = 10,
				ATMFEE =  2;

struct totals{
	double  Credits,
			Debits,
			Service;
};

int main()
{
	//Variable Declarations
	int transCode;
	double balance,
		   transAmt;
	ifstream fin;
	string input;
	int space_position = input.find(' ');
	int transCtr;

	//Declared my structure
	totals print;

	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);
	
	//Displays the title.
	DisplayTitle();

	//Opens the File.
	fin.open("C:\\Users\\Kodi\\Desktop\\checkIn.dat");

	//Creates a message if the file failed to open.
	if (fin.fail( ))
    {
        cout << "Input file opening failed.\n";
		system("pause");
        exit(1);
    }

	//While it is not the end of file.
	while(! fin.eof())                 
    {
		//Inputs the numbers into a string.
        input = input + " " + GetData(fin);
    }

	//Closes the file.
	fin.close();
	
	balance = static_cast<double>(GetBegBal(input));
	cout << balance;
	
	cout << input;
	system("pause");
	/*for (int i = 0; i < 100; i++){
		transCode = atoi(input.substr(0, space_position).c_str());
	}*/
	transCode = 0;
	while(transCode != 0)
	{
		switch(transCode)
		{
			case 1: 
					balance = ProcessCheck(balance, transAmt);
					print.Debits = print.Debits + transAmt;
					break;
			case 2: 
					balance = ProcessDeposit(balance, transAmt);
					print.Credits = print.Credits + transAmt;
					break;
			case 3: 
					balance = ProcessATM(balance, transAmt); 
					print.Service = print.Service + ATMFEE;
					print.Debits = print.Credits + transAmt;
					break;
		}
		DisplayBal(balance);
		if(balance < 0){
			balance = ProcessSvcChg(balance);
			print.Service = print.Service + CHARGE;
		}
	}
	system("pause");
	return 0;
}	


	void DisplayTitle()
	{
		cout << "\n       Check Register\n\n";
	}

	float GetBegBal(string start)
	{
		float x;
		int space_position = start.find('_');
		x = atof(start.substr(0, space_position).c_str());
		return x;
	}

	void DisplayBal(double x)
	{
		cout << "\t\tBalance = $" << setw(10) << x;
	}

	string GetData(ifstream& in)
	{
		string value;
		in >> value;
		return value;
	}

	double ProcessCheck(double bal, double amt)
	{
		cout << "\n  Check =    " << setw(10) << amt;
		return (bal - amt);
	}

	double ProcessDeposit(double bal, double amt)
	{
		cout << "\n  Deposit =  " << setw(10) << amt;
		return (bal + amt);
	}
	double ProcessATM(double bal, double amt)
	{
		cout << "\n  ATM     =  " << setw(10) << amt;
		bal = bal - amt;
		DisplayBal(bal);
		bal = bal - ATMFEE;
		cout << "\n  ATM Fee =  " << setw(10) << ATMFEE;
		return (bal);
	}
	double ProcessSvcChg(double bal)
	{
		cout << "\n  Service chg =" << setw(8) << CHARGE;
		bal = bal - CHARGE;
		DisplayBal(bal);
		return (bal);
	}
Last edited on
File: 2000 1 1225.72 1 463.81 3 200 1 632 2 1500 1 300 2 1800

Am I right in saying that the first number is the transaction number and the second is the transaction type? If so, it's pretty easy to just separate them out when parsing. You can leverage the ability of filestream extraction to parse values and skip whitespaces.

lets say I want to return a pair of values parsed from input. I can write:
1
2
3
4
5
6
7
std::pair<double, int> ParseTransaction(ifstream& in)
{
    std::pair<double, int>transaction;
    in >> transaction.first;
    in >> transaction.second;
    return transaction;
}

You don't have to use a pair, you could use two different functions and alternate between them. But the point is extracting from input will automatically parse the value to the desired type, and it will also skip any whitespaces.

*disclaimer* I didn't check the syntax when I wrote the example, so forgive me if it doesn't compile.
Last edited on
Topic archived. No new replies allowed.