### Problem in for loop condition using sizeof()

hey i made a program that calculates bills

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265`` ``````class television //TELEVISION CLASS { protected: char allTVs[5][15]={"Samsung" , "Sony", "LG", "Panasonic", "Onida"}; float priceTVs[5]={75000.00,80000.00,120000.00,50000.00,35000.00}; public: void viewTVs() //VIEWS ALL TVS { for(int i=0; i<5; i++) cout<<"\n"<>category; switch(category) { case 0: check=1; break; case 1: //LAPTOPS system("cls"); viewLaptops(); while(productID!=0) { cout<<"\n Enter product number (0 to quit): "; L: cin>>productID; if(productID==0) break; else if(productID>5 || productID<1) { cout<<"\n No such product please try again :"; goto L; } else { checkLap=1; totalPrice+=priceLaptops[productID-1]; cout<<"\n \t \t Price :"<>productID; if(productID==0) break; else if(productID>5 || productID<1) { cout<<"\n No such product please try again :"; goto M; } else { checkMob=1; totalPrice+=priceMobiles[productID-1]; cout<<"\n \t \t Price :"<>productID; if(productID==0) break; else if(productID>5 || productID<1) { cout<<"\n No such product please try again :"; goto T; } else { checkTV=1; totalPrice+=priceTVs[productID-1]; cout<<"\n \t \t Price :"<>bObj[bill].fname; cout<<"\n Enter last name of purchaser :"; cin>>bObj[bill].lname; bObj[bill].cost=totalPrice; bObj[bill].billNo=(rand()%99999)+10000; ++bill; } void viewID() { if(bill>0) { tempBill=bill; for(bill=0; bill

There is more to the code but my question is regarding printBill(). After the TV check their is a for loop. Before i had a problem that the program hanged during that part when the for loop was
 `` `` ``for(z=0;z<=sizeof(tv)/2;z++)``

it was semantically the same for laptops and mobiles but they worked fine
So whats wrong with this loop
Last edited on
(z=0;tv[z]<=5;z++) should be (z = 0; z <= 5; z++)
If there wouldn't any entries with value larger that 5 in array, you will get infinite loop and duffer overdlow and crash.
but it works perfectly fine
its not supposed to take any value greater than 5, that is made sure by the enterItems()
its when i used
for(z=0;z<=sizeof(tv)/2;z++)
it crashed just for the tv case. But if i only entered laptops and mobiles it worked perfectly fine even though they have the same semantical condition.
by the way it prints wierd characters as well (like a smiley face xD)
`for(z=0;z<=sizeof(tv)/2;z++)`
will crash because most likely sizeof(int) == 4, i.e. out of bounds

write it like so:
`for(z=0;z<=sizeof(tv)/sizeof(*tv);z++)`

All the loop like
`for(z=0;tv[z]<=5;z++)`
have the same problem: if you enter a valid product id at position 0, what is the value at position 1?
im sorry im kind of new in c++. what did you mean by position 0 and 1?
 but it works perfectly fine its not supposed to take any value greater than 5, that is made sure by the enterItems()

If tv[z] never takes any number greater than 5, then your loop will never exit.

What will actually happen is that, once z reaches 10, it starts reading values from memory beyond the end of the array. In other words, it's reading values that are effectively random - you can't be sure what the compiler has decided to use that memory for. Eventually, one of these values will be higher than 5 (maybe even the first of them), and the loop will exit. But it's unpredictable, and presumably, it's not the way you actually intended it to behave.

What are you actually trying to achieve with that loop?
 im sorry im kind of new in c++. what did you mean by position 0 and 1?
postion 0: tv[0] etc.

You set all fields of tv, lap, etc. to -1 which is <= 5, hence
`for(z=0;tv[z]<=5;z++)`
will always lead to z (and the other) is going out of bounds

you should write this
`for(z=0;(tv[z]>0) && (tv[z]<=5);z++)`
and take care the there's always one field left (or additionally limit z to sizeof(tv)/sizeof(*tv))
ok understood thanks coder777

@MikeyBoy i was using the array tv[] to store the values that the user entered in the TVCHECK part in the function enterItem() so that they can be used to later to print out the items that the user bought. After the bill is printed, setDefault() changes all the variables back to 0 so that the variables can be used in the next instance (its a menu program).
OK... but I don't understand why you're comparing your loop counter variable to the values stored in tv[] to check whether to continue looping or not. How many times did you intend it to loop? When did you intend it to finish looping?
i inteded it to finish looping after all the values that the user entered. For example: if the user enters the following values
1
2
3
4
5
0 //0 does not get sent to tv[]
so therefore tv[10]={1,2,3,4,5,0,0,0,0,0} //Maybe i got the zero's wrong but you get the idea

So after 5 i want the loop to stop so that it doesnt perform it again unnecessarily .
But what you're actually doing is looping until you happen to find a number in memory that's greater than 5. That can only happen once you go past the end of your array (because you initialise all members of the array to be -1, and you only allow numbers from 1 - 5 to be entered), in which case you're in the territory of stuff you can't predict. What if the first number after the end of the array happens to be a 1? What if it happens to be -10,000?

Also, why is your option class inheriting from laptops, mobiles and television? That doesn't seem to be intuitive to me. Inheritence models an "is-a" relationship. Is it true that an "option" is a television, and is a laptops, and is a mobiles, all at the same time? Multiple inheritence is rather a, well, brave thing to attempt, unless you're absolutely sure you know what you're doing.
Last edited on
@MikeyBoy yes now i know my condition is wrong.

Yes you are right option is not always a laptop, mobile and tv but it some times is. The program allows you to buy any of the items(in any category i.e. laptop, mobile and tv) for a single bill. Im not sure if that helps you to understand why i used multiple inheritance.

Also, could you explain why multiple inheritance is a brave thing to do?
Topic archived. No new replies allowed.