Thread: frustrated... filling in inputs with input before it

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

    frustrated... filling in inputs with input before it

    this is filling in the input fields after it where i have the passwords at.

    Code:
    //This program written by Dan Kemper
    //This is a bank program
    
    //Includes Header Files
    #include <conio.h>
    #include <iostream>	
    #include <fstream>	
    #include <iomanip>
    #include <windows.h>
    #include <cctype>	
    
    //Required for cout
    using namespace std;	
    
    int main ()
    {
       //Declares variables
       char FirstName[20]; //first name
       char LastName[30]; //last name
       char Password; // password to get into registered users info
       char PW; //gets password from dat file to compare
       char User; //if person is registered or new
       char Print; //prints to textfile when told to
       float Balance; //balance when opening account
       float Deposits; //deposits
       float Withdrawls; //withdrawls
       float NewBalance; //balance after withdrawls and deposits are done. number thats saved into textfile for next time opened as 'balance'
       
       //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 << "\nThis is a bank program.\n";
       cout<<"\nPress [ENTER] to continue...\n";
       cin.get();
       system("cls");
       
       cout << "Are you a registered user (A), or would you like to make an account (B)? ";
       cin.get(User);
       User = toupper(User); //Makes uppercase automatically
       system("cls");
       if (User=='a' || User=='A')
       {
                     ifstream infile; //declares file pointer named infile
                     infile.open("passwords.dat",ios::in); //opens dat file which has password in it
                     infile.get(PW); //gets password from file
                     cout << "First Name: ";
                     cin >> FirstName;
                     cout << "\nLast Name: ";
                     cin >> LastName;
                     cout << "\nPassword: ";
                     cin.get(Password);
                     cout << PW; 
                     getch();
                     system("cls");
                     
                     if (Password==PW)
                     {
                                             cout << "Balance: " << Balance;
                                             cout << "\n\nDeposits: ";
                                             cin >> Deposits;
                                             cout << "\n\nWithdrawls: ";
                                             cin >> Withdrawls;
                                             cout << "\n\nNew Balance: " << NewBalance;
                                             cout << "\n\n" << FirstName << " " << LastName << ", is all the above information correct (Y/N)? ";
                                             cin >> Print;
                     }
                     if (Password!=PW)
                     {
                                             cout << "\n\nThat was the incorrect password!\a\a\a";
                                             getch();
                                             return 0;
                     }
                     infile.close();
       }
    
       
        if (User=='b' || User=='B')
        {
                          cout << "First Name: ";
                          cin >> FirstName;
                          cout << "\nLast Name: ";
                          cin >> LastName;
                          cout << "\nPick Your Password: ";
                          cin.get(Password);
                          cout << "\nDeposits: ";
                          cin >> Deposits;
                          cout << "\nBalance: ";
                          cout << setiosflags (ios::fixed) << setprecision(2) << Deposits ;
                          cout << "\n\n" << FirstName << " " << LastName << ", is all the above information correct (Y/N)? ";
                          cin >> Print;
    
        }
    
        if (Print=='y' || Print=='Y') 
        {
                                ofstream outfile ("Bank.txt");
                                outfile << "Name: " << FirstName << " " << LastName;
                                outfile << "\nPassword: *******";
                                outfile << "\n\nDeposits: " << Deposits;
                                outfile << "\nBalance: " << Deposits;
                                outfile << "\nThank you!";
                                outfile.close();
                                ofstream  pwfile ("Passwords.dat");
                                pwfile << Password;
                                pwfile.close();
                                cout << "\n\nThank You!\a";
        }      
        if (Print=='n' || Print=='N')
        {
                                cout << "Okay well then your going to have to restart, sorry.\a";
                                cout << "\n\nThank You!\a";
        }    
        //Calculation
        NewBalance = Balance + Deposits - Withdrawls;
       //Stops program until button is pressed
       getch();
       return 0;
    }

    also i would liek to know if theres a way i can use the persons name (which the enter) to name the outfiles so each new user would have there own files

    thanks for the help

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    frustrated... filling in inputs with input before it

    this is filling in the input fields after it where i have the passwords at.
    Incomprehensible.

  3. #3
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66
    what?

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    @ReLiEnThAwK: You realize your "question" makes absolutely no sense (as in being a readable English sentence)?

    Anyway...
    The problem is the newline character is left in the input stream after each cin >> call.
    You don't notice this between cin >> FirstName and cin >> LastName because >> skips whitespace. (discards leading whitespace)
    When you call cin.get(Password); the newline character is availible, consumed, and stored as the Password.
    A simple solution would be to call cin.ignore() after each cin >> to clear '\n' from the input stream.


    Yes you can use the name as the name of the password file.
    Just pass the name to infile.open instead of "passwords.dat"
    Code:
    infile.open(LastName,ios::in);
    Probably some string concatenation to make a better filename.
    like FirstName + '_' + LastName + '_' + "pass.dat" (not real syntax)
    Last edited by spydoor; 02-17-2006 at 03:15 PM.

  5. #5
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66
    i see, thanks for the help. but i use the cin.get and cin.ignores which you use when the variable is a character string. but you cant compare a character to a character string. maybe if i used typecasting to force the character string to a character? maybe im a little mixed up...
    as for the file name thing. that makes sense. before i was just doing
    Code:
    ofstream outfile (FirstName".txt")
    which obviosly didnt work. i dont know why i didnt think of that.
    thanks for the help.

    im a noob

  6. #6
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66
    please help me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  2. How do I validate these inputs?
    By Kyeong in forum C Programming
    Replies: 1
    Last Post: 10-19-2008, 02:20 PM
  3. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 06:16 AM
  4. Custom Made Safe Input Function
    By Beast() in forum C Programming
    Replies: 6
    Last Post: 08-21-2004, 10:19 PM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM