C++ 中的 For 循环- 变量没有正确增加
我一直在尝试编写一些代码,要求用户为会计程序提供多个借方条目。如果用户给出 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
好吧,在循环之前,您将
number
变量设置为 1。在循环内,您将
number
变量设置为(entries + 1)
使用这个片段:为什么不简单地这样做呢?
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:Why not simply do this?
您的内部 for 循环将始终在条目+1 处留下数字:
您是否打算在此处使用不同的变量,而不是重新使用现有的数字?
Your inner for loop will always leave number at entries+1:
Did you mean to use a different variable here, rather than re-use the existing
number
?显然,错误在于这部分代码:
您正在使用
number
来显示借记条目编号,并且它在上面的循环中递增到
entries + 1
Obviously the bug is in this part of code :
You are using
number
to display debit entry numberAnd it is incremented in the above loop to
enteries + 1
您输出
number
变量,该变量在内部for
循环中增加到entries
+1...You output the
number
variable, which you incremented in your innerfor
loop toentries
+1...你为什么不呢?
还有一些更接近现实生活的解决方案是不要单独保存它。创建结构体或类更加合乎逻辑。
Why don't you?
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.
您想将循环末尾的数字加 1。
最简单的方法是使用:
for 循环
是完全不必要的,编码时,始终从您可以想象的最简单的解决方案开始,并随着您的继续而变得更加复杂。
you want to add 1 to the number at the end of the loop.
The simplest way to do this is to use:
the for loop
is completely unneccesary, when coding, always start with the simplest solution you can imagine and get more complex as you proceed.