How to get code to loop back to the beginning?

I have my code completely working but cant get it to loop back to the very begining. I need it to say "Enter the number of books in the sale" instead it says "Enter price: ". What do I need to do to have it loop back to the beginning completely?

Thanks,

TECK

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

using namespace std;

int main()
{
  char shipping;
  int num, counter, book;
  float tax, sub, discount,  priceOfBook, totalBooks, totalshipping, totalStanda\
rd, totalExpedited, total;

  cout<<"Enter the number of books in the sale: ";
  cin>>book;

  while(book !=0)
    {
      sub = 0;
      for(counter = 1; counter <= book; counter++)
        {
          cout<<"Enter price: ";
          cin>>priceOfBook;

          sub += priceOfBook;
          tax = sub * .05;
        }

      cout<<"Enter shipping method [S]Standard [E]Expedited shipping: ";
      cin>>shipping;

       if(shipping == 'S' || shipping == 's')
        {
        totalshipping =  4.99;
        }
      else
        {
        totalshipping = 12.99;
        }

      if(sub <= 50)
        {
        discount = 0.00;
        }
        else if(sub >= 50 && sub <= 100)
          {
          discount = sub * .1;
          }
        else if(sub > 100)
          {
          discount = sub * .15;
          }

      total = (sub + tax + totalshipping) - discount;

      cout<<setprecision(2)<<fixed;
      cout<<"Subtotal: " <<setw(20) <<"$" <<sub <<endl;
      cout<<"Tax: " <<setw(25) <<"$" <<tax  <<endl;
      cout<<"Discount: " << setw(20) <<"$" <<discount <<endl;
      cout<<"Shipping: " <<setw(20) <<"$" <<totalshipping <<endl;
      cout<<"Total: " <<setw(23) <<"$" <<total <<endl;
    }
  cout<<"Error! There must be a book you want to buy! " <<endl;
  return 0;
}
Just use your main(); function

EXAMPLE, look all the way down.

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
75
76

#include <iostream>
#include<iomanip>

using namespace std;

int main()
{
  char shipping;
  int num, counter, book;
  float tax, sub, discount,  priceOfBook, totalBooks, totalshipping, totalStanda\
rd, totalExpedited, total;

  cout<<"Enter the number of books in the sale: ";
  cin>>book;

  while(book !=0)
    {
      sub = 0;
      for(counter = 1; counter <= book; counter++)
        {
          cout<<"Enter price: ";
          cin>>priceOfBook;

          sub += priceOfBook;
          tax = sub * .05;
        }

      cout<<"Enter shipping method [S]Standard [E]Expedited shipping: ";
      cin>>shipping;

       if(shipping == 'S' || shipping == 's')
        {
        totalshipping =  4.99;
        }
      else
        {
        totalshipping = 12.99;
        }

      if(sub <= 50)
        {
        discount = 0.00;
        }
        else if(sub >= 50 && sub <= 100)
          {
          discount = sub * .1;
          }
        else if(sub > 100)
          {
          discount = sub * .15;
          }

      total = (sub + tax + totalshipping) - discount;

      cout<<setprecision(2)<<fixed;
      cout<<"Subtotal: " <<setw(20) <<"$" <<sub <<endl;
      cout<<"Tax: " <<setw(25) <<"$" <<tax  <<endl;
      cout<<"Discount: " << setw(20) <<"$" <<discount <<endl;
      cout<<"Shipping: " <<setw(20) <<"$" <<totalshipping <<endl;
      cout<<"Total: " <<setw(23) <<"$" <<total <<endl;

	  /*Redo*/
	  int decision;
	  cout<<"Type 1 to redo this again: ";
	  cin>>decision;
	  if(decision == 1){
		  main(); //Your main function where it is called to redo
	  }else{
		  return 0;
	  }
    }
  cout<<"Error! There must be a book you want to buy! " <<endl;
  return 0;
}
Last edited on
add a new statement:
1
2
 cout<<"Enter the number of books in the sale: ";
  cin>>book;


at the bottom of the loop while
Eyenrique's answer is more helpful than Backslashx00's. Involving the main function in recursion is not a good idea, especially in your case since calling the main function from within a while loop has the potential for creating a never-ending loop. For example, your user went through several main calls, but once he or she enters 0 for the number of books, the user will be confused when "Enter Price" pops up. Then, your user would either have to close the window via the button or he or she would have to enter 0 so many times.
Last edited on
Hmm.. did not think of it like that. Guess I had a bad habit of re-using that function.
Last edited on
@Daleth is right, an iterative solution is better than a recursive way for this problem.
Post the complete assignment and i could help you!
@eyenrique http://www.cplusplus.com/forum/articles/1295/
Please don't give people solutions to their homework. It doesn't help them learn and it doesn't help this site. People start to expect/demand solutions when they show up and creates a very negative attitude from the people who regularly help people out.

By all means give them sample pieces of code or explain things to them, but doing their assignment is highly discouraged.
I expect responsibility from the people who ask for help, i suppose that this people are actually in some courses where they learn basic concepts -from their teachers- or are studying some textbook and if i do a partial or complete solutions they can learn by examining the code. I like share solutions -if i have it- and if i can't share solutions so i think that i'm in the wrong site.
Topic archived. No new replies allowed.