所以我遇到了一些问题,但我不知道这是我的代码还是我的编译器造成的问题。我的任务是要求用户输入整数,而程序将偶数存储在一个数组中,并将赔率存储在另一个数组中。当用户输入0时,程序应该向他们显示他们输入了多少个均值和多少赔率,然后列出每个数组中的值。
所以我很快就为它编写了一个程序,我认为它可以正常工作。没有这样的运气。它总是得到平均数和赔率数正确,但它不是列出数组中的数字,而是通常列出用户输入的最后x个数字(x等于该数组中的元素数)。我偶尔也会在输出的末尾出现古怪的5位数整数。
现在,问题是,我的代码看起来没有任何错误,所以我认为可能是我的编译器正在创建问题。
所以这是我的代码,无论如何:
#include <iostream>
using namespace std;
int main() {
int input, remainder, even = 0, odd = 0;
int evenArray[even];
int oddArray[odd];
cout << "This program accepts integers until you enter 0.\nPlease enter a value: ";
cin >> input;
while (input != 0) {
remainder = input % 2;
if (remainder == 0) {
evenArray[even] = input;
even++;
}
else {
oddArray[odd] = input;
odd++;
}
cout << "Enter another integer: ";
cin >> input;
}
cout << "\nThe number of evens is " << even << ".\n";
cout << "The even values are: ";
for(int i = 0; i < even; i++) {
cout << evenArray[i] << " ";
}
cout << endl;
cout << "The number of odds is " << odd << ".\n";
cout << "The odd values are: ";
for(int i = 0; i < odd; i++) {
cout << oddArray[i] << " ";
}
}
这是一个运行的例子:
This program accepts integers until you enter 0.
Please enter a value: 1
Enter another integer: 2
Enter another integer: 3
Enter another integer: 4
Enter another integer: 5
Enter another integer: 5
Enter another integer: 3
Enter another integer: 2
Enter another integer: 4
Enter another integer: 6
Enter another integer: 4
Enter another integer: 3
Enter another integer: 0
The number of evens is 6.
The even values are: 2 4 2 4 6 3
The number of odds is 6.
The odd values are: 2 4 2 4 1912356720 32767
如果有人能告诉我这里的问题,我将非常感激。
答案 0 :(得分:2)
对于初学者来说,变量长度数组不是标准的C ++特性,而且你可能不会像这样声明一个元素数量等于0的数组
int input, remainder, even = 0, odd = 0;
int evenArray[even];
int oddArray[odd];
并且这样的声明没有意义。
如果在编译时未知数组的大小,则使用标准容器std::vector
。
这是一个示范程序
#include <iostream>
#include <vector>
int main()
{
using namespace std;
std::vector<int> evenArray, oddArray;
int input;
cout << "This program accepts integers until you enter 0.\nPlease enter a value: ";
while ( cin >> input and input != 0 )
{
if ( input % 2 == 0)
{
evenArray.push_back( input );
}
else
{
oddArray.push_back( input );
}
cout << "Enter another integer: ";
}
cout << "\nThe number of evens is " << evenArray.size() << ".\n";
cout << "The even values are: ";
for ( int x : evenArray )
{
cout << x << ' ';
}
cout << endl;
cout << "The number of odds is " << oddArray.size() << ".\n";
cout << "The odd values are: ";
for ( int x : oddArray )
{
cout << x << ' ';
}
cout << endl;
return 0;
}
另一种方法是声明一些固定最大大小的数组,并限制用户输入的元素不会超过给定的最大值。例如
#include <iostream>
int main()
{
using namespace std;
const size_t N = 100;
int evenArray[N], oddArray[N];
size_t even = 0, odd = 0;
cout << "This program accepts integers until you enter 0.\nPlease enter a value: ";
int input;
while ( even < N and odd < N and cin >> input and input != 0 )
{
if ( input % 2 == 0)
{
evenArray[even++] = input;
}
else
{
oddArray[odd++] = input;
}
cout << "Enter another integer: ";
}
cout << "\nThe number of evens is " << even << ".\n";
cout << "The even values are: ";
for ( size_t i = 0; i < even; i++ )
{
cout << evenArray[i] << ' ';
}
cout << endl;
cout << "The number of odds is " << odd << ".\n";
cout << "The odd values are: ";
for (size_t i = 0; i < odd; i++)
{
cout << oddArray[i] << ' ';
}
cout << endl;
return 0;
}
答案 1 :(得分:0)
我想知道你是如何编译的......
无论如何,使用std::vector
作为“动态数组”,因为您不知道用户将输入多少个数字。
另一件事是你可以使用二元运算符&
来加速奇偶校验。您只需将您的号码与someNumber & 1
一样。如果结果为1,则数字为奇数。
实施例
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> even, odd;
int userDecision;
while(cin >> userDecision && userDecision) {
int input;
cin >> input;
if((input & 1) == 1) {
odd.push_back(input);
} else {
even.push_back(input);
}
}
cout << "Number of evens " << even.size() << endl;
// Print even numbers
cout << "Number of odds " << odd.size() << endl;
// Print odd numbers
return 0;
}
答案 2 :(得分:0)
你有两个问题:
1)int evenArray[even];
通常,C ++不支持可变长度数组。有些编译器会这样做,但你永远不应该这样做。
2)即使1)不成立,也要初始化even
和odd
,其大小为0
。
所以,就像我说这是C ++一样,如果已知大小,你应该总是尝试使用std::vector
或std::array
。以下是您的计划的外观:
#include <iostream>
#include <vector>
int main() {
std::vector<int> even, odd;
int input;
std::cout << "This program accepts integers until you enter 0.\nPlease enter a value: ";
while (std::cin >> input && input != 0)
(input % 2 == 0) ? even.push_back(input) : odd.push_back(input);
std::cout << "\nThe number of evens is " << even.size() << ".\n";
std::cout << "The even values are: ";
for(unsigned i = 0; i < even.size(); ++i)
std::cout << even[i] << " ";
std::cout << std::endl << "The number of odds is " << odd.size() << ".\n";
std::cout << "The odd values are: ";
for(unsigned i = 0; i < odd.size(); ++i)
std::cout << odd[i] << " ";
return 0;
}
答案 3 :(得分:0)
嗯,这里有你需要注意的事情:
来自维基百科页面的示例:
int arr[4] = {0, 1, 2, 3};
int *p = arr + 5; // undefined behavior