Multiple string parses to single line

Okay I am working on a bank account program.

I am having trouble with the function below, I am trying to get text and variables to go to a file for the program to read from on the main screen.

An example of what I am trying to achieve for the output would be "DEPOSIT OF $500 ON 06/14/2011" //Where the date is already determined elsewhere and the amount is entered by the user as shown below.

The output I am ACTUALLY getting from this code is "16" regardless of what information is entered from the user.

EDIT: Also, the variables are defined as follows:

float cDep;
string trans1;
string currentDate;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void cDeposit()
{
	system("cls");
	
	cout << "Enter amount deposited:";
	cout << "> ";
	cin >> cDep;

	trans5 = trans4;
	trans4 = trans3;
	trans3 = trans2;
	trans2 = trans1;

	trans1= ( "DEPOSIT OF  $", cDep , "ON " , currentDate);

	ofstream t1file ("trans1.txt");
	if (t1file.is_open() )
	{
		t1file << (trans1, ios::trunc);

		t1file.close();
	}
	
}


Any ideas on what is going on here and what I should be doing?
Last edited on
No, that doesn't look right and shouldn't compile. What is 'trans1'?

Line 16 should be: ofstream t1file ("trans1.txt", ios_base::out | ios_base::trunc); http://www.cplusplus.com/reference/iostream/ofstream/

While line 19: t1file << trans1 (if 'trans1' can be streamed)

The output I am ACTUALLY getting from this code is "16" regardless of what information is entered from the user.
Are you sure that it's not 42?

So what is 'trans1', 'cDep', and 'currentDate'?
Surprisingly, it does compile which is what was making it so hard for me to determine the issue as I'm new to working with outputting to files.

The output I am getting is either 16, or sometimes 16888. May be from other parts of my code?

cDep is the variable used for the amount that the user enters to be deposited into the account they chose and is defined as 'float'

currentDate is what the current computer date is determined in the main() function and is output as a string:

1
2
char date[9];
	_strdate(date);


trans1 - trans5 are the variables used to keep track of the last 5 transactions that are made to the bank account, and when the user enters a new transaction, the string for trans4 goes to trans5, trans3 to trans4 etc and then the user enters the new trans1 name. Then those will all be output to an external file to be read from.

I made the changes you suggested and now it just erases the contents of the file but does not put the new information in, any ideas?

Also, I think there is a problem at line 14, I don't know if strings can be entered that way or no? But I could not see another way of doing that.

EDIT:
I changed trans1 to being just plain text as follows, and it output correctly:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void cDeposit()
{
	system("cls");
	
	cout << "Enter amount deposited:";
	cout << "> ";
	cin >> cDep;

	trans5 = trans4;
	trans4 = trans3;
	trans3 = trans2;
	trans2 = trans1;

	trans1= ("DEPOSIT OF  $");

	ofstream t1file ("trans1.txt", ios_base::out | ios_base::trunc);
	if (t1file.is_open() )
	{
		t1file << (trans1);

		t1file.close();
	}
	
}


So any ideas on how to include the amount that was entered by the user and the date in the string?
Last edited on
The comma expression (on line 14/19 of you original post) is somewhat strange. Each of that comma separated expression is evaluated and the last or first (may depend on the compiler) is the result. So trans1 might be currentDate.

On line 19 it probably results the value of ios::trunc.

So you don't know 42? http://en.wikipedia.org/wiki/Phrases_from_The_Hitchhiker%27s_Guide_to_the_Galaxy ;)

You can use stringstream http://www.cplusplus.com/reference/iostream/stringstream/ to build trans1.
That'd look like this:
1
2
3
4
5
6
#include <sstream>
...
std::stringstream ss;

ss << "DEPOSIT OF  $" << cDep << "ON " << currentDate;
trans1=ss.str();


With the stream format flags http://www.cplusplus.com/reference/iostream/ios_base/fmtflags/ you can determine how the floating point values will look like.

You may consider to use strftime() instead of the non standard _strdate(). strftime provides more options to build the date/time
Topic archived. No new replies allowed.