跟踪 if 语句被使用 true 的次数

发布于 2024-12-07 19:27:50 字数 690 浏览 0 评论 0原文

我认为这个问题本身就说明了问题,我正在用 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 语句为 true 的次数?

I think the question speaks for itself, I'm writing a program in c++ and there is a part where the console asks the user which type of input they want to use

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
}

but how do i keep track of how many times the if statement was true?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨洒年华 2024-12-14 19:27:50

您可以添加一个计数器,并在每次进入 if 语句的 true 块时递增它。

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

    ...

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

    ...

You can add a counter and increment it every time you enter the if-statement's true block.

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

    ...

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

    ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文