Please help me how can I write the code here #include #include // Function to repeat a string by a given integer std::string multiply(const std::string& str, int num) { std::string result; for (int i = 0; i < num; ++i) { result += str; } return result; } // Function to multiply two integers int multiply(int num1, int num2) { return num1 * num2; } // Function to multiply a double and an integer double multiply(double num1, int num2) { return num1 * num2; } // Function template to handle multiple data types template T multiply_type(T num1, int num2) { return num1 * num2; } int main() { int choice; std::cout << "Choose a test case (1-5): "; std::cin >> choice; switch (choice) { case 1: { int num1, num2; std::cout << "Enter two integers: "; std::cin >> num1 >> num2; int result = multiply(num1, num2); std::cout << "Output: " << result << std::endl; break; } case 2: { int num1, num2; std::cout << "Enter two integers: "; std::cin >> num1 >> num2; int result = multiply(num1, num2); std::cout << "Output: " << result << std::endl; break; } case 3: { double num1; int num2; std::cout << "Enter a double and an integer: "; std::cin >> num1 >> num2; double result = multiply(num1, num2); std::cout << "Output: " << result << std::endl; break; } case 4: { std::string str; int num; std::cout << "Enter a string and an integer: "; std::cin >> str >> num; std::string result = multiply(str, num); std::cout << "Output: " << result << std::endl; break; } case 5: { std::string str; int num; std::cout << "Enter a string and an integer: "; std::cin >> str >> num; std::string result = multiply(str, num); std::cout << "Output: " << result << std::endl; break; } default: std::cout << "Invalid choice!" << std::endl; } return 0; }
Please help me
how can I write the code here
#include <iostream>
#include <string>
// Function to repeat a string by a given integer
std::string multiply(const std::string& str, int num) {
std::string result;
for (int i = 0; i < num; ++i) {
result += str;
}
return result;
}
// Function to multiply two integers
int multiply(int num1, int num2) {
return num1 * num2;
}
// Function to multiply a double and an integer
double multiply(double num1, int num2) {
return num1 * num2;
}
// Function template to handle multiple data types
template <typename T>
T multiply_type(T num1, int num2) {
return num1 * num2;
}
int main() {
int choice;
std::cout << "Choose a test case (1-5): ";
std::cin >> choice;
switch (choice) {
case 1: {
int num1, num2;
std::cout << "Enter two integers: ";
std::cin >> num1 >> num2;
int result = multiply(num1, num2);
std::cout << "Output: " << result << std::endl;
break;
}
case 2: {
int num1, num2;
std::cout << "Enter two integers: ";
std::cin >> num1 >> num2;
int result = multiply(num1, num2);
std::cout << "Output: " << result << std::endl;
break;
}
case 3: {
double num1;
int num2;
std::cout << "Enter a double and an integer: ";
std::cin >> num1 >> num2;
double result = multiply(num1, num2);
std::cout << "Output: " << result << std::endl;
break;
}
case 4: {
std::string str;
int num;
std::cout << "Enter a string and an integer: ";
std::cin >> str >> num;
std::string result = multiply(str, num);
std::cout << "Output: " << result << std::endl;
break;
}
case 5: {
std::string str;
int num;
std::cout << "Enter a string and an integer: ";
std::cin >> str >> num;
std::string result = multiply(str, num);
std::cout << "Output: " << result << std::endl;
break;
}
default:
std::cout << "Invalid choice!" << std::endl;
}
return 0;
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 4 images