The do while return error.

Hello comrades, welcome me to this forum coz am a newbie here. Am not after a person who can work out this for me, my focus is in getting relevant information that can lead me the right way so that I can do the fixing by myself. With me is a for loop problem that am trying to convert it to a ‘do while’. https://www.theengineeringprojects.com/2021/02/while-loop-in-javascript.html My focus is to see the program do a calculation of the cost of a product but also if the person who is using the program wishes to continue if the input that they did was not their choice, they should do so.

Thanks for any assistance that I will get.

#include <iostream>
#include <string>
#include <limits>

int cost;
int sandwich;
using namespace std;
int main(void)

{

cout << "1. Ham - $4\n"
<< "2. Veggie - $3\n"
<< "3. Rat - $2\n"

do
{
cout << "Choose a sandwich: (1,2 or 3, others to exit)" << "\n";
cin >> sandwich;
while ((sandwich > 0) && (sandwich < 4));

do
{
cout << "Invalid Order)" << "\n";

while ((sandwich < 0) && (sandwich > 4));

}
if (sandwich == "1. ham")
cost = 4;
else if (sandwich == "2. veg")
cost = 3;
else
if (sandwich == "3. rat")
cost = 2;
else
return (std::cout << "Invalid sandwich\n"));

cout << "The cost is $" << cost << '\n';
}


return 0;
Last edited on
Perhaps:

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
#include <iostream>

int main(void) {
	int cost{};
	int sandwich{};

	do {
		std::cout << "\n1. Ham - $4\n"
			<< "2. Veggie - $3\n"
			<< "3. Rat - $2\n";

		std::cout << "Choose a sandwich: (1,2 or 3, others to exit): ";
		std::cin >> sandwich;

		switch (sandwich) {
		case 1:
			cost += 4;
			break;

		case 2:
			cost += 3;
			break;

		case 3:
			cost += 2;
			break;
		}

		std::cout << "The total cost is $" << cost << '\n';
	} while (sandwich >= 1 && sandwich <= 3);
}

Topic archived. No new replies allowed.