Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have to create a program where depending on the number you input a certain for

ID: 3816445 • Letter: I

Question

I have to create a program where depending on the number you input a certain fortune would appear.Example: If you input the house number 8045 your output will be : It is eight, "Faith", Holy House!

I currently wrote my program using hard code, but I have to convert it to Object-oriented programming where I am having a lot of trouble.

This is my code written in hard coding:

int main() {
string c;
int n, rem , sum = 0;
cout << "Enter Your House Number: ";
cin >> n;
cout << endl;
while(n > 0){
while(n != 0){
rem = n% 10;
sum = sum + rem;
n = n/10;
}
if(sum > 9){
n = sum;
sum = 0;
}
}
cout << "Your lucky number is: " << sum << endl;
  
switch(sum){
case 1:
cout << "Message: It is one, Bingo! Lucky House"<< endl;
break;
case 2:
cout << "Message: It is two, For you! Sweet Home" << endl;
break;
case 3:
cout <<"Message: It is three, Normal, Formal House!" << endl;
break;
case 4:
cout <<"Message: It is four, Normal, Formal House!" << endl;
break;
case 5:
cout <<"Message: It is five, Normal, Formal House!" << endl;
break;
case 6:
cout <<"Message: It is six, Normal, Formal House!"<< endl;
break;
case 7:
cout <<"Message: It is seven, Heaven! Dream House!"<<endl;
break;
case 8:
cout <<"Message: It is eight, Faith, Holy House!" << endl;
break;
case 9:
cout <<"Message: It is nine, Fine! Well House" << endl;
break;
default :
cout << "Invalid House Number..! Try Again" << endl;   
}
return 0;
}

This is what I have for OOP:

class HouseNum{
private:
int number;
int sum;
public:
//Default constructor
HouseNum(){
number =0;
sum = 0;
}
  
friend ostream &operator << (ostream &output, HouseNum &p);
friend istream &operator >> (istream &input, HouseNum &p);
};

ostream &operator << (ostream &output, HouseNum &p){
cout << "Your number is " << p.number<< endl;
cout << "Your sum is: " << p.sum<< endl;
return output;
}

istream &operator >> (istream &input, HouseNum &p){
input >> p.number;
return input;
}

int main() {
cout << "Enter your House number: ";
HouseNum num;
cin >> num;
cout << num;
return 0;
}

Im stuck here, Im not sure what to do

Explanation / Answer

#include <iostream>
using namespace std;

class HouseNum
{
private:
    int number;
    int sum;
public:
  
    HouseNum(int number)
    {
        this->number = number;
    }
    void LuckyNumber()
    {
    int n = number;
    int rem;

    while(n > 0)
    {
        while(n != 0){
            rem = n% 10;
            sum = sum + rem;
             n = n/10;          
        }
        if(sum > 9){
            n = sum;
            sum = 0;
        }
    }
  
  
        cout << "Your lucky number is: " << sum << endl;
    switch(sum){
        case 1:
            cout << "Message: It is one, Bingo! Lucky House"<< endl;
            break;
        case 2:
            cout << "Message: It is two, For you! Sweet Home" << endl;
            break;
        case 3:
            cout <<"Message: It is three, Normal, Formal House!" << endl;
            break;
        case 4:
            cout <<"Message: It is four, Normal, Formal House!" << endl;
            break;
        case 5:
            cout <<"Message: It is five, Normal, Formal House!" << endl;
            break;
        case 6:
            cout <<"Message: It is six, Normal, Formal House!"<< endl;
            break;
        case 7:
            cout <<"Message: It is seven, Heaven! Dream House!"<<endl;
            break;
        case 8:
            cout <<"Message: It is eight, Faith, Holy House!" << endl;
            break;
        case 9:
            cout <<"Message: It is nine, Fine! Well House" << endl;
            break;
        default :
            cout << "Invalid House Number..! Try Again" << endl;         
    }
    }
  
    friend ostream &operator << (ostream &output, HouseNum &p);
    friend istream &operator >> (istream &input, HouseNum &p);
};

ostream &operator << (ostream &output, HouseNum &p){
    cout << "Your number is " << p.number<< endl;
    cout << "Your sum is: " << p.sum<< endl;
    return output;
}

istream &operator >> (istream &input, HouseNum &p){
    input >> p.number;
    return input;
}

int main()
{
    int num,sum;
    cout << "Enter your House number: ";
    cin >> num;
  
  
    HouseNum HN(num);

  
    HN.LuckyNumber();
    cout<<HN;
  
    return 0;
}

Output: