A simple program , need to see where's the mistake :)

O

Odinn

Greetings , This is my first year at programming and 3rd week at this
so count me as a pure newbie.
I am trying to make a program that gets a number and prints out starts
as this:

For input 3 :
*
**
***

For input 6:
*
**
***
****
*****
******
As you see the program I am trying to make generates as much as lines
as the input number , one star increasement at each line.
This is the program I wrote:

#include <stdio.h>

star(int x)
{
int i=1;
while(i<=x)
{
printf("*");
i++;
}
}

print(int x){
int start = 1;
int end = x;
while (start <= end)
{
printf("%d \n", star(start) );
start++;
}
}


main()
{
printf("Enter value \n");
int veri;
scanf("%d", &veri);
print(veri);
}

Seems works but with a bug:
I always get a number after stars such as this:
Ex:

Enter value
8
*2
**3
***4
****5
*****6
******7
*******8
********9

Thanks in advance :)
 
M

michael.kirby

Odinn said:
Greetings , This is my first year at programming and 3rd week at this
so count me as a pure newbie.
I am trying to make a program that gets a number and prints out starts
as this:

For input 3 :
*
**
***

For input 6:
*
**
***
****
*****
******
As you see the program I am trying to make generates as much as lines
as the input number , one star increasement at each line.
This is the program I wrote:

#include <stdio.h>

star(int x)
{
int i=1;
while(i<=x)
{
printf("*");
i++;
}
}

print(int x){
int start = 1;
int end = x;
while (start <= end)
{
printf("%d \n", star(start) );
start++;
}
}


main()
{
printf("Enter value \n");
int veri;
scanf("%d", &veri);
print(veri);
}

Seems works but with a bug:
I always get a number after stars such as this:
Ex:

Enter value
8
*2
**3
***4
****5
*****6
******7
*******8
********9

Thanks in advance :)

Not sure what you were trying to accomplish by calling the star
function from within the printf. Maybe I am missing something. If you
just take that out and add the printing of the new line in your star
function it seems to output what you described as wanting:

#include <stdio.h>

star(int x)
{
int i=1;
while(i<=x)
{
printf("*");
i++;
}
printf("\n");
}

printstar(int x){
int start = 1;
int end = x;
while (start <= end)
{
star(start) ;
start++;
}
}

main()
{
printf("Enter value \n");
int veri;
scanf("%d", &veri);
printstar(veri);
}
 
C

Christopher Benson-Manica

Odinn said:
Greetings , This is my first year at programming and 3rd week at this
so count me as a pure newbie.

Well, you've tried at least, and that's a great start. Not bad at
all, but there are some things to note:
#include <stdio.h>
star(int x)

This is valid - the return type defaults to "int" if you don't supply
one explicitly - but it isn't a great idea, and it also isn't
supported by the latest C language standard. Given that you don't
return anything, perhaps "void" would be appropriate here.
{
int i=1;
while(i<=x)
{
printf("*");
i++;
}
}
print(int x){
int start = 1;
int end = x;
while (start <= end)
{
printf("%d \n", star(start) );

Here's where those numbers you're seeing come in. That %d in printf()
tells it to look for a number, which is what star(start) returns.
Since you didn't actually return anything, though, the number that
gets printed can't be predicted. Watch out for that.

star(start);
printf( "\n" );

will work quite nicely without printing that number.
start++;
}
}

main()

This would be better as

int main(void)

