Thread: My loops not working and dont know what to do

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    9

    Unhappy My loops not working and dont know what to do

    Basically this is my program, a person enters an integer which is the height of a half pyramid. It then generates the pyramid using spaces and hashes, it looks something like this

    My loops not working and dont know what to do-picture1-png

    so far my program looks something like this:

    Code:
    #include <cs50.h>#include <stdio.h>
    
    
    int main(void)
    
    
    {
    
    
    int height = GetInt();
    int integer = 1;
    //int spaces = height - integer;
    int integer2 = 2;
    int numberOfHashes = height - (height - integer2);
    int loopcount = 0;
    
    
    while (loopcount < height)
    {
        while (integer < height)
        {
            printf(" ");
            integer++;
        }
        
        while (numberOfHashes > 0)
        {
            printf("#");
            numberOfHashes--;
        }
        printf("\n");
        loopcount++;
    }
    return 0;
    }

    i dont get why it doesn't work. Someone please help. I would also like to add that i am totally new to programming and am still going over the basics, this is just a training program.

    Thanks
    Last edited by ziggy786; 12-31-2012 at 02:33 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    How does it not work?

    In what way is the output wrong?

    What output is expected? What really happens?

    Edit: The thing I wonder about is how the values of "integer" and "numberOfHashes" are reset after the first time thru the while loops.

    Decided my edit 2 was maybe to harsh.

    Tim S.
    Last edited by stahta01; 12-31-2012 at 03:24 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    9
    when i execute the file, enter an integer and it makes a pyramid with the height of the number entered. It just does the first line of the pyramid, 7 spaces and 2 hashes, its supposed to show a whole half pyramid.

    like this
    My loops not working and dont know what to do-picture1-png
    it only shows the first line of hashes

    Edit: So what should i do?

    Edit2: Is there an equivalent of GetInt() that is not in that library?
    Last edited by ziggy786; 12-31-2012 at 03:32 PM.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    How do you reset the values of "integer" and "numberOfHashes" inside the while (loopcount < height) loop?

    Hint: You are NOT resetting the values.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    9
    how do i do that, sorry for acting like such a newbie, i am one

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    numberOfHashes = value; /* where you pick the value */

    My reformatted version of your code.

    Output is not right; but, is closer in my view to correct.
    Code:
    6
          ###
          ###
          ###
          ###
          ###
          ###
    Code:
    #include <stdio.h>
    
    #include "cs50.h"
    
    int main(void)
    {
        int height = GetInt();
        int integer = 1;
        //int spaces = height - integer;
        int integer2 = 2;
        int numberOfHashes = height - (height - integer2);
        int loopcount = 0;
    
        while (loopcount < height)
        {
            integer = 0;
            while (integer < height)
            {
                printf(" ");
                integer++;
            }
    
            numberOfHashes = 3;
            while (numberOfHashes > 0)
            {
                printf("#");
                numberOfHashes--;
            }
            printf("\n");
            loopcount++;
        }
        return 0;
    }
    Last edited by stahta01; 12-31-2012 at 03:40 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    9
    where do i add that?

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by ziggy786 View Post
    where do i add that?
    Do NOT plan on being a professional programmer.

    Edit: I am now adding you to my ignore list; you do not seem to have been smart enough to write the original code.

    Tim S.

    PS: I edited my prior post.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting for loops to while loops
    By nabhatt in forum C Programming
    Replies: 3
    Last Post: 02-16-2012, 09:45 PM
  2. Replies: 3
    Last Post: 06-01-2011, 04:19 PM
  3. why scanf() dont working?
    By RAJJUMOTE in forum C Programming
    Replies: 10
    Last Post: 12-16-2007, 10:29 PM
  4. I dont get this working on gcc it works fine in windows
    By wise_ron in forum C Programming
    Replies: 8
    Last Post: 05-08-2006, 05:33 AM
  5. if you dont like linux threads, dont read...
    By ... in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 02-03-2003, 11:26 AM