0

So I understand the bases of the project, what I'm suppose to do etc, but when I prompt the user I get a bug, it also loops forever when I enter a negative integer is that normal? The do and while loop is as far as I got, for the the part where you have to make a for loop for each of the ints (height, spaces, hashes, etc) I get confused. I know that the spaces is one less than the number the user inputs and the hashes start at two and add one. I have no idea how to translate this to code! I seen some peoples work, but I want to understand each process I take to ensure that come later psets I have grasped an understanding of the basic concepts. I have also reviewed my notes and re-watched the lecture. I have also watched all the shorts and read the notes at least four times.

Here's my code:

    #include <cs50.h>
    #include <stdio.h>

    int main(void)
      {
         printf("Mario's Pymid Height: ");
         int height = get_int();
         do
         {

         } while (height <= 0 || height >= 23);
      }

1 Answer 1

0

The printf and especially the height = get_int(); should go into the do..while loop. You want to perform them at least once, and again if the entered height was not valid (0 and 23 are valid, too). Note that the int height; part (variable declaration) needs to be outside of the do..while loop, as this variable needs to be used after the loop, and variable declarations in C are block scope.

After the do..while, you'd have to write another loop (a for loop or maybe regular while loop), with one iteration per requested line. Inside, other loop(s) work well.

If you don't know how to translate into code, write in tiny bits of your native language first, preferably on paper. Write down the algorithm in really small steps (computers are stupid, they do everything you tell them, but don't understand complex problems), and follow your "code" for some example, keeping track of output and variable values. When you are convinced your "code" might be right, translate it into C.

2
  • So the int height goes on the outside of the variable I tried this before and I get a bug in my code it prints height on the same line as workspace and it doesn't prompt the user with height.
    – Alice
    Jun 27, 2017 at 0:00
  • So you have the height = get_int(); within the do..while loop?
    – Blauelf
    Jun 27, 2017 at 7:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .