# Looping questions

• 10-13-2005
1rwhites
Looping questions
OK here is my problem

Code:

```int main(void) {   int tiles; /* tile size in inches */ printf("Enter the size of the tiles in inches > "); scanf("%d", &tiles);   char cont = 'Y';   while (cont == 'Y' || cont == 'y') {   int wid_inch;   int wid_feet;   int length_inch;   int length_feet;   int total_tiles;   char sent;   double length_total;   double width_total;   double length_tile;   double room_tile;     printf("Enter the room width (feet and inches seperated by a space): ");     scanf("%d %d", &wid_feet, &wid_inch);     printf("Enter the room length (feet and inches separated by a space): ");     scanf("%d %d", &length_feet, &length_inch);     width_total = (wid_feet * 12) + wid_inch;     room_tile = ceil(width_total / tiles);     length_total = (length_feet * 12) + length_inch;     length_tile = ceil(length_total / tiles);     total_tiles = room_tile * length_tile;     printf("The %d'%d width and %d' %d length room requires %d, %d inch tiles\n\n",             wid_feet, wid_inch, length_feet, length_inch, total_tiles, tiles);         cont = 'a';     }   return(0); }```
At the end, instead of having cont= 'a' and closing the problem... I want to ask the user if they have another room. And if they enter Y or y to ask them for the room width and room length of that room.

Then eventually I also want to add all tiles together.
• 10-13-2005
itsme86
You obviously know how to prompt for an answer and read it into a variable since you do it elsewhere in your program...what exactly are you having trouble with?
• 10-13-2005
SlyMaelstrom
Your shouldn't be declaring variables inside of a loop. It's bad practice.

... it's also bad forum practice to start two topics stating two questions on the same code. Just ask another question in the same topic.

Here is your code with a prompt added. I also changed a few logical errors.

Code:

```int main(void) {   int tiles; /* tile size in inches */   char cont;     int wid_inch;   int wid_feet;   int length_inch;   int length_feet;   int total_tiles;   char sent;   double length_total;   double width_total;   double length_tile;   double room_tile; printf("Enter the size of the tiles in inches > "); scanf("%d", &tiles);     do {     printf("Enter the room width (feet and inches seperated by a space): ");     scanf("%d %d", &wid_feet, &wid_inch);     printf("Enter the room length (feet and inches separated by a space): ");     scanf("%d %d", &length_feet, &length_inch);     width_total = (wid_feet * 12) + wid_inch;     room_tile = ceil(width_total / tiles);     length_total = (length_feet * 12) + length_inch;     length_tile = ceil(length_total / tiles);     total_tiles = room_tile * length_tile;     printf("The %d'%d width and %d' %d length room requires %d, %d inch tiles\n\n",             wid_feet, wid_inch, length_feet, length_inch, total_tiles, tiles);     do {       getchar();    // Eats the \n in the stream. Sorry I forgot that. :)       printf("Do you have another room you wish to enter? (y/n) \n >");       scanf("%c", &cont);             if ( cont != 'Y' && cont != 'y' && cont != 'N' && cont != 'n' )           printf("Please enter \"y\" or \"n\". \n");                 } while ( cont != 'Y' && cont != 'y' && cont != 'N' && cont != 'n' );               } while (cont == 'Y' || cont == 'y');   return(0); }```
• 10-13-2005
cwr
Quote:

Originally Posted by SlyMaelstrom
Your shouldn't be declaring variables inside of a loop. It's bad practice.

Why? If they are only used inside the loop, then it is good practice, it limits their scope.

Also, your code neglects to eat the newline from stdin after prompting.
• 10-13-2005
SlyMaelstrom
Quote:

Originally Posted by cwr
Why? If they are only used inside the loop, then it is good practice, it limits their scope.

Also, your code neglects to eat the newline from stdin after prompting.

Yes, but this isn't the case. The code is meant to enter the loop no matter what, so there is no reason to declare in the loop.

...and I changed the code to eat the \n. Thanks, I forgot to do that.
• 10-13-2005
1rwhites

Enter the size of the tiles in inches > 12
Enter the room width (feet and inches seperated by a space): 17 4
Enter the room length (feet and inches separated by a space): 9 3
The 17'4 width and 9' 3 length room requires 1080459264, 0 inch tiles

Do you have another room you wish to enter? (y/n)
Do you have another room you wish to enter? (y/n)
>y
Enter the room width (feet and inches seperated by a space): 12 4
Enter the room length (feet and inches separated by a space): 4 4
The 12'4 width and 4' 4 length room requires 1079001088, 0 inch tiles

It asks if I have another room twice.
• 10-13-2005
SlyMaelstrom
Your right, it does. That's because I didn't eat the newline character like the guy mentioned, and it's accepting that into the input. I'll fix that for you. Check the code in my last post in a few minutes.
• 10-13-2005
1rwhites
OK. it asks for the next room correctly. But the calculations are messed up. Instead of getting 180, 12 inch tiles like it used to. It gets 1080459264, 0 inch tiles
• 10-13-2005
SlyMaelstrom
That's because you wrote this...

Code:

```printf("The %d'%d width and %d' %d length room requires %d, %d inch tiles\n\n",             wid_feet, wid_inch, length_feet, length_inch, total_tiles, tiles);```
...incorrectly.

Think about what you're asking it to print, then you'll realize why it's outputting that. You didn't ask me to change your calculations and string constants, you just asked how you should add a prompt.
• 10-13-2005
cwr
That's because the code is using %d instead of %f for outputting doubles.
• 10-13-2005
SlyMaelstrom
The code will work now.