....
{
printf("Enter value \n");
int veri;
scanf("%d", &veri);
print(veri);

....which means you'll want to return something here. 0 means success,
so

return 0;

Probably better than my first C program...
 
O

Odinn

Thanks alot for the help Mr Christopher , aswell as the help. Really
appreciated.
Uluc Aydin
Christopher Benson-Manica yazdi:
 
A

Andrew Poelstra

Greetings , This is my first year at programming and 3rd week at this
so count me as a pure newbie.

No problem. Remember to format your code correctly when posting it
online. (Use a 2 or 4-space tab, and no // comments.) I've corrected
your spacing below as an example for you.

This is the program I wrote:

#include <stdio.h>

star(int x)

Don't rely on implicit int. It is invalid in C99, and therefore limits
your future-proofness.
{
int i=1;
while(i <= x)

Start at 0 and use < instead of <=. It seems counter-intuitive at first,
but it's how computers tend to work.
{
printf("*");
i++;
}

Hey hey hey! This function returns int. You need to return something
from it, or change the return type to void.
}

print(int x){

Be consistent in your bracing style. While you're learning, it's okay to
experiment, but keep to one style per source file. (And always follow
in-house coding standards.)
int start = 1;
int end = x;
while (start <= end)

int start = 0;
while (start < x)
{
printf("%d \n", star(start) );

Here's your problem. %d prints an integer. To do what you want, use:
star(start);
putchar("\n");
start++;
}
}


main()

int main(void)

See how much more explicit that is?
{
printf("Enter value \n");
int veri;

Mixed declaration and code are prohibited in C90, and so for decent
portability you should not do this. (What I mean is, you can't have
"int n" after a printf() statement.)
scanf("%d", &veri);
print(veri);

What is this print()? Where is it defined?
Also, you need to return something from main():
return 0;

Hope that helps. :)
 
C

CBFalconer

Odinn said:
I am trying to make a program that gets a number and prints out
starts as this:

For input 3 :
*
**
***

For input 6:
*
**
***
****
*****
******
As you see the program I am trying to make generates as much as
lines as the input number , one star increasement at each line.
This is the program I wrote:

#include <stdio.h>

star(int x)
{
int i=1;
while(i<=x)
{
printf("*");
i++;
}
}

print(int x){
int start = 1;
int end = x;
while (start <= end)
{
printf("%d \n", star(start) );
start++;
}
}

main()
{
printf("Enter value \n");
int veri;
scanf("%d", &veri);
print(veri);
}

Seems works but with a bug: I always get a number after stars
such as this: Ex:

Enter value
8
*2
**3
***4
****5
*****6
******7
*******8
********9

Thanks in advance :)

Not bad for a beginning. You show promise. With gcc set to accept
C89 standard:

[1] c:\c\junk>cc junk.c
junk.c:4: warning: return type defaults to `int'
junk.c: In function `star':
junk.c:11: warning: control reaches end of non-void function
junk.c: At top level:
junk.c:13: warning: return type defaults to `int'
junk.c: In function `print':
junk.c:21: warning: control reaches end of non-void function
junk.c: At top level:
junk.c:25: warning: return type defaults to `int'
junk.c: In function `main':
junk.c:27: warning: ISO C89 forbids mixed declarations and code
junk.c:30: warning: control reaches end of non-void function

After reformatting (don't use tabs, use spaces) and correcting the
errors (altered or moved lines marked with /**/) it works.

#include <stdio.h>

/**/void star(int x)
{
int i = 1;

while (i <= x) {
printf("*");
i++;
}
}

/**/void print(int x)
{
int start = 1;
int end = x;

while (start <= end) {
/**/ star(start);
/**/ printf("\n");
start++;
}
}

/**/int main(void)
{
int veri;

/**/printf("Enter value \n");
scanf("%d", &veri);
print(veri);
/**/return 0;
}

Study the error messages and the changes made, and think about why
I made them.
 
A

Andrew Poelstra

Greetings , This is my first year at programming and 3rd week at this
so count me as a pure newbie.
I am trying to make a program that gets a number and prints out starts
as this:

For input 3 :
*
**
***

For input 6:
*
**
***
****
*****
******
<snip code, which I've already commented on>


Here's my version of your program:

#include <stdio.h>

void print_stars(int num) {
int i;
for(i = 0; i < num; i++)
putchar('*');
putchar('\n');
}

int main(void) {
int num, ctr;
scanf(&num);
for(ctr = 0; ctr < num; ctr++)
print_stars(ctr);
return 0;
}

Consider the following:
The function name clearly shows what it does, and the argument is
named clearly. By simply reading the prototype, one can figure out
exactly what the function does and how to use it.
I've used putchar() instead of printf() for a number of reasons. The
first is that putchar() is tailor-made for printing one character, and
therefore can be much more efficient than printf(). Another reason is
that printf() needs to scan each character and check for format strings.
putchar() eliminates this overhead. Thirdly, putchar() shows my intent
more clearly.
I moved the printing of the newline into the print_stars() function,
which is where it logically belongs.
Notice also that I formatted consistently and didn't rely on implicit
return values. On those functions that /did/ return something, I made
sure to include a return statement.

My final comment is that you shouldn't be using scanf() for user input,
as it's very unfriendly and doesn't handle erroneous input properly.
Better to write your own get_int() function using fgets() or fgetc(). I
recommend that as your next project.
 
F

fermineutron

This should do the trick:

#include<stdio.h>

int main(void){
int i,j,N;

printf("enter a number"); /*Ask for input*/
scanf("%d",&N); /*Store input*/
i=0; /*set external loop counter to 0*/
while(i++<N){ /*increment i by 1 and compare to N, if
it is less than N print i stars*/
j=0; /*set inner loop counter to 0*/
printf("\n"); /*print a new line character to print
start on a new line*/
while(j++<i) /*increment j by 1 and compare to i, if it
is less than i print a star*/
printf("*");
}

return 0;
}
 
B

Barry Schwarz

This should do the trick:

#include<stdio.h>

int main(void){
int i,j,N;

printf("enter a number"); /*Ask for input*/
scanf("%d",&N); /*Store input*/
i=0; /*set external loop counter to 0*/
while(i++<N){ /*increment i by 1 and compare to N, if
it is less than N print i stars*/
j=0; /*set inner loop counter to 0*/
printf("\n"); /*print a new line character to print
start on a new line*/
while(j++<i) /*increment j by 1 and compare to i, if it
is less than i print a star*/
printf("*");
}

return 0;
}

What have you got against for loops?


Remove del for email
 
R

Richard Heathfield

fermineutron said:
This should do the trick:

#include<stdio.h>

int main(void){
int i,j,N;

printf("enter a number"); /*Ask for input*/
scanf("%d",&N); /*Store input*/
i=0; /*set external loop counter to 0*/
while(i++<N){ /*increment i by 1 and compare to N, if
it is less than N print i stars*/
j=0; /*set inner loop counter to 0*/
printf("\n"); /*print a new line character to print
start on a new line*/
while(j++<i) /*increment j by 1 and compare to i, if it
is less than i print a star*/
printf("*");
}

return 0;
}

Some sample runs:

me@here> ./foo
enter a number1

*me@here> ./foo
enter a numberSIX
me@here> ./foo
enter a numberme@here>

For the love of Mike, if you must use the thrice-accursed scanf function,
look up the meaning of its return value, and act upon that knowledge. What
if you get input that isn't a number? What if the user signals EOF?

And think more closely about your end conditions.
 
C

Christopher Benson-Manica

Andrew Poelstra said:
int num, ctr;
scanf(&num);

What is this, my lord? Surely hell hath no fury like a terribly
incorrect scanf() call?
My final comment is that you shouldn't be using scanf() for user input,
as it's very unfriendly and doesn't handle erroneous input properly.
Better to write your own get_int() function using fgets() or fgetc(). I
recommend that as your next project.

And you helpfully provided an example of how *not* to use scanf() :)
 
F

Fred Kleinschmidt

CBFalconer said:
Odinn said:
I am trying to make a program that gets a number and prints out
starts as this:

For input 3 :
*
**
***

For input 6:
*
**
***
****
*****
******
As you see the program I am trying to make generates as much as
lines as the input number , one star increasement at each line.
This is the program I wrote:

#include <stdio.h>

star(int x)
{
int i=1;
while(i<=x)
{
printf("*");
i++;
}
}

print(int x){
int start = 1;
int end = x;
while (start <= end)
{
printf("%d \n", star(start) );
start++;
}
}

main()
{
printf("Enter value \n");
int veri;
scanf("%d", &veri);
print(veri);
}

Seems works but with a bug: I always get a number after stars
such as this: Ex:

Enter value
8
*2
**3
***4
****5
*****6
******7
*******8
********9

Thanks in advance :)

