two dimmensional arrays

Hello guys,
Im currently doing a project for school.
Its basically a Cash register that gains data from an input file.
In the input file, the first data is the beginning balance (which i got working).
After the beginning balance, it is followed by transaction number, then transaction amount. I can not figure out how to grab the data from the file (in the getData function). Any help would be appreciated.

EDIT: Just found out I do not need two dimensional arrays for the assignment, its just how I thought it would be done.

1
2
Look Below.
  
Last edited on
http://www.cplusplus.com/doc/tutorial/files/

A little less than halfway down this page you will see how to read data from a file. It uses the getline() function. This will read the file line-by-line and save it in a specified variable. If you have multiple bits of data on one line, you'll have to split it up using string member functions found in the documentation on this website.
@Tresky, thank you for your reply.
I understand how to read data from a file as Ive done it for the beginning balance, where I have an issue is when I have to read data from a file and input it into a 2dimmensional array. So the file looks like "1 1000.1 3 2819 1 2932". The transaction number is the first followed by the balance.
Just found out I do not need two dimensional arrays for the assignment, its just how I thought it would be done.
Is there a specific reason why you want this data inside of a two-dimensional array? It seems like overkill. :P

However, to split the line up into data points.... I don't want to type this all again cause I typed it all yesterday, so here is the link to the post. ;D

http://www.cplusplus.com/forum/general/118983/
My very last post at the bottom explains how to split a line up into data points.

Hope it helps.
Well I got the string to work, but I can not get the values to work and set in the beginning balance.
Here is the code.
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std; 

// Function Prototypes
	void DisplayTitle();
	double GetBegBal(ifstream&);
	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;

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

	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");

	while(! fin.eof())                 
    {
        input = input + " " + GetData(fin);
    }
	balance = GetBegBal(fin);
	cout << input;
	
	/*while(transCode != 0)
	{
		switch(transCode)
		{
			case 1: balance = ProcessCheck(balance, transAmt); break;
			case 2: balance = ProcessDeposit(balance, transAmt); break;
			case 3: balance = ProcessATM(balance, transAmt); break;
		}
		DisplayBal(balance);
		if(balance < 0)
			balance = ProcessSvcChg(balance);
		GetData(fin);
	}*/
	system("pause");
	return 0;
}	


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

	double GetBegBal(string start)
	{
		#include <string>
		double x;
		x = starthow
		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
You're searching for the space before you even have data in the string. You have to get the first line of data and then find the space.

-Get line of data.
-Find first space.
-Split the string from the beginning to the space and save separately.
-Remove the beginning to the space from the original string.

-Find first space.
-Split the string from the beginning to the space and save separately.
-Remove the beginning to the space from the original string.

Keep repeating this until you have finished the whole line. It's a repetitive process because you have 4 or 5 pieces of data on the same line so you have to do it 4 or 5 times to split all of the data up.

Does this make sense?
I understand the concept, but putting it into code is where I am completely lost.
Especially since I need to do this part in a function:

-Get line of data.
-Find first space.
-Split the string from the beginning to the space and save separately.
-Remove the beginning to the space from the original string.

and the rest in the main im guessing. I believe I should use a for loop but do not know where to set the end.
I also cannot convert values from the string to a double or integer, which is the main cause of this problem.

EDIT: in other words, I dont know how.
Last edited on
To convert from a string to an integer you use a command called atoi(). There isn't one for double, but there is one for float (atof) which you can then cast to a double. You'll have to include a new library for both of these. I believe it is cstdlib if I remember correctly.

1
2
3
4
5
6
#include <cstdlib>
...

string value = "32";

int integer = atoi(value);
I get the error.
"No suitable conversion function from "std::string" to "const char*" exists."

I have the following includes.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
#include <stdlib.h>
Post that little bit of code.

1
2
3
for (int i = 0; i < 100; i++){
		transCode = atoi(input.substr(0, space_position));
	}


even tried to declare everything like you did in your example, butsame error came up.
transCode = atoi(input.substr(0, space_position).c_str());

Forgot to tell you that you that atoi is a C function, so it accepts C-style strings. Just call that member function after substr().
yes, it now works, but my function does not grab a value, but instead stays 0.00

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

EDIT: this is how I call the function.
balance = static_cast<double>(GetBegBal(input));
Last edited on
1. Make sure there is data in the input file.
2. Make sure you are accessing the correct input file from your program.
3. Make sure that in the line of data you are reading that there is actually a space to be found. If there is no space, then space_position will be set to 0 and your substr() will grab everything from position 0 to position 0... which is nothing. Haha.
Topic archived. No new replies allowed.