Newbie question in C PLEASE help!!!

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2008
Posts: 48
Reputation: atman is an unknown quantity at this point 
Solved Threads: 0
atman atman is offline Offline
Light Poster

Newbie question in C PLEASE help!!!

 
0
  #1
Oct 26th, 2008
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!!!
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,973
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 287
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Newbie question in C PLEASE help!!!

 
0
  #2
Oct 26th, 2008
  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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 48
Reputation: atman is an unknown quantity at this point 
Solved Threads: 0
atman atman is offline Offline
Light Poster

Re: Newbie question in C PLEASE help!!!

 
0
  #3
Oct 26th, 2008
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);
}
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,973
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 287
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Newbie question in C PLEASE help!!!

 
0
  #4
Oct 26th, 2008
Why do you make it so complicated?
You can do with one while!
And please : if you send code put it between CODE-tags
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 48
Reputation: atman is an unknown quantity at this point 
Solved Threads: 0
atman atman is offline Offline
Light Poster

Re: Newbie question in C PLEASE help!!!

 
0
  #5
Oct 26th, 2008
Originally Posted by ddanbe View Post
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Newbie question in C PLEASE help!!!

 
1
  #6
Oct 26th, 2008
>which CODE tags should i use?
Click here.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 15
Reputation: thanatosys is an unknown quantity at this point 
Solved Threads: 1
thanatosys thanatosys is offline Offline
Newbie Poster

Re: Newbie question in C PLEASE help!!!

 
0
  #7
Oct 26th, 2008
you should use this
  1. code = c withing brackets;
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,973
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 287
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Newbie question in C PLEASE help!!!

 
0
  #8
Oct 26th, 2008
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......
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 48
Reputation: atman is an unknown quantity at this point 
Solved Threads: 0
atman atman is offline Offline
Light Poster

Re: Newbie question in C PLEASE help!!!

 
0
  #9
Oct 26th, 2008
Originally Posted by ddanbe View Post
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!!!
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,973
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 287
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Newbie question in C PLEASE help!!!

 
0
  #10
Oct 26th, 2008
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC