dbrad dbrad - 4 months ago 7
C Question

printing specific variable in c a specific way

After getting all my header files properly "linked" I finally reached the point of being able to compile my code correctly. When I did mariov1.c (code shown below) I used a test input of 20, the result was a printout of 3 hashmarks then the display of my cmd line like this

###[xvp@localhost ~]$


now It was supposed to have an output of 21 spaces and 2 hashmarks adding a hashmark and subtracting a space each row until the number of hashmarks = usrHeight (essentially it is not actually setup to be determined by the number of hashes but it still works that way).Should have looked something like this.

How High?
10
constructing...
##
###
####
#####
######
#######
########
#########
##########
###########


This would keep it right aligned.

mariov1.c source

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

int main(void)
{
int usrHeight = 0;
int levelCounter = 0;
int paddIt = usrHeight - 1;
int hashCounter = 2;


do
{
printf("How high?\n");
int usrHeight = GetInt();
}
while ( usrHeight > 23 || usrHeight < 0);
if ( usrHeight >= 0 && usrHeight <= 23);
{
printf("constructing...\n");

}
for (levelCounter = 0; levelCounter <= usrHeight; levelCounter++)
{
printf("%.*s\n", paddIt, "");
for (int i = 0; i <= hashCounter; i++)
putchar('#');
paddIt = paddIt - 1;
hashCounter = hashCounter + 1;
}

}


I made a second attempt after this blunder using another do while loop with a for loop in the middle of it (pretty sure i did it wrong) but it just filled the terminal with hashes continously so I'm not sure where to go at this point.

Here's mariov2.c source



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

int main(void)
{
int usrHeight = 0;
int levelCounter = 0;
int paddIt = usrHeight - 1;
int hashCounter = 2;


do
{
printf("How high?\n");
int usrHeight = GetInt();
}
while ( usrHeight > 23 || usrHeight < 0);
if ( usrHeight >= 0 && usrHeight <= 23);
{
printf("constructing...\n");

}
//doing a test to see if this do while loop will handle this appropriatelly
do
{
printf("%.*s\n", paddIt, "");
for (int i = 0; i <= hashCounter; i++)
{
putchar('#');
paddIt = paddIt - 1;
hashCounter = hashCounter + 1;
levelCounter = levelCounter + 1;
}
}
while ( levelCounter <= usrHeight );
//commented area part of original code
//for (levelCounter = 0; levelCounter <= usrHeight; levelCounter++)
//{
// printf("%.*s", paddIt, "");
// for (int i = 0; i <= hashCounter; i++)
// putchar('#');
// paddIt = paddIt - 1;
// hashCounter = hashCounter + 1;
//}

}

Answer

First thing to notice is that the usrHeight you are using in the loop is always 0 because in the segment

do 
{
    printf("How high?\n");
    int usrHeight = GetInt();
}
while ( usrHeight > 23 || usrHeight < 0);

The usrHeight is local to this while scope. Once the control is outside this scope and you use usrHeight it is actually the variable which you defined in the scope of function main. Remove int from int usrHeight = GetInt();

Also you have computed the paddIt variable before you have actually initialized it with a value using GetInt ().