-3

I am new to C++... I am having trouble to get even & odd numbers from a positive integer.. But for some reason it is not displaying it correctly. So, in my if-else statement I am using the mod operator and using even = even * 10 + remainder; for even numbers and for the odd number I am trying to do is: odd = odd * 10 + remainder;.. I am getting an error with

Invalid operands to binary expression.

So, can anyone help me solve the problem by getting the even & odd numbers from a positive integer. Thanks.

Here is my code:

#include <iostream>
#include <fstream> // for file stream. 



using namespace std;

int main() {

    // Variables 
    int x, reversedNumber, remainder;

    // Creating String variables and setting them to empty string. 
    string even = "", odd = "";

    // Creating a file. 
    ofstream out;
    out.open("outDataFile.txt");

    // creating a character variable 
    // for the user input if they want to use the program again. 
    char ch;

    do {
        // Even number. 
        even = "";

        // Odd number.
        odd = "";

        // Reversed number
        reversedNumber = 0;

        // Prompt the user to enter a positive integer. 
        cout << "\nEnter a positive integer and press <Enter> ";


     // Validate user input. 
//        if (cin >> x && x < 0) {
//
//        } else {
//             cout << "Invalid entry, Try again." << endl;
//            cin.clear();
//            while (cin.get() != '\n');  
//        }

        // Display Results to the screen
        cout << "the original number is " << x << "\n";

        // Display results in the text file. 
        out << "the original number is " << x << "\n";

        // Display number reversed. 
        cout << "the number reversed ";

        // Display number reversed in text file. 
        out << "the number reversed ";

        //Reversing the integer. 
        while (x != 0) {
            remainder = x % 10;

            reversedNumber = reversedNumber * 10 + remainder;

            // Display on screen 
            cout << remainder << " ";

            // Display in text file. 
            out << remainder << " ";

            x /= 10;

        }

        // Display the results on screen and in the text file. 
        cout << "\n";
        out << "\n";


        // Reading the reverse numbers result.
        while (reversedNumber != 0) {

            remainder = reversedNumber % 10;


            // Checking if the number is even or odd. 
            if (remainder % 2 == 0) {

              // even = even * 10 + remainder;


            } else  {


              //  odd = odd * 10 + remainder;



            }

            reversedNumber /= 10;

        }

        //Displaying the even numbers. 

        if (even != "") {

            cout << "the even digits are " << even << "\n";


            out << "the even digits are " << even << "\n";


        }          

        // If it is not even then display.. 
        else {

            cout << "There are no even digits \n";

            out << "There are no even digits \n";

        }

        //Display the odd results. 

        if (odd != "") {

            cout << "the odd digits are " << odd << "\n";

            out << "the odd digits are " << odd << "\n";

        }           
         // If its not odd then display. 
        else {

            cout << "There are no odd digits \n";

            out << "There are no odd digits \n";

        }

        // just a divider to divide the results inside text file.
        out << "----------------- \n";

        // Prompt the user if they want to use the program again.
        cout << "\nDo you like to continue/repeat? (Y/N):";

        // get the input from user. 
        cin >> ch;

        if ((ch == 'Y') || (ch == 'y')) {

        } else {
            cout << "\nGoodbye!" << endl;
        }

    } while (ch == 'y' || ch == 'Y');


    // close the text file. 

    out.close();

    return 0;

}
smac89
  • 39,374
  • 15
  • 132
  • 179
  • There is no overload for the `+` operator for string and integers. See here https://en.cppreference.com/w/cpp/string/basic_string/operator%2B – smac89 Jun 29 '18 at 02:06
  • Upon further inspection of your code, you are attempting to do `even * 10 + remainder;` when `even` is `std::string`....what do you expect that expression to give you? – smac89 Jun 29 '18 at 02:08
  • I see that it is a string.... how can I accomplish this, prior of being new to c++, I need some help - so I can grasp some snippets from you guys . – Harut Simonyan Jun 29 '18 at 02:09
  • Convert the string to a number before using it like that...See https://en.cppreference.com/w/cpp/string/basic_string/stol. Then after you are done, convert it back to a string with https://en.cppreference.com/w/cpp/string/basic_string/to_string – smac89 Jun 29 '18 at 02:09
  • is it the place when I am calling `even = ""` and `odd=""` ? – Harut Simonyan Jun 29 '18 at 02:11
  • I recommend not even storing `even` and `odd` as strings, just have them as integers, then convert them to string (using `std::to_string`) after you are done – smac89 Jun 29 '18 at 02:12
  • can you please show me ? – Harut Simonyan Jun 29 '18 at 02:13
  • As for your question, what I meant was `even = std::to_string(std::stoi(even) * 10 + remainder)` – smac89 Jun 29 '18 at 02:13
  • I will give an answer as it is much easier – smac89 Jun 29 '18 at 02:13
  • is there any easier way without `to_string` ? – Harut Simonyan Jun 29 '18 at 02:16
  • There is an easier way without using `to_string`, see my answer below – smac89 Jun 29 '18 at 02:23

1 Answers1

2

Convert the number to string before appending it to the string:

while (reversedNumber != 0) {
    remainder = reversedNumber % 10;

    // Checking if the number is even or odd. 
    if (remainder % 2 == 0) {
      even += std::to_string(remainder);    
    } else  {
      odd += std::to_string(remainder);
    }

    reversedNumber /= 10;
}

If you are uncomfortable with using std::to_string, then you can have even and odd as integers and simply do this:

int even = 0;
int odd = 0;

...

while (reversedNumber != 0) {
    remainder = reversedNumber % 10;

    // Checking if the number is even or odd. 
    if (remainder % 2 == 0) {
      even *= 10 + remainder;    
    } else  {
      odd *= 10 + remainder;
    }

    reversedNumber /= 10;
}
smac89
  • 39,374
  • 15
  • 132
  • 179