C++ 中的 For 循环- 变量没有正确增加

发布于 2025-01-03 17:54:51 字数 1558 浏览 1 评论 0原文

我一直在尝试编写一些代码,要求用户为会计程序提供多个借方条目。如果用户给出 5 作为答案,程序应询问他借方条目的名称和金额 5 次,然后再继续执行下一行代码。因此,我使用 for 循环来解决这个问题,似乎发生了一些奇怪的事情。

这是代码在编译和执行时应运行的格式:

您希望记入多少借方条目?:2
1.借方条目名称:example1
1.扣款金额:123
2.借方条目名称:example2
2.借方金额:456



继续执行下一行代码

以下是我用 C++ 为会计程序的这一部分编写的代码:

#include <iostream>
#include <string>
using namespace std;

int main()
{
   cout << "How many debit entries do you wish to make?: ";
   int entries;
   cin >> entries;
   string debitNames[50];
   float debitAmounts[50];
   int offset = 0;
   int number = 1;
   for (offset = 0; offset < entries; offset++)
   {
      cout << number << ". Debit Entry Name: ";
      cin >> debitNames[offset];
      cout << number << ". Debit Amount: ";
      cin >> debitAmounts[offset];
      for (number = 1; number <= entries; number++)
      {
      }
   }
   char response;
   cin >> response;
   return 0;
}



结果非常奇怪,这是我编译并运行它时发生的情况:

您希望进行多少借方条目?:5
1.借方条目名称:example1
1.扣款金额:123
6.借方条目名称:example2
6.借方金额:123
6.借方条目名称:example3
6.借方金额:123
6.借方条目名称:example4
6.借方金额:123
6.借方条目名称:example5
6. 借记金额:123


继续执行下一行代码

该程序在将数字列为 1 后,突然跳到 6 并保持该状态,直到所有 5 组问题都被问完。如您所知,现在我使用“number”变量来简单地告诉用户已询问“number”条目,如用户自己指定的那样。尝试在编译器中运行此代码,看看是否出现相同的结果。顺便问一下,我使用的是 Visual C++ 20120 Express Edition,这与该问题有什么关系吗?

多谢。

I have been trying to write some code that asks the user for several debit entries for an accounting program. If the user gives 5 as the answer, the program should ask him the debit entry's name and amount 5 times before continuing on to the next line of code. So, I used for loops to solve this problem and it seems something strange is happening.

This is the format in which the code should run when being compiled and executed:

How many debit entries do you wish to make?: 2
1. Debit Entry Name: example1
1. Debit Amount: 123
2. Debit Entry Name: example2
2. Debit Amount: 456

continue on to next line of code

Here's the code I've written in C++ for this part of the accounting program:

#include <iostream>
#include <string>
using namespace std;

int main()
{
   cout << "How many debit entries do you wish to make?: ";
   int entries;
   cin >> entries;
   string debitNames[50];
   float debitAmounts[50];
   int offset = 0;
   int number = 1;
   for (offset = 0; offset < entries; offset++)
   {
      cout << number << ". Debit Entry Name: ";
      cin >> debitNames[offset];
      cout << number << ". Debit Amount: ";
      cin >> debitAmounts[offset];
      for (number = 1; number <= entries; number++)
      {
      }
   }
   char response;
   cin >> response;
   return 0;
}

The outcome is extremely strange, here is what happens when I compile and run it:

How many debit entries to do you wish to make?: 5
1. Debit Entry Name: example1
1. Debit Amount: 123
6. Debit Entry Name: example2
6. Debit Amount : 123
6. Debit Entry Name: example3
6. Debit Amount: 123
6. Debit Entry Name: example4
6. Debit Amount: 123
6. Debit Entry Name: example5
6. Debit Amount: 123

continue on to next line of code

The program, after listing the number as 1, suddenly jumps off to 6 and remains there until all the 5 sets of questions have been asked. As you know by now I'm using the 'number' variable to simply tell the user that 'number' entries have been asked, as specified by the user his/herself. Try running this code in your compiler and see whether the same result appears. By the way, I use Visual C++ 20120 Express Edition, could this have anything do with the problem?

Thanks a lot.

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

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

发布评论

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

