Repeat section of code after IF statement

Hello,

I was wondering is there anyway for my code to repeat a certain process again without writing it over again.

Here is the part that I want to repeat.
1
2
3
4
5
6
7
8
9
10
cout << "Hello and welcome to Bobs's Pizza online ordering!" << endl;
cout << "Type 'food' to see food options and type 'drink' to see refreshments." << endl;
cin >> choice1;
if (choice1 == food) {
    cout << endl;
	cout << "Food Options" << endl;
	cout << endl;
	cout << "Pizza      $15" << endl;
	cout << "Salad      $7" << endl;.
}


And here is that part I want it to go after.

1
2
if (finished == no);
    cout << endl;


There must be an effient way of repeating it without copying and pasting it under it. Sorry if this is sloppy or if I haven't explained it well. Just started C++ last week :).

Thanks in advance.

If you need to look at the whole thing to understand it better, here it is.
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

int main()

{
//Food or Drink 1st Choice STRINGS and const char*
string choice1;
const char* food = "food";
const char* drink = "drink";
//---------------------------
cout << "Hello and welcome to Bobs's Pizza online ordering!" << endl;
cout << "Type 'food' to see food options and type 'drink' to see refreshments." << endl;
cin >> choice1;
if (choice1 == food) {
    cout << endl;
	cout << "Food Options" << endl;
	cout << endl;
	cout << "Pizza      $15" << endl;
	cout << "Salad      $7" << endl;
}
if (choice1 == drink) {
    cout << "Refreshments" << endl;
    cout << endl;
    cout << "Cola" << endl;
    cout << "Sprite" << endl;
    }
    else
    cout << "That wasn't a valid option.  Please try again." << endl;
    cin >> choice1;

// ANSWER 1 STRING and const char*
string answer1;
const char* pizza = "pizza";
const char* salad = "salad";
const char* cola = "cola";
const char* sprite = "sprite";
//-------------------------
cin >> answer1;
int cart=0; //------ CART IS DEClARED HERE
int ppizza=15; //----- PRICE OF PIZZA
float tax=.0875; // ----- TAX AMMOUT
if (answer1 == pizza) {
    cart=ppizza+cart;
    cout << endl;
    cout << "Pizza has been added to your cart" << endl;
    cout << endl;
    cout << "Would you like to go to checkout? (yes/no)" << endl;
    // the string finished decided whether you are done or not
    string finished;
    const char* yes = "yes";


    //-------------------------------------------------------
    cin >> finished;
int total;
if (finished == yes);
    total=cart*tax;
    cart=total+cart;

    cout <<"Your total is $" << cart << ".\n";


}
if (finished == no);
    cout << endl;
// REPEAT THE FIRST PART HERE
    
else 
    cout << "That wasn't a valid option.  Please try again." << endl;
    cin >> finished;
}

Last edited on
Just use a while loop. The syntax is:

while(condition){
your code here
}
now if I were you, I'd wrap a while loop around your if statement and use a condition of

while(choice1 != "done")

and add that "type 'done' if you are finished"
Thanks for the quick reply.

Wouldn't I still be pasting my code into there anyway? Is there not a way to make it more automated? Sorry if my question is really noobish.

This is what I did but when I run it, it works until I type in 'food'.

http://cpp.sh/8t4yz <--- shows what happens when you run it.

To replicate my problem, first type 'food' then 'pizza' then 'no'.

1
2
3
4
5
6
7
8
9
10
11
    while(choice1 !="no")
cout << "Type 'food' to see food options and type 'drink' to see refreshments." << endl;
cin >> choice1;
if (choice1 == food) {
    string choice2;
    cout << endl;
	cout << "Food Options" << endl;
	cout << endl;
	cout << "Pizza      $15" << endl;
	cout << "Salad      $7" << endl;
	cin >> choice2;
Last edited on
Hi,

59:21: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]


remove the semicolon at the end of line 59 and 67, and put braces around lines 60 to 63 and 68. Always use braces, even if there is 1 statement.

Consider using std::string instead of pointers to char arrays.

http://www.cplusplus.com/reference/string/string/

The is a whole reference section, tutorials and articles at the top left of this page :+)

Prefer double rather than float

Put the menu into a function of it's own.
Last edited on
Hello,

What differentiates using const char* and std::string ?
Thanks for the feedback. :)
Can you compare a string to an array of char??

Also what if the user enters Food or FOOD or FooD instead of "food"? You should lowercase or uppercase the string choice so that it will match "food" or "drink"

A simple way to post test loop the menu is a do while loop
do{
... statement

}while(true condition);

so in this case, you ask the user if he/she is finished Y for yes N for no

}while(choice == 'n' || choice == 'N');

this means while choice equals to n or N, then re-loop the menu
char * is a pointer-to-char or a C-Style string. std::string is better.
There are no differences, but the string object is better to use.
Also if you don't know this already, the keyword const makes a value unchangeble
Zarman wrote:
There are no differences, but the string object is better to use.


Well there are a lot of differences. I think you meant they are both a string of characters.

A char array is just that and nothing else. If one wants to do anything with it, then one needs a function, or write your own code.

std::string is a C++ class, and that causes it have all kinds of advantages.

It has constructors, overloaded operators, the memory is dynamically allocated, knows it's size, STL algorithms work with it, it has it's own functions that do things, and more things if one reads the links to the documentation I posted earlier.
Topic archived. No new replies allowed.