跟踪if语句的使用次数是多少次

时间:2011-10-02 16:58:35

标签: c++ if-statement

我认为问题不言而喻,我正在用c ++编写一个程序,并且有一部分控制台询问用户他们想要使用哪种类型的输入

while (loop == 5) {
  cout << "\nWould you like to enter a depoist or a check? "; //asks for a choice
  cin >> choice;
  //determines whether or not to close the program
  if(choice == 0 || depo == 0 || check == 0) {
    return 0;          
  }//end close if
  //choses which type of input to make
  if( choice == 1) {
    cout << "\nPlease enter check amount: ";    
    cin >> check;
    check += check;
  } else if(choice == 2) {
    cout << "\nPlease enter deposit amount: ";
    cin >> depo;
    depo += depo;
  }//end if
}

但是如何跟踪if语句的真实次数?

1 个答案:

答案 0 :(得分:3)

每次输入if-statement的true块时,都可以添加一个计数器并递增它。

int true_counts = 0;
while (loop == 5){

    ...

    if( choice == 1){
        true_counts++;

    ...