954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Newbie question in C PLEASE help!!!

Hello, Im taking C in school, and i have a problem with my code, nothing happens at all when i run the program.
The task is:

A program is required that prompts the user for a number. The program will then print a series of asterisks to represent the number. If the user enters a number less than 1, the program stops. For example: 

Enter a number: 5

*****

Enter a number: 3

***

Enter a number: 9

*********

Enter a number: 0



i have decided to use while loop since its a pretest loop
here it is


#include
main()
{
int num, i;

while(num>1)
{
printf("Enter a number:");
scanf("%d", &num);

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


any help would be apreciated!!!

atman
Junior Poster in Training
50 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 
main()
{
int num, i;

while(num>1) <-----
{


What do you think the value of num is where I put the arrow?
Most likely it will be zero but you can never be certain about that.
Put your scanf function before this while, that way you give num a value.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

here comes the latest version, but when i enter 1 it terminates as well as when i enter 0, it should terminate only at 0


#include
main()
{
int num, i;

do
{
printf("\n");
printf("Enter a number:");
scanf("%d", &num);

i=1;
while(i<=num)
{
printf("*");
i++;
}
}while(num>1);
}

atman
Junior Poster in Training
50 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

Why do you make it so complicated?
You can do with one while!
And please : if you send code put it between CODE-tags

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 
Why do you make it so complicated? You can do with one while! And please : if you send code put it between CODE-tags


im really sorry, im new to this website,

which CODE tags should i use?

could you please give me a hint with doing it just with while, i tried to do it like you suggested in your first reply but got infinite loop

thanx a lot.,
sorry again

atman
Junior Poster in Training
50 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

>which CODE tags should i use?
Click here.

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

you should use this

code = c withing brackets;
thanatosys
Newbie Poster
15 posts since Oct 2008
Reputation Points: 10
Solved Threads: 1
 

Code tags :
Select your code.
Then click one of the buttons on top of the field where your typing or pasting text.
Hover over all of them to get their meaning. You need the #-button.

Code algorithm :
Get num
While num not=0 print *
Decrement num ----> this is essential otherwise you will never get out of the while loop! And you will loop forever and ever and ever and......

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

Code tags : Select your code. Then click one of the buttons on top of the field where your typing or pasting text. Hover over all of them to get their meaning. You need the #-button.

Code algorithm : Get num While num not=0 print * Decrement num ----> this is essential otherwise you will never get out of the while loop! And you will loop forever and ever and ever and......

Wow, Thank you sooo much, it works great!!
I have just started CPD Computer Programming course, and its a part from our first assignment,
thanx a lot!!!
cheers!!!

atman
Junior Poster in Training
50 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

The trills you get when some code is working can not be described by someone who has never written some code...
Mark thisone as solved...

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

#include
main()
{
int nu,i;
printf("enter the number");
scanf("%d",ν);
for(i=0;i<=nu;i++)
{
printf("* \t");
}
printf("\n");
}
~

yuvaraj.tr
Newbie Poster
21 posts since Oct 2008
Reputation Points: 7
Solved Threads: 2
 

#include
main()
{
int nu,i;
printf("enter the number");
scanf("%d",ν);
for(i=0;i<=nu;i++)
{
printf("* \t");
}
printf("\n");
}
~

yuvaraj.tr
Newbie Poster
21 posts since Oct 2008
Reputation Points: 7
Solved Threads: 2
 
#include main() { int nu,i; printf("enter the number"); scanf("%d",ν); for(i=0;i<=nu;i++) { printf("* \t"); } printf("\n"); } ~


What's this? You feel inspired today? Read the rules of the forum. Here

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You