Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

why will my loop does not work

loop

  • Please log in to reply
5 replies to this topic

#1 atoivan

atoivan

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 73 posts

Posted 08 November 2011 - 12:16 PM

why is the loops not working ?

i tried Indefinite for loop (
for (;;)
but it does not stop looping so i use while loop and with the while loop it prints the condition statement which i know it is within the loop that is why it outputs it .when i get it out i can not use the if statement


with the for loop dis is the code
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include  <ncurses.h>

void create_new_stock(void);
void display_stock(void);
void sales(void);
void modify(void);
void Additem(void);
void Viewitems(void);
void Viewcategory(void); 
void Editcategory(void);
void Edititems(void);
void Edit(void);
void Deleteitem(void);
void Deletecategory(void);
void PrintBeeper(void);
void CreateFile(void);
void DeleteFile(void);
void sel();



double 	u_prx = 0.0; 			
double 	total=0.0; 				
double 	G_total=0.0; 			
double 	amut_b=0.0; 			
double 	amut_u=0.0;				
double 	S_prx_Blk=0.0;			
double 	S_prx_u=0.0;			
double 	T_amut_blk=0.0;			
double	T_amut_u =0.0;			
double  qty_sold = 0.0;			
int 	T_qty_bulk =0;			
int 	T_qty_u =0;			
int     G_T_qty_bulk=0;			
int		G_T_qty_u=0;			
int	   	qty_u= 0; 				
int    	qty_left = 0; 			
int    	qty_bulk = 0;			 

char cate[20];				
char iname[20]; 				
char code[6]; 					



void create_new_stock()
{
	int bra;
	char ans;
printf("Enter Category ");
scanf("%s",cate);
printf("How many brands do u want to add?");
scanf("%d",&bra);
for(;;)
{
printf("Enter brand name");
scanf("%s",iname);
printf("Enter Buying price(bulk)");
scanf("%lf",&amut_b);
puts("Enter Quantity in bulk");
scanf("%d",&qty_bulk);
puts("Enter Quantity of items (number of items in a box)");
scanf("%d",&qty_u);
T_qty_u=qty_bulk*qty_u;
T_amut_blk=amut_b/qty_u;
puts("Enter Selling price for(box)");
scanf("%lf",&S_prx_Blk);
puts("Enter selling price for unit");
scanf("%lf",&S_prx_u);
if (bra<=1)
puts("do u want to add more y or n");
scanf("%c",&ans);
if(tolower(ans) == 'n') 			/* look for any sign of no */
break;

}
getchar();
}

with the while loop this is the code

#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include  <ncurses.h>

void create_new_stock(void);
void display_stock(void);
void sales(void);
void modify(void);
void Additem(void);
void Viewitems(void);
void Viewcategory(void); 
void Editcategory(void);
void Edititems(void);
void Edit(void);
void Deleteitem(void);
void Deletecategory(void);
void PrintBeeper(void);
void CreateFile(void);
void DeleteFile(void);
void sel();



double 	u_prx = 0.0; 			
double 	total=0.0; 				
double 	G_total=0.0; 			
double 	amut_b=0.0; 			
double 	amut_u=0.0;				
double 	S_prx_Blk=0.0;			
double 	S_prx_u=0.0;			
double 	T_amut_blk=0.0;			
double	T_amut_u =0.0;			
double  qty_sold = 0.0;			
int 	T_qty_bulk =0;			
int 	T_qty_u =0;			
int     G_T_qty_bulk=0;			
int		G_T_qty_u=0;			
int	   	qty_u= 0; 				
int    	qty_left = 0; 			
int    	qty_bulk = 0;			 

char cate[20];				
char iname[20]; 				
char code[6]; 					

void create_new_stock()
{
	int bra;
	char ans;
	int a=1;
printf("Enter Category ");
scanf("%s",cate);
printf("How many brands do u want to add?");
scanf("%d",&bra);


while(bra>=1)
{
printf("Enter brand name");
scanf("%s",iname);
printf("Enter Buying price(bulk)");
scanf("%lf",&amut_b);
puts("Enter Quantity in bulk");
scanf("%d",&qty_bulk);
puts("Enter Quantity of items (number of items in a box)");
scanf("%d",&qty_u);
T_qty_u=qty_bulk*qty_u;
T_amut_blk=amut_b/qty_u;
puts("Enter Selling price for(box)");
scanf("%lf",&S_prx_Blk);
puts("Enter selling price for unit");
scanf("%lf",&S_prx_u);
a++;
puts("do u want to add more y or n");
scanf("%c",&ans);

if(tolower(ans) == 'n') 			/* look for any sign of no */
break;
}
getchar();
}

	

void display_stock()
{

	
}

void sales()
{

	
	}

void modify()
{

	

}

int main()
{
char sele; // seletion of data

puts(" Welcome to my sales Program");
puts("<1> \t Create a new stock ");
puts("<2> \t Display Stock");
puts("<3> \t Sales");
puts("<4> \t Modify");
puts("<5> \t exit");

puts("Enter selection");
scanf("%c",&sele);


switch (sele)
{
case '1':
create_new_stock();
break;
case '2':
display_stock();
break;
case '3':
sales();
break;
case '4':
modify();
break;
case '5':
exit(1);
break;

	}
return 0;
getchar();
		}

pls help me correct them

Edited by atoivan, 09 November 2011 - 12:41 AM.

  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 08 November 2011 - 12:30 PM

You never change bra inside your loop.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 atoivan

atoivan

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 73 posts

Posted 08 November 2011 - 02:28 PM

You never change bra inside your loop.


i do not get u pls
  • 0

#4 spkenn5

spkenn5

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 08 November 2011 - 09:19 PM

i think he means the bracket inside the loop
  • 0

#5 atoivan

atoivan

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 73 posts

Posted 09 November 2011 - 12:44 AM

{
	int bra;
	char ans;
printf("Enter Category ");
scanf("%s",cate);
printf("How many brands do u want to add?");
scanf("%d",&bra);
for(;;)
{
printf("Enter brand name");
scanf("%s",iname);
printf("Enter Buying price(bulk)");
scanf("%lf",&amut_b);
puts("Enter Quantity in bulk");
scanf("%d",&qty_bulk);
puts("Enter Quantity of items (number of items in a box)");
scanf("%d",&qty_u);
T_qty_u=qty_bulk*qty_u;
T_amut_blk=amut_b/qty_u;
puts("Enter Selling price for(box)");
scanf("%lf",&S_prx_Blk);
puts("Enter selling price for unit");
scanf("%lf",&S_prx_u);
if (bra<=1)
puts("do u want to add more y or n");
scanf("%c",&ans);
if(tolower(ans) == 'n') 			/* look for any sign of no */
break;

}


this is the loop there is no bracket inside the loop

Edited by atoivan, 09 November 2011 - 01:39 AM.

  • 0

#6 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 09 November 2011 - 06:55 AM

In your while loop, your test condition is on the value of variable bra. You never change it in the loop, however, so it will never break out.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/






Also tagged with one or more of these keywords: loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download