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

标签: c++ arrays sorting visual-c++ arraylist

I need help with getting this users input of an integer and retrieving the even numbers and displaying them with spaces.I already have the input processed into an array and have it reversed (thanks to stackoverflow) now need to extract the even numbers from the array and display them.

#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;
}

4 个答案:

答案 0 :(得分:1)

If you want to display the even numbers from the array integers you can use a simple for loop and if statement:

for(int i = 4; i >= 0; i--)
{
    if(integers[i] % 2 == 0)
        cout << integers[i] << " ";
}

答案 1 :(得分:0)

Your approach is all wrong, you can't detect even numbers by searching a list, you need a mathematical test for evenness. Write a function called is_even which tests one number and returns true if it is even and false if it is not. Then you can use that function, very simply, like this

for (int j = 0; j < 5; j++)
{
    if (is_even(integers[j]))
        cout << integers[j] << " ";
}
cout << endl;

Now you just need to write the is_even function.

答案 2 :(得分:0)

void evennumbers(int num[])
{
    for(int i=0;i<5;i++)
    {
       if(num[i]%2==0)
          cout<<num[i]<<" ";
    }
}

And avoid taking input to char what if user enters a number with more than one digit

答案 3 :(得分:0)

#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] << " ";
       }
    }
}