# Do-while loop within a switch-case structure?

Page 1 of 1

## 4 Replies - 312 Views - Last Post: 27 October 2019 - 05:45 AMRate Topic: 2 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=417678&amp;s=657e0c199a981c263b6f397f808dd545&md5check=' + ipb.vars['secure_hash'], cur_rating: 3, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Abhi_02

Reputation: 0
• Posts: 2
• Joined: 26-October 19

# Do-while loop within a switch-case structure?

Posted 26 October 2019 - 06:40 PM

In this multiplication game, I have to generate two random numbers and multiply them. The user has to guess the right number. After the game, the user has a choice to either restart the game or quit. I know I have to use a do-while loop for this, but I don't know what to put in place of the bolded comments. Any help is much appreciated!
```#include <stdio.h>
#include <stlib.h>
#include <time.h>
#include <math.h>

int main () {

//Start do-while loop
do {
//Display rules of the game to the user.
printf("Two random numbers (1-12) will be generated and displayed. The objective of this game is to correctly guess the product of the numbers.");

//Generate two random integers between 1 and 12 and display them to the user
int i;
int n1;
int n2;
srand(time(NULL));
for(i = 1; i<=12;i++)
{
n1 = 1 + rand() % 12;
n2 = 1 + rand() % 12;
printf("The two random numbers generated are : %d and %d\n", n1,n2);
}
//Prompt the user to enter the product of the two numbers
int a;
printf("Enter the product of the two numbers: ");
scanf("%d", &a);

//Determine and display if or not the answer was right
int countCorrect;
int countIncorrect;
int product = n1*n2;
if (a == product)
{
printf("Correct response!");
countCorrect++;
}
else
{
printf("Incorrect response, the correct answer is: %d\n", product);
countIncorrect++;
}
int num;
switch (num)
{
case 1:
printf("You have chosen to play again");
[b]//What should I put here?[/b]
break;

case 2:
printf("You have chosen to see statistics");
printf("The number of correct answers are: %d\n", countCorrect);
printf("The number of incorrect answers are: %d\n", countIncorrect);
break;

case 3:
printf("You have chosen to reset statistics");
countCorrect = 0;
countIncorrect = 0;
break;

case 4:
printf("You have chosen to quit");
[b]//What should I put here?;[/b]
break;

default:
printf("Invalid number! Please enter a number from 1 to 4.");
break;
}
}
```

This post has been edited by modi123_1: 26 October 2019 - 07:12 PM
Reason for edit:: In the future please use the [code] tag button in the editor

Is This A Good Question/Topic? 0

## Replies To: Do-while loop within a switch-case structure?

### #2 modi123_1

• Suitor #2

Reputation: 15356
• Posts: 61,557
• Joined: 12-June 08

## Re: Do-while loop within a switch-case structure?

Posted 26 October 2019 - 07:13 PM

I would use a boolean variable in a while loop starting at line 18 and encompasses everything to the end. Your continue or quit would set the variable so the loop would happen again or not.

### #3 Abhi_02

Reputation: 0
• Posts: 2
• Joined: 26-October 19

## Re: Do-while loop within a switch-case structure?

Posted 26 October 2019 - 07:28 PM

modi123_1, on 26 October 2019 - 07:13 PM, said:

I would use a boolean variable in a while loop starting at line 18 and encompasses everything to the end. Your continue or quit would set the variable so the loop would happen again or not.

Hey! Thanks for the edit, I'm a new user, so sorry if the code was not readable before

Could you possibly try to explain what you are saying in code? I'm a little confused, I apologize.

### #4 modi123_1

• Suitor #2

Reputation: 15356
• Posts: 61,557
• Joined: 12-June 08

## Re: Do-while loop within a switch-case structure?

Posted 26 October 2019 - 07:44 PM

A while loop repeats what ever is in the body, correct?

```bool foo = true;

while (foo == true)
{
// bunch of code.
}
```

That 'bunch of code' repeats while foo is true. If you turn foo to false the 'bunch of code' does _NOT_ happen.

It would seem like a quick fix to have a similar set up in your code. A variable for the while loop to check on, and your SWITCH statement sets the variable. If they want to do it again, set it to true. If they want to exit then set to false.

### #5 baavgai

• Dreaming Coder

Reputation: 7500
• Posts: 15,541
• Joined: 16-October 07

## Re: Do-while loop within a switch-case structure?

Posted 27 October 2019 - 05:45 AM

Perhaps I'm missing something: if you're doing a do while, where the hell is the while? I'd prefer a simple while in almost all cases; one reason being you don't forget it at the end of you do.

I'd use functions if permissible, as they make this a lot cleaner. Your "Generate two random integers" seems horribly confused.

```int main () {
srand(time(NULL)); // this should be up here, never call it again

// these go here, and initializse them
int countCorrect = 0;
int countIncorrect = 0;

//Start do-while loop
do {
//Display rules of the game to the user.
printf("Two random numbers (1-12) will be generated and displayed. The objective of this game is to correctly guess the product of the numbers.");

//Generate two random integers between 1 and 12 and display them to the user
int i;
int n1;
int n2;
// srand(time(NULL));
for(i = 1; i<=12; i++) {
n1 = 1 + rand() % 12;
n2 = 1 + rand() % 12;
printf("The two random numbers generated are : %d and %d\n", n1,n2);
}
*/
int n1 = 1 + rand() % 12;
int n2 = 1 + rand() % 12;
printf("The two random numbers generated are : %d and %d\n", n1,n2);
// I like this int product = n1*n2;
// do it here
int correctAnswer = n1 * n2;

//Prompt the user to enter the product of the two numbers
// int a; // this is a crap name
printf("Enter the product of the two numbers: ");

//Determine and display if or not the answer was right
// these are both in the wrong place and not initialzed
// int countCorrect;
// int countIncorrect;
// int product = n1*n2; // above
// see how much easier to read?
// if (a == product) {
printf("Correct response!");
countCorrect++;
} else {
printf("Incorrect response, the correct answer is: %d\n", product);
countIncorrect++;
}
// so, um, where do you ask for this value?
int num;
switch (num) {

```

```int play(); // return 1 if they win, 0 if they loose
void showStats(int played, int correct);
void resetStats(int *played, int *correct);

int main () {
srand(time(NULL));

// we seem to play one game before we enter the menu, so
int played = 1;
int correct = play();

// a flag to tell is when we're done
int done = 0;
while(!done) {
case 1:
printf("You have chosen to play again");
played++;
correct += play();
break;

case 2:
showStats(played, correct);
break;

case 3:
resetStats(&played, &correct);
break;

case 4:
printf("You have chosen to quit");
// well, if you're using that done, above, then:
done = 1;
break;

default:
printf("Invalid number! Please enter a number from 1 to 4.");
break;
}
}
}

```

Hope this helps.