0
#include <iostream>
using namespace std;

int main()
{
   char num[10];
   int a;
   cout << "Odd or Even"<< endl;
   for(;;)
   {
      cout << "Enter Number:" ;
      cin >> num;
      cout << endl;
      for(a=9;a>=0;a--)
      {
         if(num[a]!='\0 && num[a]!=' ')
            break;
      }
      if(num[a]==1 || num[a]==3 || num[a]==5 || num[a]==7 || num[a]==9)
         cout << "Odd" << endl;
      else
         cout << "Even" << endl;
   }
}

I am a rookie of C++,and I wrote a program to discriminate if a number is even or odd, but no matter what number I enter, it only outputs "Even". So I added these to find out when did the loop breaks:

cout << a << endl;
cout << "\"" << num[a] << "\"" << endl;

Result:

Enter Number:11
9
" "
Even

the for loop beraks when num[9]=' '? Which will lead to else and always output "Even".

3
  • 1
    if(num[a]==1 ... Try if(num[a]=='1'. Commented Jul 30, 2014 at 4:12
  • Your approach is wrong, try to take an int array instead of a char array for numbers. Commented Jul 30, 2014 at 4:20
  • an int is four bytes and a char is only one byte,so is a number,so if i take int num[80] instead of char num[80],three-fourths of the 80*4=320 bytes will be filled with ' ' which is a waste of memory.Also if i take int ,the compiler build message will list hundreds of "argument......"which i dont want to understand. Commented Jul 30, 2014 at 15:58

3 Answers 3

1

You are confused about the character '1' and the number 1. They are different.

Instead of

  if(num[a]==1 || num[a]==3 || num[a]==5 || num[a]==7 || num[a]==9)

you need

  if(num[a]=='1' || num[a]=='3' || num[a]=='5' || num[a]=='7' || num[a]=='9')

Update

There is one more problems that is probably tripping you up.

  1. num is not initialized. Zero-initialize it. Remember 0 is not the same as the character '0'.

    char num[10] = {0};
    
  2. Move the initialization of num inside the for loop. That will eliminate the problem of data from a previous execution of the loop from affecting the current execution of the loop.

Here's a version that works for me.

#include <iostream>
using namespace std;

int main()
{
   cout << "Odd or Even"<< endl;
   for(;;)
   {
      char num[10] = {0};
      int a;
      cout << "Enter Number:" ;
      cin >> num;
      cout << endl;
      for(a=9;a>=0;a--)
      {
         if(num[a]!='\0' && num[a]!=' ')
            break;
      }
      cout << num[a] << endl;
      if(num[a]=='1' || num[a]=='3' || num[a]=='5' || num[a]=='7' || num[a]=='9')
         cout << "Odd" << endl;
      else
         cout << "Even" << endl;
   }
}

PS

You can replace the line

         if(num[a]!='\0' && num[a]!=' ')

by

         if(isdigit(num[a]))

That makes more sense to me.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your correction , but the program still goes wrong
0

If you are doing this with c++ there are much easier ways! Consider the following:

while (!done) {
    string inputline;
    getline(cin, inputline); //Now we have a string with the users input!

    stringstream ss; // stringstreams help us parse data in strings!
    int num; // We have a number we want to put it into.
    ss >> num; // We can use the string stream to parse this number. 
               // You can even add error checking!

    // Now to check for odd even, what do we know about even numbers? divisable by 2!
    if (num % 2 == 0) // No remainder from /2
       cout << Even << '\n'
    else
       cout << Odd << '\n'
}

see how you go with that!

Warning Untested code

2 Comments

i know there`s a lot of ways faster ,but i still want to know the errors I made.
Thats fine there are plenty of answers that show you where your problem was. I am just trying to show a different solution.
0

You did a mistake (typo) here in this line..

if(num[a]!='\0 && num[a]!=' ')

it should be

if(num[a]!='\0' && num[a]!=' ')

1 Comment

thanks but that`s the typo i made in stackflow not on the editor

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.