Help

Write a C++ program that prompts the user to select one of three television models and provides a description of the models. Use the switch control structure to display the model, the description, and the price that the user chooses.
The user enters model number and the corresponding output should be as shown below:
• Model 100: Remote control, timer, and stereo for $1000
• Model 200: The same as Model 100, plus picture-in-picture, for $1200
• Model 300: The same as Model 200, plus HDTV, flat screen, and a 16 x 9 aspect ratio, for $2400
Problem 2 (50 points):
Rewrite the above problem using If-Else-If control structure.
What is your question? Tell us specifically what you're having trouble with.

https://cplusplus.com/doc/tutorial/
Last edited on
Do you want HELP with problems you are having with code you've written, or do you want us to write code for you for free?
@abrahampaz,
We are not here to write your homework for you, we will help you...but you must first put some effort into it! Try writing your own code, then if you have problems, ask for help and post your code.

Thanks,
max
I agree that you need to put an effort on it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

int main()
{
	
	std::cout << "Enter Model Number: ";
	int modelNumber{};
	std::cin >> modelNumber;

	switch (modelNumber)
	{
		case 100:
		{
			std::cout << "Model 100: Remote control, timer, and stereo for $1000\n";
			break;
		}
		case 200:
		{
			std::cout << "Model 200: The same as Model 100, plus picture-in-picture, for $1200\n";
			break;
		}
		case 300:
		{
			std::cout << "Model 300: The same as Model 200, plus HDTV, flat screen, and a 16 x 9 aspect ratio, for $2400\n";
			break;
		}
		default:
		{
			std::cout << "The model " << modelNumber << " is not available\n";
		}
	}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <map>
#include <string>
#include <iostream>

struct Info{
	std::string desc;
	double price {};
};

int getInt(const std::string& prm)
{
	int i {};

	while ((std::cout << prm) && (!(std::cin >> i) || std::cin.peek() != '\n')) {
		std::cout << "Not an integer\n";
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	return i;
}

int main()
{
	const std::map<int, Info> stock {{ 100, {"Remote control, timer, and stereo", 1000} },
		{200, {"The same as Model 100, plus picture-in-picture", 1200}},
		{300, {"The same as Model 200, plus HDTV, flat screen, and a 16 x 9 aspect ratio", 2400}}};

	std::cout << "The available models are:\n";

	for (const auto& [model, info] : stock)
		std::cout << model << '\n';

	std::cout << '\n';

	const auto model {getInt("Enter model number: ")};

	if (const auto fnd {stock.find(model)}; fnd != stock.end())
		std::cout << "\nModel " << model << ": " << fnd->second.desc << " for $" << fnd->second.price << '\n';
	else
		std::cout << "\nThe model " << model << " is not available\n";
}

Topic archived. No new replies allowed.