2014-01-15 231 views
-1

嗨我正在一个简单的工资申请。以下代码包含使用switch语句的两个选项菜单。该代码还链接到名为“shop-account”的文本文件。该文件仅包含值100.使用功能读取/写入文件

对于选项1,用户应该能够从文件中转移金额。用户应该能够进行尽可能多的转帐,而不用透支帐户。代码应该能够输出当前余额。我相信我假设要使用void函数,但我以前从未使用它,并且真的很挣扎。我希望有人可以看看代码,并告诉我哪里出错了。谢谢

int read_balance (void); 
void write_balance (int balance); 

using namespace std; 
int _tmain(int argc, _TCHAR* argv[]) 
{ 

    int selection; 
    int total; 
    int balance; 
    int NewAmount; 

    do { 
      cout << "1. Transfer an amount" <<endl; 
      cout << "2. Quit" << endl; 
      cout << "Please enter menu number"<<endl; 
      cin >> selection; 

      switch(selection) 
      { 
       case 1: 
        cout << "You have choosen to transfer an amount" << endl; 
        cout << "How much do you wish to transfer from the shop account?"<<endl; 
        cin >> NewAmount; 
        balance -= NewAmount; 
        write_balance (balance); 
        cout << balance << endl; 
        break; 

       case 2: 
       return 0; 
       break; 

       default: 
       cout << "Ooops, invalid selection!" << endl; 
       break; 
       } 

     }while(selection != 2); 

    system("pause"); 
    return 0; 
} 


int read_balance (void) 
{ 
    fstream f; 
    f.open("shop-account.txt"); 
    f >> balance; //error: "balance" is unidentified 
    f.close(); 
    return balance; 
} 


void write_balance (int balance) 
{ 
    fstream f; 
    f.open("shop-account.txt"); 
    f << balance; 
    f.close(); 
} 
+0

我希望你能看看代码,并告诉我们你认为你出错的地方。由于 –

+0

也是唯一一块代码,我正在寻找帮助是如何从文件中实际转移一笔金额,一旦我找到了这一点,我会继续尝试,并自己做其余的 – user3057816

+2

缺少缩进和它不是一个事实可编辑的例子帐户为“你要去哪里错”? - 在你的'read_balance'里,'balance'在哪里?没有地方。将'int balance;'添加到函数中。 – ShinTakezou

回答

1

你的功能没有平衡变量声明!你必须在你的函数中添加一个平衡的声明

int read_balance(void) 
{ 
    fstream f; 
    f.open("shop-account.txt"); 

    int balance; 
    f >> balance; //now it's defined 
    f.close(); 
    return balance; 
} 
+0

这只能解答一半的问题。假设OP想要一个程序...... _makes sense_ – sehe

+1

一些流错误检查会很好 – zoska

+4

@zoska:你不觉得这太多了吗? –

3
  1. 与其他人一样提到的,你没有申报在正确的范围内int balance(函数级别范围,在这种情况下)。

    事实上,它看起来像你忘记打电话read_balance产品总数,让您的balance计算中使用的不确定的值,这是Undefined Behaviour

  2. 接下来:在你使用变量的地方声明你的变量,这样当你决定将代码片段提取到子函数中时,可以防止这种情况发生,并且可以更容易地看到变量的使用位置。因此,更容易看到代码是否正确。

  3. Next:错误处理。如果你没有,你的程序是毫无价值的。事实上,固定上述问题,即使,

    • 只需输入无效输入一次会运行一个循环,并只保留从其减去从账户余额不定值和写作这些错误的价值观到磁盘。这可能不是你想要的。

    • 使得shop-account.txt只读足以节目愚弄到

      • 传输无限量的,而无需更新具有文件中平衡的错误观念文件
      • (因为它从来没有检查)

这里是一个清理的版本,进行最低限度的检查,并添加一个选项来“检查帐户余额”。似乎很有用。

看到它Live On Coliru

我希望一些这有助于。首先,我希望你的老师会提到大部分内容:/

int read_balance(void); 
void write_balance(int balance); 

#include <iostream> 
#include <limits> 

int main() 
{ 
    while(std::cin.good()) 
    { 
     std::cout << "0. Request balance"  << std::endl; 
     std::cout << "1. Transfer an amount" << std::endl; 
     std::cout << "2. Quit"     << std::endl; 
     std::cout << "Please enter menu number" << std::endl; 

     int selection = 2; 
     if(std::cin >> selection) 
     { 
      std::cout << "DEBUG: selection:" << selection << "\n"; 
      switch(selection) 
      { 
      case 0: 
       std::cout << "The current account balance is " << read_balance() << std::endl; 
       break; 

      case 1: 
       { 
        std::cout << "You have choosen to transfer an amount" << std::endl; 
        std::cout << "How much do you wish to transfer from the shop account?"<<std::endl; 

        int amount = 0; 
        if (std::cin >> amount) 
        { 
         std::cout << "DEBUG: amount:" << amount << "\n"; 
         int balance = read_balance(); 
         if(amount<=0) 
         { 
          std::cout << "Amount must be positive\n"; 
         } 
         else if(balance < amount) 
         { 
          std::cout << "Insufficient funds\n"; 
         } 
         else 
         { 
          int new_balance = balance - amount; 
          write_balance(new_balance); 
          std::cout << "New account balance: " << new_balance << std::endl; 
         } 
        } else 
        { 
         // bad input cleared outside the switch 
        } 
       } 
       break; 

      case 2: 
       return 0; 
       break; 

      default: 
       std::cout << "Ooops, invalid selection!" << std::endl; 
       break; 
      } 
     } 

     if(std::cin.eof()) 
     { 
      std::cout << "Bye\n"; 
      return 0; 
     } 
     if(std::cin.fail()) 
     { 
      std::cin.clear(); 
      std::cin.ignore(99999, '\n'); 
      std::cout << "Invalid input\n"; 
      // note eof() can be true here 
     } 
    } 
} 

#include <fstream> 

int read_balance(void) 
{ 
    std::ifstream f; 
    f.exceptions(std::ios::failbit | std::ios::badbit); 
    f.open("shop-account.txt"); 
    int balance; 
    f >> balance; 
    f.close(); 
    return balance; 
} 

void write_balance(int balance) 
{ 
    std::ofstream f; 
    f.exceptions(std::ios::failbit | std::ios::badbit); 
    f.open("shop-account.txt"); 
    f << balance; 
    f.close(); 
} 
+0

你不觉得这太过分吗? OP显然不知道编程,因为他甚至不能正确地初始化函数中的变量。现在你正在向他抛出异常。而这个“之前忽略:” - 这是很不清楚你的意图是在这里(我知道,你知道,但会开始程序员知道吗?) – zoska

+0

@zoska你不觉得你的评论有点可笑吗?他们正在学习编程。在C++中。当然,你告诉他们如何使用C++。编程。 (当然,除去那些调试遗留物,正如你所看到的,我倾向于很好地测试我的答案:) :) – sehe

+1

@zoska:多少太多了?你想提供废话代码只是因为有人没有看到好的代码?当你认为他们应该开始看到好的代码,那么? –