Not bad for a beginning. You show promise. With gcc set to accept
C89 standard:

[1] c:\c\junk>cc junk.c
junk.c:4: warning: return type defaults to `int'
junk.c: In function `star':
junk.c:11: warning: control reaches end of non-void function
junk.c: At top level:
junk.c:13: warning: return type defaults to `int'
junk.c: In function `print':
junk.c:21: warning: control reaches end of non-void function
junk.c: At top level:
junk.c:25: warning: return type defaults to `int'
junk.c: In function `main':
junk.c:27: warning: ISO C89 forbids mixed declarations and code
junk.c:30: warning: control reaches end of non-void function

After reformatting (don't use tabs, use spaces) and correcting the
errors (altered or moved lines marked with /**/) it works.

#include <stdio.h>

/**/void star(int x)
{
int i = 1;

while (i <= x) {
printf("*");
i++;
}
}

/**/void print(int x)
{
int start = 1;
int end = x;

while (start <= end) {
/**/ star(start);
/**/ printf("\n");
start++;
}
}

/**/int main(void)
{
int veri;

/**/printf("Enter value \n");
scanf("%d", &veri);
print(veri);
/**/return 0;
}

Study the error messages and the changes made, and think about why
I made them.

Another version (why use printf for a single character?):

#include <stdio.h>
void printstars( int n ) {
while ( n-- ) {
putc( (int)'*', stdout );
}
putc( (int)'\n', stdout );
}

int main( int argc, char *argv[] )
{
if ( argc > 1 ) {
n = atoi(argv[1]);
/* or better, use strtod and check for errors */

for ( i=1; i <= n; i++ ) {
printstars(i);
}
return 0;
}
 
K

Keith Thompson

Fred Kleinschmidt said:
#include <stdio.h>
void printstars( int n ) {
while ( n-- ) {
putc( (int)'*', stdout );
}
putc( (int)'\n', stdout );
}
[...]

What is the purpose of the casts? (Hint: none.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top