Do While Error

Hi, I am new here. I am not looking for this to be solved for me but for a suggestion of how to fix this :-) -- I took a for loop problem, and tried to convert it to a 'do while'.

The goal is for the sandwich order to calculate cost, but also ask the user if they want to continue if input that is not choice : 1, 2 or 3. Also - should I be making the choices int vs string?

Thanks in advance for any help.

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 <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;
[/code]
Last edited on
1
2
3
4
5
6
int main()             // <==== don't put a semicolon here
{                      // <==== starts the main function statements

//  Do your stuff here

}                      // <==== ends the main function statements 



It looks like you tried to write far too much code at once.
Last edited on
That is a reasonable assessment lol. I will start there.

Thank you. I am dizzy from reading stack overflow rn.

A couple of things to note:

1.
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either

(you tried to use code tags, but inserting the space between / and code torched the tags.

2. Your code with code tags and formatting:
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 <iostream>
#include <string>
#include <limits>

int main(void);
int cost;
int sandwich;
using namespace std;
//I keep getting "expected unqualified ID" at the below bracket. My build then fails.
{

   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;

Your have more problems than just the do...while loops.

1. you can't redefine the main function. (the ; at the end is the killer)

2. your opening main function bracket is misplaced.

3. you are missing an ending bracket.

4. you are missing a ; when printing out your menu.

5. you have two do blocks, yet have only one while statement.

Thanks for the info. I will run down this list and see if I can fix this code. Also - noted on the formating.

Lisa
That error list may not be complete, it is just what was obvious from the error list Visual Studio vomited up and what I noticed at a quick glance.

When confronted by multiple compile errors tackling/fixing the errors one at a time, preferably from the top down, more often than not will make some or all of following errors disappear.

In C++ int main(void) is not invalid, but not recommended. It is the C way to write the main function that doesn't use any parameters passed to it by the OS.

https://stackoverflow.com/questions/12225171/difference-between-int-main-and-int-mainvoid
It's enough to get it closer to completion. Thanks for the links as well.

OK - I made several of the changes suggested, and a few of my own. I think I am close - but still no build.

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
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>
#include <limits>

using namespace std;
int main(void)
{
    const double Ham = 4;
    const double Veggie = 3;
    const double Rat = 2;
    
    int sandwich;
    int cost;

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

     
         cout << "Choose a sandwich: (1,2 or 3, others to exit)" << "\n";
         cin >> sandwich;
        
        //Validate choice
        while ((sandwich < 1) || (sandwich <3))
        {
            cout << "Please enter 1, 2, or 3:";
            cin >> sandwich;
        }
      
        //Calculate cost based on user input.
        if (sandwich == 1)
           cost = 4;
        else if (sandwich == 2)
           cost = 3;
        else
           if (sandwich == 3)
              cost = 2;
           else
            
             //  return cout << "Invalid sandwich\n";

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


   return 0;
}


}
1
2
3
4
5
do
{// <-- open brace
   //statements...
} // <-- close brace
while( condition ); // <-- semicolon 


also, let your IDE do the indentation.
Thanks. I have something functional now. I appreciate all the help. I will be staying in this forum to keep learning. :-)
This could be the way that you are probably thinking about input and output execution..
I made some changes in your code..
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
44
#include <iostream>
#include <string>
#include <limits>

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

{

   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;
         
         if (sandwich == 1) {
                cost = 4;
                cout << "The cost is $" << cost << '\n';
         }
         else if (sandwich == 2) {
                cost = 3;
                cout << "The cost is $" << cost << '\n';
         }
         else {
            if (sandwich == 3) {
                cost = 2;
                cout << "The cost is $" << cost << '\n'; 
            }
            else
                cout << "Invalid sandwich\n";
         }    
      }
      while (sandwich>0 && sandwich<4);
      
      if((sandwich < 0) || (sandwich > 4))
              cout << "invalid order ! " << endl;

   return 0;
}
Last edited on
Your final do loop doesn't really do anything as sandwich can't be both < 0 and > 4 at the same time!

Possibly:

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>

using namespace std;

int main() {
	cout << "1. Ham - $4\n"
		<< "2. Veggie - $3\n"
		<< "3. Rat - $2\n";

	int sandwich {}, cost {};

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

		if (sandwich == 1) {
			cost = 4;
			cout << "The cost is $" << cost << '\n';
		} else if (sandwich == 2) {
			cost = 3;
			cout << "The cost is $" << cost << '\n';
		} else {
			if (sandwich == 3) {
				cost = 2;
				cout << "The cost is $" << cost << '\n';
			}
		}
	} while (sandwich > 0 && sandwich < 4);

	cout << "Thanks for ordering. Bye\n";
}

yeah, there should be || between the each cases..

Also there need not be the loop also in that case (message is updated now)
Are you sure about L38 in your update post???

Topic archived. No new replies allowed.