Like Tree1Likes
  • 1 Post By stahta01

My loops not working and dont know what to do

This is a discussion on My loops not working and dont know what to do within the C Programming forums, part of the General Programming Boards category; Basically this is my program, a person enters an integer which is the height of a half pyramid. It then ...

  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

    Name:  Picture1.png
Views: 206
Size:  8.8 KB

    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 01:33 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    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 02:24 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  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
    Name:  Picture1.png
Views: 57
Size:  8.8 KB
    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 02:32 PM.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    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.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  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
    2,372
    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 02:40 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

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

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    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.
    Salem likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

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, 08: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, 09: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, 10:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21