Thread: I am a no0b!!!

  1. #1
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66

    Cool 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;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > int PrintStatement;
    All your other use of this variable suggests that it should be a char variable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    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;
    }
    Last edited by major_small; 07-26-2005 at 05:46 AM. Reason: color-happy syntax highlighter
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66
    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

  5. #5
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66
    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!

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I see you're still trying to read chars into an int
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66
    ok i'll change that then

  8. #8
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66
    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?

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66
    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

  11. #11
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Use a while loop instead of do-while. And then ask your question again at the bottom of the loop body.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  12. #12
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66
    hmm i'm not sure i understand. can you or someone show me an axample or sumthin?

  13. #13
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  14. #14
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66
    ok thanks i'll go try it out

Popular pages Recent additions subscribe to a feed