评论(6

赢得她心 2025-01-10 17:54:51

好吧,在循环之前,您将 number 变量设置为 1。

在循环内,您将 number 变量设置为 (entries + 1)使用这个片段:

for (number = 1; number <= entries; number++)
{
}

为什么不简单地这样做呢?

cout << (offset + 1) << ". Debit Entry Name: ";

Well, before the loop, you set the number variable to 1.

Inside the loop, you set the number variable to (entries + 1) using this snippet:

for (number = 1; number <= entries; number++)
{
}

Why not simply do this?

cout << (offset + 1) << ". Debit Entry Name: ";
笨笨の傻瓜 2025-01-10 17:54:51

您的内部 for 循环将始终在条目+1 处留下数字:

  for (number = 1; number <= entries; number++)
  {
  }

您是否打算在此处使用不同的变量,而不是重新使用现有的数字?

Your inner for loop will always leave number at entries+1:

  for (number = 1; number <= entries; number++)
  {
  }

Did you mean to use a different variable here, rather than re-use the existing number?

幸福丶如此 2025-01-10 17:54:51

显然,错误在于这部分代码:

for (number = 1; number <= entries; number++)
      {
      }

您正在使用 number 来显示借记条目编号

,并且它在上面的循环中递增到 entries + 1

Obviously the bug is in this part of code :

for (number = 1; number <= entries; number++)
      {
      }

You are using number to display debit entry number

And it is incremented in the above loop to enteries + 1

白云悠悠 2025-01-10 17:54:51

您输出 number 变量,该变量在内部 for 循环中增加到 entries+1...

You output the number variable, which you incremented in your inner for loop to entries+1...

救赎№ 2025-01-10 17:54:51

你为什么不呢?

  number++;
  for (int number = 1; number <= entries; number++)
  {
  }

还有一些更接近现实生活的解决方案是不要单独保存它。创建结构体或类更加合乎逻辑。

#include <iostream>
#include <list>
#include <time.h>
#include<string>
using namespace std;

struct Debt{

  string name;
  float owe;
};


int main()
{
cout << "How many debit entries do you wish to make?: ";
int entries;
cin >> entries;
list<Debt> debts;



for (int offset = 1; offset <= entries; offset++)
{
  Debt debt;

  cout << offset << ". Debit Entry Name: ";
  cin >> debt.name;

  cout << offset << ". Debit Amount: ";
  cin >> debt.owe;

  debts.push_back(debt);

  for (int number = 1; number <= entries; number++)
  {
  }
}
int i=0;
list<Debt>::iterator it;
for ( it=debts.begin() ; it != debts.end(); it++ )
  cout << "Debt seq " << ++i << " Name " << (*it).name << " debt is " << (*it).owe << endl;


char response;
cin >> response;
return 0;
}

Why don't you?

  number++;
  for (int number = 1; number <= entries; number++)
  {
  }

Also some more close to real life solution is to not to hold this separately. It is much more logical to create struct or class.

#include <iostream>
#include <list>
#include <time.h>
#include<string>
using namespace std;

struct Debt{

  string name;
  float owe;
};


int main()
{
cout << "How many debit entries do you wish to make?: ";
int entries;
cin >> entries;
list<Debt> debts;



for (int offset = 1; offset <= entries; offset++)
{
  Debt debt;

  cout << offset << ". Debit Entry Name: ";
  cin >> debt.name;

  cout << offset << ". Debit Amount: ";
  cin >> debt.owe;

  debts.push_back(debt);

  for (int number = 1; number <= entries; number++)
  {
  }
}
int i=0;
list<Debt>::iterator it;
for ( it=debts.begin() ; it != debts.end(); it++ )
  cout << "Debt seq " << ++i << " Name " << (*it).name << " debt is " << (*it).owe << endl;


char response;
cin >> response;
return 0;
}
叹梦 2025-01-10 17:54:51

您想将循环末尾的数字加 1。
最简单的方法是使用:

    number++;

for 循环

  for (number = 1; number <= entries; number++)   
  {   
  }

是完全不必要的,编码时,始终从您可以想象的最简单的解决方案开始,并随着您的继续而变得更加复杂。

you want to add 1 to the number at the end of the loop.
The simplest way to do this is to use:

    number++;

the for loop

  for (number = 1; number <= entries; number++)   
  {   
  }

is completely unneccesary, when coding, always start with the simplest solution you can imagine and get more complex as you proceed.

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