2014-11-05 53 views
-4

我正在编程一个股票交易程序。 对于此功能,我需要更改特定帐户中的余额,但每当输出回文件时,它都会更新一个帐户的余额并删除其他人。C++文件编辑不删除。其他信息

源代码:

void Buying() 
{ 
    ifstream Companies; 
    ifstream Account; 
    Account.open("Account.txt"); 
    { 
     int ID; 
     double cash; 
     string sym; 
     ifstream Account; 
     Account.open("Account.txt"); 
     //Check for errord 
     if (Account.fail()) 
     { 
      cout << "Failed" << endl; 
      exit(1); 
     } 
     account S_accounts; 
     for (int i = 0; i < 5; i++) 
     { 
      Account >> S_accounts.id[i] >> S_accounts.cash[i]; 

     } 
     cout << "Please enter your last 4 digits of your ID: "; 
     do 
     { 
      cin >> ID; 
      cout << endl; 
      switch (ID) 
      { 
      case 1111: 
       cash = S_accounts.cash[0]; 
       break; 
      case 2222: 
       cash = S_accounts.cash[1]; 
       break; 
      case 3333: 
       cash = S_accounts.cash[2]; 
       break; 
      case 4444: 
       cash = S_accounts.cash[3]; 
       break; 
      case 5555: 
       cash = S_accounts.cash[4]; 
       break; 
      default: 
       cout << "ID number is invalid. please try again." << endl; 
      } 
      break; 
     } while (ID != 1111 || ID != 2222 || ID != 3333 || ID != 4444 || ID != 5555); 
     if (cash > 36.80) 
     { 
      cout << "You have $" << cash << " what stock would you like to purchase?" << endl; 
      listing(); 
      cout << endl; 
      cout << "Type the stock symbol of the company that you would like to purchase." << endl; 
      cin >> sym; 
      double price; 
      int number_of_stock; 
      double total; 
      int i = 0; 
      while (i < 7) 
      { 
       Stock S_companies; 
       Companies >> S_companies.price[i]; 
       i++; 
      } 
      //Stock S_companies; 
      if (sym == "AAPL" || sym== "aapl") 
      { 
       price = 450.00; 
      } 
      else if (sym == "BA" || sym == "ba") 
      { 
       price = 75.50; 
      } 
      else if (sym == "INTC" || sym == "intc") 
      { 
       price = 22.30; 
      } 
      else if (sym == "RMBS" || sym == "rmbs") 
      { 
       price = 5.55; 
      } 
      if (sym == "SIRI" || sym == "siri") 
      { 
       price = 3.15; 
      } 
      if (sym == "SWKS" || sym == "swks") 
      { 
       price = 25.35; 
      } 
      if (sym == "XLNX" || sym == "xlnx") 
      { 
       price = 36.80; 
      } 
      cout << "How many stock(s) would you like to buy?" << endl; 
      cin >> number_of_stock; 
      total = number_of_stock*price; 
      if (cash > total) 
      { 
       account S_account; 
       ofstream Oaccount; 
       Oaccount.open("Account.txt"); 
       if (Oaccount.fail()) 
       { 
        "Failed to open the file."; 
       } 

我知道这是我需要修改,但我在做什么完全一无所知,因此仅改变了,在使用他们的ID登录的人的账户余额。

   if (ID == 1111) 
       { 
         Oaccount << S_accounts.id[0] << '\t' << '\t' << S_accounts.cash[0] - total; 
       } 
       Oaccount.close(); 
      } 
      else 
       cout << "You do not have enough money."; 
     } 
     else 
     { 
      cout << "Sorry, you do not have enough money in your account."; 
      exit(1); 
     } 
     Account.close(); 
    } 
} 

回答

0

我不是调试code.But这是你如何更新/修改一个条目... 让数与要查找客户的价值......可以说你要改变他名称更改为新名...

while(!file.eof()) 
{ 
    if(obj.number==given_value) 
    { 
     file.tellg(pos); 
     { 
      //modify values here like.. 
      strcpy(obj.name,newname); 
      file.seekg(pos); 
      file.write((char*)&obj,sizeof(obj)); 
     } 
    } 
} 
+0

这不回答问题。 – cybermonkey 2014-11-05 12:11:53