943,936 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1333
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 26th, 2008
0

Newbie question in C PLEASE help!!!

Expand Post »
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<stdio.h>
main()
{
int num, i;

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

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


any help would be apreciated!!!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
atman is offline Offline
50 posts
since Oct 2008
Oct 26th, 2008
0

Re: Newbie question in C PLEASE help!!!

  1. main()
  2. {
  3. int num, i;
  4.  
  5. while(num>1) <-----
  6. {

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.
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Oct 26th, 2008
0

Re: Newbie question in C PLEASE help!!!

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<stdio.h>
main()
{
int num, i;

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

i=1;
while(i<=num)
{
printf("*");
i++;
}
}while(num>1);
}
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
atman is offline Offline
50 posts
since Oct 2008
Oct 26th, 2008
0

Re: Newbie question in C PLEASE help!!!

Why do you make it so complicated?
You can do with one while!
And please : if you send code put it between CODE-tags
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Oct 26th, 2008
0

Re: Newbie question in C PLEASE help!!!

Click to Expand / Collapse  Quote originally posted by ddanbe ...
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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
atman is offline Offline
50 posts
since Oct 2008
Oct 26th, 2008
1

Re: Newbie question in C PLEASE help!!!

>which CODE tags should i use?
Click here.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Oct 26th, 2008
0

Re: Newbie question in C PLEASE help!!!

you should use this
  1. code = c withing brackets;
Reputation Points: 10
Solved Threads: 1
Newbie Poster
thanatosys is offline Offline
15 posts
since Oct 2008
Oct 26th, 2008
0

Re: Newbie question in C PLEASE help!!!

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......
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Oct 26th, 2008
0

Re: Newbie question in C PLEASE help!!!

Click to Expand / Collapse  Quote originally posted by ddanbe ...
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!!!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
atman is offline Offline
50 posts
since Oct 2008
Oct 26th, 2008
0

Re: Newbie question in C PLEASE help!!!

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...
Last edited by ddanbe; Oct 26th, 2008 at 5:40 pm.
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Infinite loop problem
Next Thread in C Forum Timeline: Transfer data to/from the Windows clipboard





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC