Answer to Question #278304 in C++ for Develskie

Question #278304

write a program that will accept two different integers, or two different floatingpoint numbers, or two different characters or two different strings then output the value that is greater between the two values entered by the user. use function overloading to solve this.

1
Expert's answer
2021-12-11T01:58:20-0500
#include <iostream>
#include <string>
using namespace std;


void PrintGreater(int i, int j) {
    if (i < j) {
        cout << j << " is greater than " << i << endl;
    }
    else {
        cout << i << " is greater than " << j << endl;
    }
}


void PrintGreater(double x, double y) {
    if (x < y) {
        cout << y << " is greater than " << x << endl;
    }
    else {
        cout << x << " is greater than " << y << endl;
    }
}


void PrintGreater(char ch1, char ch2) {
    if (ch1 < ch2) {
        cout << ch2 << " is greater than " << ch1 << endl;
    }
    else {
        cout << ch1 << " is greater than " << ch2 << endl;
    }
}


void PrintGreater(string str1, string str2) {
    if (str1 < str2) {
        cout << '\"' << str2 << "\" is greater than \"" << str1 << '\"' << endl;
    }
    else {
        cout << '\"' << str1 << "\" is greater than \"" << str2 << '\"' << endl;
    }
}


int main() {
    int i1, i2;
    double x1, x2;
    char ch1, ch2;
    string str1, str2;


    cout << "Enter two integer values: ";
    cin >> i1 >> i2;
    PrintGreater(i1, i2);


    cout << endl << "Enter two double values: ";
    cin >> x1 >> x2;
    PrintGreater(x1, x2);


    cout << endl << "Enter two chars: ";
    cin >> ch1 >> ch2;
    PrintGreater(ch1, ch2);


    cout << endl << "Enter two strings: ";
    cin >> str1 >> str2;
    PrintGreater(str1, str2);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog