简体   繁体   中英

How to ignore .txt for cin for output file in C++?

This is my C++ code and I wish to create an output file. In order to create the output file, I request the user to include.txt at this input, cin >> accnum; , hence, when output file is produced, it will show accnum.txt as plain text. My question is, what should I do in order to ignore.txt to be appeared in the text file, provided that I must input.txt(this is to produce txt file, without.txt at the input, no output file will be produced). I have put a comment line, kindly refer to that line. Thanks for your help!

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    using namespace std;
    
    int main ()
    {
        double withdraw,deposit,number,transfer,num,amt;
        string name,first_name,middle_name,last_name;
        int x=0;
        int option;
        int A = 300;
        int B = 500;    
        int C,W,y;
        string accnum;
        ifstream infile;
        ofstream outfile;
        
        do {
            cout <<"                          Welcome to ATM Machine                         "<<endl;
            cout <<"\n\t****Menu****";
            cout <<"\n\t*                 1.Creating a new account                     *";
            cout <<"\n\t*                 2.Cash Deposit                               *";
            cout <<"\n\t*                 3.Cash withdrawal                            *";
            cout <<"\n\t*                 4.Fund transfer between two account          *";
            cout <<"\n\t*                 5.Exit                                       *";
            cout <<"\n\t*                                                              *";
            cout <<"\n\t********";
            cout <<"\n\t                  Please choose an option: ";
            cin>>option;
            
            switch(option){
                case 1:
                    cout <<"Press 1 to Confirm" << endl << "Press 0 to exit " << endl ;
                    cin >> C ;
                    {
                        if( C == 1 )
                        {
                        cout << "Enter first name: " << endl;
                        cin >> first_name;
                        cout << "Enter middle name: " << endl;
                        cin >> middle_name;
                        cout << "Enter last name: " << endl;
                        cin >> last_name;
                        
                        name = first_name + " " + middle_name + " " + last_name;
                        cout << "enter a 4 digit number : " << endl << "gentle reminder : please add .txt at the end of your number" << endl << "e.g 1234.txt" << endl; 
                        cin  >> accnum ; //how to ignore .txt
                        cout << "What is your primary balance: " << endl;
                        cin >> amt;
                        infile.open(name);
                        outfile.open(name);
                        cout << "your account has been created " << endl ;
                        outfile << "name" << name << endl;
                        outfile << "account no. : " << accnum << endl ; 
                        outfile << "current balance : RM" << amt << endl ;
                        }else {
                        cout << "Bye, have a nice day";
                        }
                    infile.close();
                    outfile.close();
                    }
                    break;
                    
                case 2:
                    cout<<"Deposit amount: "<<endl;
                    cin>>deposit;
                    if(deposit<10) {                  
                        cout<<"The smallest note acceptable is RM10."<<endl;
                    }else {
                        x=static_cast<int>(deposit)%10;
                        if(x!=0)
                        {                   
                        cout<<"Invalid deposit amount"<<endl;
                        cout<<"Example of deposit amount: RM10, RM20, RM50, RM320..."<<endl;
                        }else {
                        A+=deposit;
                        cout<<"current balance: RM"<< A <<endl;
                        }
                    }                     
                    break;
                    
                case 3:
                    cout<<"Amount you want to withdraw"<<endl;
                    cin>>withdraw;
                    if(A < withdraw || withdraw < 10) {    // A is balance 
                    cout <<"Failed to withdraw money! Please try again."<<endl;
                    }else {
                    A-=withdraw;
                    cout <<"current balance: "<< A <<endl;
                    }
                    break;
                    
                case 4:
                    infile.open("1212.txt");
                    
                    infile >> x;
                    cout<<"Enter Details of transfering Account(Account A)"<<endl; 
                    cout<< "Your account number is:"<<endl; 
                    cin>>W;
                    cout<< "What is your name:"<<endl;
                    cin.ignore();
                    getline(cin,name);
                    cout<<"Enter Details of receiving Account(Account B)"<<endl;
                    cout<<"Enter account number:"<<endl;
                    cin>>W;
                    cout<<"Enter name:"<<endl;
                    cin.ignore();
                    getline(cin, name);
                    cout <<"your account balance is : " << x << endl << "how much do you want to transfer ? ";
                    cin >> num ;
             
                    outfile.open("1212.txt");
                    outfile << x - num ;
                    cout << "your account balance is : " << x - num ;
            
            
                   infile.close();
                   outfile.close();
        
                   infile.open("2121.txt");
        
                   infile >> y ;
                   outfile.open("2121.txt") ;
                   outfile << y + num ;
            
                   infile.close();
                   outfile.close();
                        break;
                
                case 5:
                    cout<<"Thank you for using ATM machine. Bye, have a nice day !"<<endl;
                    return 0;
                
                default:
                    if(option != 5) cout<<"Invalid option.Please try again!"<<endl;
                    break;
            }
        }while(option != 5);
         
        system("pause"); 
        return 0;
    }

You asked about how to remove the .txt part from accnum and here's one way to do that:

cin  >> accnum ; //how to ignore .txt

// check that the length/size of the entered accnum is at least 4 characters
// and
// that the last 4 characters is ".txt"

if(accnum.size() >= 4 && accnum.substr(accnum.size() - 4) == ".txt") {
    // extract a substring from accnum without the last 4 characters
    accnum = accnum.substr(0, accnum.size() - 4);
}

However, I have my doubts about the requirement that the user should actually add .txt to the account number, because seeing this at an ATM would be really confusing:

enter a 4 digit number :
gentle reminder : please add .txt at the end of your number"
e.g 1234.txt

It's especially confusing since the only thing you want to do with .txt is to remove it. I would consider changing this user interface.

Suggestions:

  • Let the user add the account number without .txt
  • Instead of saving the account information in a file with the account holders name as the filename, save it in a file named accnum + ".txt" . This makes it possible for a person to have more than one account - just as in real life.

Just create the file without a format, the name will be the file_name content exactly, be aware that in windows has reserved names such as CON .

#include <iostream>
#include <fstream>
#include <string>


int main (void) {
    std::string  file_name = "1234";
    std::fstream f_handle;

    f_handle.open(file_name, std::fstream::out);
    if (f_handle.is_open()){
        f_handle << "Some text" << std::endl;
        f_handle.close();
    }

    f_handle.open(file_name, std::fstream::in);
    if (f_handle.is_open()){
        std::cout << f_handle.rdbuf() << std::endl;
        f_handle.close();
    }

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM