-
I am a no0b!!!
I just started learning loops and stuff and I am in need of help! I have this ATM program that is all messed up and I have no idea how to fix it. The program asks for first name, last name, account number, statement balance, outstanding deposits and outstanding withdrawls. Then it calculates and shows the result. Then it asks if they would rather print out the screen, or print to a textfile (print to textfile is default for this). If it helps, I use dev C++. Anyone please look at my code and help me out! Thanks a whole bunch.
Code:
//This program written by Dan Kemper
//This program will balance your checkbook and print the results to a textfile named "checkbook.txt" or print the scrren to your printer
//Suggestions and other comments can be sent to my email: [email protected]
# include <iostream.h>
# include <fstream.h>
# include <iomanip.h>
# include <conio.h>
int main ()
{
//Declares variables
char FirstName[20];
char LastName[30];
char Account[9];
int PrintStatement;
float Balance;
float Deposits;
float Withdrawls;
float NewBalance;
//Makes Textfile
ofstream outfile ("checkbook.txt");
//Outputs to screen
cout << " AAA TTTTTTTTTTTTTTT MMMMM MMMMM\n";
cout << " AAAAA TTTTTTTTTTTTTTT MMMMMM MMMMMM\n";
cout << " AAA AAA TTTTTT MMM MMM MMM MMM\n";
cout << " AAA AAA TTTTTT MMM MMM MMM MMM\n";
cout << " AAAAAAAAAAA TTTTTT MMM MMM MMM MMM\n";
cout << " AAAAAAAAAAAAA TTTTTT MMM MMM MMM MMM\n";
cout << " AAA AAA TTTTTT MMM MMM MMM MMM\n";
cout << "AAA AAA TTTTTT MMM MMMMM MMM\n";
system("PAUSE");
cout << "\n\nThis program balances your checkbook.\n\n";
cout << "First Name: ";
//Asks user for input
cin.get(FirstName, 20);
cin.ignore(80, '\n');
cout << "Last Name: ";
cin.get(LastName, 30);
cin.ignore(80, '\n');
cout << "Account number: ";
cin.get(Account, 9);
cin.ignore(80, '\n');
cout << "\n\nStatement balance: ";
cin >> Balance;
cout << "\nOutstanding deposits: ";
cin >> Deposits;
cout << "\nOutstanding withdrawls: ";
cin >> Withdrawls;
//Conversions
NewBalance = Balance + Deposits - Withdrawls;
//Output Results
cout << "\nNew balance: ";
cout << setiosflags (ios::fixed) << setprecision(2) << NewBalance;
cout << "\n\nPrint? (Y/N)\n";
cin >> PrintStatement;
do
{
while (PrintStatement=='y' || PrintStatement=='Y')
{
system("PRINT");
cout << "Thank You!";
getch ();
return 0;
}
else
{
cout << "Thank You! You may see your statement in a textfile named 'checkbook.txt'.";
}
}
//Prints to Textfile
outfile << "Name: " << FirstName << " " << LastName << "\n";
outfile << "Account Number: " << Account << "\n";
outfile << "Statement Balance: " << Balance << "\n";
outfile << "Outstanding Deposits: " << Deposits << "\n";
outfile << "Outstanding Withdrawls: " << Withdrawls << "\n";
outfile << "New Balance: " << setiosflags (ios::fixed) << setprecision(2) << NewBalance;
//Stops program until button is pressed
getch();
return 0;
}
-
> int PrintStatement;
All your other use of this variable suggests that it should be a char variable.
-
here you go. see the comments for why it was wrong... take note: the way I did it cuts out the printing that you wanted. I suspect that non-standard coding was your biggest problem...
Code:
//This program written by Dan Kemper
//This program will balance your checkbook and print the results to a textfile named "checkbook.txt" or print the scrren to your printer
//Suggestions and other comments can be sent to my email: [email protected]
#include <iostream> //don't use the .h headers
#include <fstream> //and there's no space between
#include <iomanip> //the '#' and include
using namespace std; //required with the new headers
int main ()
{
//Declares variables
char FirstName[20];
char LastName[30];
char Account[9];
int PrintStatement;
float Balance;
float Deposits;
float Withdrawls;
float NewBalance;
//Makes Textfile
//if you want to MAKE the text file, you should explicitly say so with
//the 'ios::trunc' argument. that clears out whatever was there before
//and starts fresh. I changed it to test.out for my own purposes.
ofstream outfile ("test.out",ios::trunc);
//Outputs to screen
cout << " AAA TTTTTTTTTTTTTTT MMMMM MMMMM\n";
cout << " AAAAA TTTTTTTTTTTTTTT MMMMMM MMMMMM\n";
cout << " AAA AAA TTTTTT MMM MMM MMM MMM\n";
cout << " AAA AAA TTTTTT MMM MMM MMM MMM\n";
cout << " AAAAAAAAAAA TTTTTT MMM MMM MMM MMM\n";
cout << " AAAAAAAAAAAAA TTTTTT MMM MMM MMM MMM\n";
cout << " AAA AAA TTTTTT MMM MMM MMM MMM\n";
cout << "AAA AAA TTTTTT MMM MMMMM MMM\n";
//system("PAUSE"); //don't use this - nonstandard and possibly dangerous
//the following is a better alternative:
cout<<"press [ENTER] to continue...\n";
cin.get();
cout << "\n\nThis program balances your checkbook.\n\n";
cout << "First Name: ";
//Asks user for input
cin.get(FirstName, 20);
cin.ignore(80, '\n');
cout << "Last Name: ";
cin.get(LastName, 30);
cin.ignore(80, '\n');
cout << "Account number: ";
cin.get(Account, 9);
cin.ignore(80, '\n');
cout << "\n\nStatement balance: ";
cin >> Balance;
cin.ignore(1); //clean up after cin
cout << "\nOutstanding deposits: ";
cin >> Deposits;
cin.ignore(1); //see previous
cout << "\nOutstanding withdrawls: ";
cin >> Withdrawls;
cin.ignore(1); //again
//Conversions
NewBalance = Balance + Deposits - Withdrawls;
//Output Results
cout << "\nNew balance: ";
cout << setiosflags (ios::fixed) << setprecision(2) << NewBalance;
cout << "\n\nPrint? (Y/N)\n";
cin >> PrintStatement;
cin.ignore(1); //see above statements
/* this entire section is non-standard and doesn't make sense. see comments
*
* FYI: there's no way to print using standard C++ - but if you're in windows,
* you may want to ask help with that - it's a more advanced topic.
*
do
{
while (PrintStatement=='y' || PrintStatement=='Y')
{
system("PRINT"); //non-standard and possibly dangerous
cout << "Thank You!";
//getch (); //non-standard function
return 0;
}
else //there is no if for this else - what were you trying to do?
{
cout << "Thank You! You may see your statement in a textfile named 'checkbook.txt'.";
}
} //is this a do-while loop? if so, where's the while?
*/
//Prints to Textfile
outfile << "Name: " << FirstName << " " << LastName << "\n";
outfile << "Account Number: " << Account << "\n";
outfile << "Statement Balance: " << Balance << "\n";
outfile << "Outstanding Deposits: " << Deposits << "\n";
outfile << "Outstanding Withdrawls: " << Withdrawls << "\n";
outfile << "New Balance: " << setiosflags (ios::fixed) << setprecision(2) << NewBalance;
outfile.close(); //close your files.
//Stops program until button is pressed
//getch(); //again, don't use this
cin.get(); //this is much better
return 0;
}
-
thanks, i thought there was a way to print the screen... oh well. thanks for the help, i'll try it out now on my computer upstairs (it has dev c++)
i'm sure i'll have more questions so I'll post em in this thread to try and keep the forums clean :D
-
ok now I changed it a little, which means there are more problems...
Code:
//This program written by Dan Kemper
//This program will balance your checkbook and print the results to a textfile named "checkbook.txt" or print the scrren to your printer
//Suggestions and other comments can be sent to my email: [email protected]
//Includes Header Files
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main ()
{
//Declares variables
char FirstName[20];
char LastName[30];
char Account[9];
int PrintStatement;
float Balance;
float Deposits;
float Withdrawls;
float NewBalance;
//Makes Textfile
ofstream outfile ("checkbook.txt",ios::trunc);
//Outputs to screen
cout << " AAA TTTTTTTTTTTTTTT MMMMM MMMMM\n";
cout << " AAAAA TTTTTTTTTTTTTTT MMMMMM MMMMMM\n";
cout << " AAA AAA TTTTTT MMM MMM MMM MMM\n";
cout << " AAA AAA TTTTTT MMM MMM MMM MMM\n";
cout << " AAAAAAAAAAA TTTTTT MMM MMM MMM MMM\n";
cout << " AAAAAAAAAAAAA TTTTTT MMM MMM MMM MMM\n";
cout << " AAA AAA TTTTTT MMM MMM MMM MMM\n";
cout << "AAA AAA TTTTTT MMM MMMMM MMM\n";
cout<<"\nPress [ENTER] to continue...\n";
cin.get();
cout << "\n\nThis program balances your checkbook.\n\n";
cout << "First Name: ";
//Asks user for input
cin.get(FirstName, 20);
cin.ignore(80, '\n');
cout << "Last Name: ";
cin.get(LastName, 30);
cin.ignore(80, '\n');
cout << "Account number: ";
cin.get(Account, 9);
cin.ignore(80, '\n');
cout << "\n\nStatement balance: ";
cin >> Balance;
cin.ignore(1);
cout << "\nOutstanding deposits: ";
cin >> Deposits;
cin.ignore(1);
cout << "\nOutstanding withdrawls: ";
cin >> Withdrawls;
cin.ignore(1);
//Conversions
NewBalance = Balance + Deposits - Withdrawls;
//Output Results
cout << "\nNew balance: ";
cout << setiosflags (ios::fixed) << setprecision(2) << NewBalance;
cout << "\n\nDo you want the above information to be printed to a textfile? (Y/N)\n";
cin >> PrintStatement;
cin.ignore(1);
do
{
//Prints to Textfile
outfile << "Name: " << FirstName << " " << LastName << "\n";
outfile << "Account Number: " << Account << "\n";
outfile << "Statement Balance: " << Balance << "\n";
outfile << "Outstanding Deposits: " << Deposits << "\n";
outfile << "Outstanding Withdrawls: " << Withdrawls << "\n";
outfile << "New Balance: " << setiosflags (ios::fixed) << setprecision(2) << NewBalance;
}
while (PrintStatement=='y' || PrintStatement=='Y');
cout << "Thank You!";
outfile.close();
//Stops program until button is pressed
cin.get();
return 0;
}
Thanks for the help!
-
I see you're still trying to read chars into an int :rolleyes:
-
-
i changed it to char and it messed the program up. it wont even run so i had to change it back to do it. why wouldnt it work?
-
Let me guess: infinite loop?
There is no exit condition for your loop once it has started. If the condition in the do-while loop is true, then that same block of code executes until PrintStatement changes... but that block of code never changes PrintStatement, so the condition always remains true.
You might want to move the three lines preceding the do-while loop into the loop body itself (after all, that block is guaranteed to execute at least once), and then, PrintStatement is modified in the loop body.
-
how do u make it not automaticly execute at least once? I just want the information to be printed to textfile only wen the input is "y" or "Y" wen asked if they want the info printed to textfile
-
Use a while loop instead of do-while. And then ask your question again at the bottom of the loop body.
-
hmm i'm not sure i understand. can you or someone show me an axample or sumthin?
-
Right now, you have this:
Code:
cout << "\n\nDo you want the above information to be printed to a textfile? (Y/N)\n";
cin >> PrintStatement;
cin.ignore(1);
do
{
//Prints to Textfile
outfile << "Name: " << FirstName << " " << LastName << "\n";
outfile << "Account Number: " << Account << "\n";
outfile << "Statement Balance: " << Balance << "\n";
outfile << "Outstanding Deposits: " << Deposits << "\n";
outfile << "Outstanding Withdrawls: " << Withdrawls << "\n";
outfile << "New Balance: " << setiosflags (ios::fixed) << setprecision(2) << NewBalance;
}
while (PrintStatement=='y' || PrintStatement=='Y');
What it seems like you want is this:
Code:
cout << "\n\nDo you want the above information to be printed to a textfile? (Y/N)\n";
cin >> PrintStatement;
cin.ignore(1);
while(PrintStatement == 'y' || PrintStatement == 'Y')
{
//Prints to Textfile
outfile << "Name: " << FirstName << " " << LastName << "\n";
outfile << "Account Number: " << Account << "\n";
outfile << "Statement Balance: " << Balance << "\n";
outfile << "Outstanding Deposits: " << Deposits << "\n";
outfile << "Outstanding Withdrawls: " << Withdrawls << "\n";
outfile << "New Balance: " << setiosflags (ios::fixed)
<< setprecision(2) << NewBalance;
// Ask the user again if they want to print. Otherwise,
// PrintStatement is not modified and you end up in an
//infinite loop (assuming the condition evaluated to true
// in the first place).
cout << "\n\nDo you want the above information to be printed to a textfile? (Y/N)\n";
cin >> PrintStatement;
cin.ignore(1);
}
Now, it will ask the user if they want to print.
If they do, it enters the loop, prints, and asks them if they'd like to do it again (if they say yes, it repeats, if they say no, it bails out of the loop).
If they say no, then it never enters the loop, and never prints.
Cheers
-
ok thanks i'll go try it out