![]() |
| | #1 |
| Registered User Join Date: Mar 2010
Posts: 17
| well ive only just started c programming and i thought the best place to start was hello world so i just thought i would build on that but i seem to have hit a brick wall and i wondered if anyone could help heres how far ive got so far and basically im getting stuck on if the user enters 0 i want the program to loop around back to the start but i have no clue on how to do that! ive tried a few different loops and also tried using it in the IF statements, but no give ![]() when i execute this code and enter 0 it continually repeats "x (0) is null please try again" and does not loop back, instead i have to stop the whole program, i guess it kinda like crashes thanks loads (in advance) Code: #include <stdio.h>
#include "simpleio.h"
int main()
{
int x, i;
printf("Hello World \n");
printf("Enter an integer \n");
x = getInt();
printf("Value of X = %d \n",x);
printf("x is stored at: %p\n",&x);
for(i=0;x==0;i++)
{
printf("x (%d) is null please try again\n",x); i++;
}
if (x>1 && x<10)
{
printf("x (%d) is less than 10\n",x);
}
else
{
printf("x (%d) is more than 10\n",x);
}
return 0;
}
|
| aadil7 is offline | |
| | #2 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 11,292
| Code: declare stuff
for( input = getinput(); input != 0; input = getinput() )
{
...stuff to repeat...
}
Code: declare stuff
do
{
...stuff to repeat...
input = getinput();
} while( input != 0 );
Code: declare stuff
while( input != 0 )
{
...stuff to repeat...
input = getinput();
}
Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #3 |
| Registered User Join Date: Jan 2010 Location: USA, New york
Posts: 123
| some description of the most common loops: FOR loop: a FOR loop is used when you want to go from, lets say, number one to ten. thus looping ten times, but it cant be used to see if number one is equal to zero or not. example: Code: int i;
for (i = 0; i < 10; i++)
{
printf("Hello, World!\n");
}
a common loop used for checking stuff, like if number one has been pressed or not. its also the best to use for your problem. example: Code: int input;
while(input != 0)
{
input = getInt();
}
Some final notes: you can exit a loop by the break; command. example: Code: int i;
for (i = 0; i < 10; i++)
{
printf("Hello, World!\n");
if (i == 5)
{
break;
}
}
times anymore. i hoped this helped, if it didnt, feel free to ask.
__________________ Macselent Corp Last edited by UltraKing227; 03-14-2010 at 01:39 PM. |
| UltraKing227 is offline | |
| | #4 | |
| Registered User Join Date: Mar 2010
Posts: 17
| Quote:
how do you suggest i do this then because if the user enters '0' i want the program to loop to the start so the user starts from the start | |
| aadil7 is offline | |
| | #5 |
| Registered User Join Date: Mar 2010
Posts: 17
| ok i seem to have got around that issue but i am getting another error now when i enter a number which is less than 10 it executes the if statement but also loops around i do not know how this happened :S Code: #include <stdio.h>
#include "simpleio.h"
int main()
{
int x, i;
printf("Hello World \n");
printf("Enter an integer \n");
x = getInt();
printf("Value of X = %d \n",x);
printf("x is stored at: %p\n",&x);
do
{
printf("Hello World \n");
printf("Enter an integer \n");
x = getInt();
printf("Value of X = %d \n",x);
printf("x is stored at: %p\n",&x);
}
while( x == 0 );
if (x>=1 && x<=10)
{
printf("x (%d) is less than 10\n",x);
}
else
{
printf("x (%d) is more than 10\n",x);
}
return 0;
}
|
| aadil7 is offline | |
| | #6 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 10,163
| A do-while loop always executes at least once, since the condition is not checked until the end of the loop. |
| tabstop is offline | |
| | #7 |
| Registered User Join Date: Mar 2010
Posts: 17
| |
| aadil7 is offline | |
| | #8 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 10,163
| |
| tabstop is offline | |
| | #9 |
| Registered User Join Date: Jan 2010 Location: USA, New york
Posts: 123
| or, you may do it like this: Code: if (x == 0)
{
do
{
printf("Hello World \n");
printf("Enter an integer \n");
x = getInt();
printf("Value of X = %d \n",x);
printf("x is stored at: %p\n",&x);
}
while( x == 0 );
}
otherwise, it just goes ahead. i hope this helped.
__________________ Macselent Corp |
| UltraKing227 is offline | |
| | #10 |
| CSharpener Join Date: Oct 2006
Posts: 5,556
| how is it better when just using while loop?
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. |
| vart is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| True Beginner - Pointer/Array For loops | KyussRyn | C Programming | 7 | 03-04-2009 03:53 AM |
| Same old beginner question... | Sharmz | C Programming | 15 | 08-04-2008 11:48 AM |
| Too many loops D: | F5 Tornado | C++ Programming | 6 | 12-03-2007 01:18 AM |
| Windows programming for beginner (Absolute beginner) | WDT | Windows Programming | 4 | 01-06-2004 11:21 AM |
| help with arrays and loops | jdiazj1 | C Programming | 4 | 11-24-2001 04:28 PM |