|
| ||||||||||
| Tags: c plus plus, do while, loop, output, programming language, while statement |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| Output not looping in C++
Code: #include <stdio.h>
#include <conio.h>
int main ()
{
int numb;
double chart;
do
{
printf ("........");
printf ("\something 1 numb = 100");
printf ("\something 2-4 numb = 70");
printf ("\something 5 numb.... = 50\n");
scanf ("%d", &numb);
if(numb==1)
{
chart = 100;
printf ("chart%lf", chart);
}
else if((numb<=2)&&(numb>5))
{
chart = 100+(numb-1)*70;
printf("chart : %lf", chart);
}
else if(numb>=5)
{
chart = 100+(numb-3)*70+(numb-4)*50;
printf ("chart : %lf", chart);
}
else
break;
}
while (numb<1);
getch();
return 0;
} |
|
#2
| |||
| |||
| Re: Output not looping in C++
On your code the while statement statement loop for one time because you have written code like that. You cant have a value less than or equal to 2 and on the same way greater than 5. Your getting this issue because your while statement not meet the necessary condition and so therefore loop not working on output. |
|
#3
| |||
| |||
| Re: Output not looping in C++
i agree with the above reply because below two codes are contradictory: Code: while(numb < 1) and else break |
|
#4
| |||
| |||
| Re: Output not looping in C++
According to me you should have below code: Code: else if((numb>=2)&&(numb<5)) |
|
#5
| ||||
| ||||
| Re: Output not looping in C++
I know above code will solve your issue but for alternative here is another structure for your code and here you don't need to use the do while statement: Code: while(1)
{
if(...)
{
}
else if(...)
{
}
else if(...)
{
}
else
break;
} |
|
#6
| |||
| |||
| Re: Output not looping in C++
Everyone thanks for the solution but by looking at above code i just need to ask why we are giving the number 1 on while, can anyone explain me. Here "while(1)" |
|
#7
| |||
| |||
| Re: Output not looping in C++
If we don't provide some way of getting out of the loop (like break, goto) , the loop will go on forever. The condition tested in a while loop can be any valid C expression. As long as that condition remains true, the while loop will continue. Since 1 is always true, the loop will never end, unless a break statement is reached. Not that while(1) is nothing but an infinite loop which we 'break'. You can create a loop that will never end by using the number 1 for the condition to be tested. |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "Output not looping in C++" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Stuck with bar looping in vista 32 bit | Ajmil | Windows Software | 5 | 26-06-2011 10:01 PM |
| remote keypress looping endlessly Harmony One | Erakna | Hardware Peripherals | 4 | 04-04-2011 11:57 PM |
| Continuous Looping In iPad Video | Depo | Portable Devices | 5 | 25-12-2010 09:19 PM |
| How to use Looping in C#? | DANIEL 602 | Software Development | 4 | 09-02-2010 05:47 AM |
| Mozilla goes looping the same Page | Zadora | Technology & Internet | 9 | 26-03-2009 10:09 AM |