looping i'm unsure

I have a c++ problem regarding looping. I am writing a program which will include a problem similar to below.

1) ok, i am writing a program that asks the user to enter a number.

2) Then ask the user if the number entered was correct.

3) If the number is correct, the user will enter 'y' or 'yes' and the program will terminate.
(pretty simple so far ?

4) If the user enters 'n' or 'no' the program should loop back to the beginning of the program and start again. goes back to 1)

5) However, if the user enters anything other than 'yes' or 'no', the program rejects this input and tells the user to re-enter, using only 'yes' or 'no'.

6) This should be asked infinitely until either 'yes' or 'no' is inputted, after which the program then loops back to the beginning.
how would i do this ?

i've been trying do while and switch but no luck. would i have to use a for loop ?

Thanks
ps. i am sorry if this was posted in the wrong forum, but i wasnt quite sure whether to put it here, (as i am a beginner) or in the general forum
You definetly posted this in the right forum :)

Post your code* and i'll be happy to help you correct it, step by step :)

* Use code tags!!! ;)
Try a the CASE function for yes,no and Error. the case function should be inside a loop(you choose) . Example if you use for(;;) it should do the trick.... and don't forget to put a break; at the end of instructions concerning Yes as answer.(in order to exit the loop) (",) try it let me know how it goes.
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.h>
#include <string>
#include <conio.h>
void funct()

main()
{
	string date;
   char answer; //either yes or no
   bool check;

do	{
		cout << "\nplease enter the date in dd/mm/yyyy: "; //beginning of loop********************************
      cin >> date;

      cout << "\nIs this the correct date ? press y/n \n" << date << "\n\n";
      cin >> answer;

      if ('y') << cout "\nThankyou!\n";
      break;
      if (!'n') << cout << "\nretry\n";
                  funct();
      else answer

      	case 'y':
         	cout << "\nThank you!!\n";
            break;

      }while (answer != 'y');
   }  getch();
}


i solved it i think. i previously put 'while' on the line above the line it is on now.
what do you think ?




I then put it into my main program right at the end:




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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream.h>
#include <string>
#include <conio.h>

void WeeklySales(); //we make weeklysales a function here too
void yesnofunc();

main()
	{
   	int number;

      do
      	{      //displays the menu
         	cout << "******Sales System******";
            cout << ("\n\n");
      		cout << "1. Display Company Logo.\n"; endl;
      		cout << "2. Input/Validate weekly sales data.\n";endl;
      		cout << "3. Calculate weekly sales.\n";endl;
      		cout << "4. Display reciept.\n";endl;
      		cout << "5. SHUT DOWN & LOG OFF.\n";endl;
				cout << "\nEnter number...\n";endl;
         	cin >> number;
            cout << ("\n\n");

      		switch (number)  //<--the expression is a variable (number) & controls the switch
            //the value of (number) is tested against a list of constants.
            //When a match is found, the statement sequence assosciated with that match is executed
               {
      				case 1:    //<-----  const = 1
                     
            			cout << ("\n\tUU\tUU \tEEEEEEEE \tLL\t\t \SSSSSSS\n\tUU\tUU \tEE \t\tLL\t\tS\n\tUU\tUU \tEE \t\tLL\t\tS\n\tUU\tUU \tEEEEEEE \t\LL\t\t\ SSSSSS\n\tUU\tUU \tEE \t\tLL\t\t       S\n\tUU\tUU  \tEE  \t  \t\LL  \t\t       S \n\t UUUUUUUU   *\tEEEEEEEE  *\tLLLLLLL  *\tSSSSSSS   *\n\n\n\n\n");break;

            		case 2:   //<------const = 2. if match found, executes, then calls the weeklysales function below

            			cout << ("Weekly sales data\n\----------------- ");
                                WeeklySales();  //<-------------   function call here
                                break;

            		case 3:

            			cout << ("Calculate weekly sales "); break;

            		case 4:

            			cout << ("Display receipt "); break;

            		case 5:

            			cout << ("Goodbye, and thank you for using U.E.L.S. ");break;

                  //default statement sequence is executed if no matches are found
            		default:

            			cout << ("Enter a number from 1-5 only!");

         		}
      	} while (number !=5); //program will NOT stop looping till 5 is entered
      getch();
   }

void yesnofunc()
{
	char answer;

do {
		cout << "\nIs this correct ?\n";
   	cin >> answer;

   	switch (answer) {
      	case 'y':
         	cout << "\nThank you!!\n";
            break;

      }
	}while (answer != 'y');
}

void WeeklySales() //<--------weekly sales function not part of main() function
{                  //when function is completed, goes back to case 3:
//declare variables
	int salescode, bikeprice, modelcode, quantity;
   string date;
   char answer; //either yes or no
   bool check;

   do
        //the instructions to enter the i.d. code will be repeated (do...while
        //loop) as long as the salescode is less than 1 or greater than 20.
   	{

      	cout << "\n\nPlease enter your identifiction code ";
         cin >> salescode;

         //the condition is tested
         if (salescode < 1 || salescode > 20)

         	{
            	cout << ("\nTHIS I.D. CODE IS NOT VALID. TRY AGAIN \n");
            }
      }
	//the instructions to enter the i.d. code will be repeated (do...while
   //loop) as long as the salescode is less than 1 or greater than 20.
   while (1 > salescode || salescode > 20);

   //another do...while loop
   do
   	{
      	cout << "\nPlease enter the bike price (one unit = 1 Euro) ";
      	cin >> bikeprice;

         if (bikeprice < 1 || bikeprice > 500)

         	{
            	cout << ("\nINVALID PRICE. Bike prices cannot be 0 or more than 500. TRY AGAIN \n");
            }
      }

   while (bikeprice < 1 || bikeprice > 500);

   //another do...while loop
   do
   	{
      	cout << "\nPlease enter the 3 digit model code ";
      	cin >> modelcode;

         if (modelcode < 0 || modelcode > 999)

         	{
            	cout << ("\nINVALID MODEL CODE. TRY AGAIN \n");
            }
      }

   while (modelcode < 0 || modelcode > 999);

   //another do...while loop
   do
   	{
         cout << ("\nPlease enter the quantity sold ");
      	cin >> quantity;

         if (quantity < 1 || quantity > 10)

         {
         	cout << ("\nINVALID QUANTITY. TRY A NUMBER FROM 1-10 \n");
         }
      }

   while (quantity < 1 || quantity > 10);

	do {
			check = true;//check to see whether to stay in loop or not

			cout << "\nPlease enter the date in this format dd/mm/yyyy: ";
         cin >> date;

			cout << "\nIs this the correct date ?\t" << date << "\tpress y/n: \n\n";
         cin >> answer;   //gets answer from user

			switch (answer)	{
         	case 'y':
            	cout << "\n\n\nThank You!\n\n";
               break;
            }
         }while (answer != 'y');
   }
Topic archived. No new replies allowed.