Thread: Looping questions

  1. #1
    bignood
    Join Date
    Sep 2005
    Posts
    33

    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.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    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?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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);
    }
    Last edited by SlyMaelstrom; 10-13-2005 at 07:31 PM.
    Sent from my iPadŽ

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    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.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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.
    Last edited by SlyMaelstrom; 10-13-2005 at 06:54 PM.
    Sent from my iPadŽ

  6. #6
    bignood
    Join Date
    Sep 2005
    Posts
    33
    whenever I run your program

    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)
    >Please enter "y" or "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.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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.
    Sent from my iPadŽ

  8. #8
    bignood
    Join Date
    Sep 2005
    Posts
    33
    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

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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.
    Sent from my iPadŽ

  10. #10
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    That's because the code is using %d instead of %f for outputting doubles.

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    The code will work now.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  2. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  3. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM