Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , ,

Sponsored Links


Output not looping in C++

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 19-01-2012
Member
 
Join Date: Nov 2011
Posts: 82
Output not looping in C++

Sponsored Links
Hello everyone i have create the DO while looping code but on output this not at all looping, i am always using the do while loop but still i am not able to loop at output. can anyone suggest me what is wrong with my code, here is my code:

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;
    }

Reply With Quote
  #2  
Old 19-01-2012
Member
 
Join Date: May 2009
Posts: 512
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.
Reply With Quote
  #3  
Old 19-01-2012
Member
 
Join Date: May 2009
Posts: 496
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
So i would like to suggest you to change values as said above and also change the while to while(numb > 1) and you don't need the else part if write this code.
Reply With Quote
  #4  
Old 19-01-2012
Member
 
Join Date: Mar 2010
Posts: 147
Re: Output not looping in C++

According to me you should have below code:
Code:
else if((numb>=2)&&(numb<5))
Reply With Quote
  #5  
Old 19-01-2012
Ronchi's Avatar
Member
 
Join Date: May 2009
Posts: 521
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;
    }
Reply With Quote
  #6  
Old 19-01-2012
Member
 
Join Date: Nov 2011
Posts: 82
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)"
Reply With Quote
  #7  
Old 21-01-2012
Member
 
Join Date: Jun 2011
Posts: 262
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 05:28 AM.