如何处理来自用户输入的偶数数组并在 C++ 中用空格显示它们
Posted
技术标签:
【中文标题】如何处理来自用户输入的偶数数组并在 C++ 中用空格显示它们【英文标题】:how to process an array of even numbers from a users input and display them with spaces in C++ 【发布时间】:2015-09-01 21:16:56 【问题描述】:我需要帮助以获取该用户输入的整数并检索偶数并用空格显示它们。我已经将输入处理为数组并将其反转(感谢***)现在需要提取偶数从数组中提取并显示它们。
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
int evenNumbers(char even[], int num[], int indexing[]);
int main()
char integers[5];
int numbers[5];
int even[5] = 0,2,4,6,8;
int evens;
cout << "Please enter an integer and press <ENTER>: " << endl;
for (int j = 0; j < 5; j++)
cin >> integers[j];
for (int j = 0; j < 5; j++)
numbers[j]= integers[j] - '0';
cout << endl;
for (int j = 5; j > 0; j--)
cout << integers[j - 1] << " ";
cout << endl;
//having problems finding the even numbers and displaying the even numbers
//from the users input of integers, i have only learned how to display the
//subscript by a linear search
evens = evenNumbers(integers, numbers, even);
if (evens == -1)
cout << "There are no even numbers" << endl;
else
cout << "The even numbers are: " << (evens + 1) << endl;
system("pause");
return 0;
int evenNumbers(char even[], int num[], int indexing[])
int index = 0;
int position = -1;
bool found = false;
for (int j = 0; j < 5; j++)
num[j]= even[j] - '0';
while (index < 5)
if (num[index] == indexing[index])
found = true;
position = index;
index++;
return position;
【问题讨论】:
>
前缀标记保留用于引用。
你的方法全错了,除此之外,它不能解释完美的偶数,如 10、12、14 等
你不应该使用你不理解的代码。
【参考方案1】:
如果要显示数组整数中的偶数,可以使用简单的for
循环和if
语句:
for(int i = 4; i >= 0; i--)
if(integers[i] % 2 == 0)
cout << integers[i] << " ";
【讨论】:
这是为了显示反转的偶数,以便将 for 循环更改为 (int i=0;i 【参考方案2】:您的方法完全错误,您无法通过搜索列表来检测偶数,您需要对偶数进行数学测试。编写一个名为is_even
的函数,它测试一个数字,如果是偶数则返回true,否则返回false。然后你就可以使用那个函数了,很简单,像这样
for (int j = 0; j < 5; j++)
if (is_even(integers[j]))
cout << integers[j] << " ";
cout << endl;
现在您只需要编写is_even
函数。
【讨论】:
【参考方案3】:void evennumbers(int num[])
for(int i=0;i<5;i++)
if(num[i]%2==0)
cout<<num[i]<<" ";
如果用户输入的数字超过一位,请避免输入 char 怎么办
【讨论】:
【参考方案4】:#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
void validNum(char valid[]);
void reverseNum(char rev[], int num2[]);
void evenNumbers(char even[], int num3[]);
void oddNumbers(char odd[], int num4[]);
int main()
char integer[5];
int number[5];
cout << "Your number is: ";
validNum(integer);
cout << "Your number in reverse is: ";
reverseNum(integer, number);
cout << "Even numbers: ";
evenNumbers(integer, number);
cout << endl;
cout << "Odd numbers: ";
oddNumbers(integer, number);
cout << endl;
system("pause");
return 0;
void validNum(char valid[])
char ch;
cout << "Please enter an integer and press <ENTER>: " << endl;
ch = cin.get;
while (ch < 0 || ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z')
cout << "ERROR: Please enter a positive integer and press <ENTER>: ";
for (int i = 0; i < 5; i++)
cin >> valid[i];
for (int j = 0; j < 5; j++)
cout << valid[j] - '0';
void reverseNum(char rev[], int num2[])
for (int j = 0; j < 5; j++)
num2[j]= rev[j] - '0';
cout << endl;
for (int j = 5; j > 0; j--)
cout << rev[j - 1]<< " ";
cout << endl;
void evenNumbers(char even[], int num3[])
for (int i = 0; i < 5; i++)
if (even[i] % 2 == 0)
cout << num3[i] << " ";
void oddNumbers(char odd[], int num4[])
for (int i = 0; i < 5; i++)
if (odd[i] % 2 == 1)
cout << num4[i] << " ";
【讨论】:
以上是关于如何处理来自用户输入的偶数数组并在 C++ 中用空格显示它们的主要内容,如果未能解决你的问题,请参考以下文章