Author Topic: hello....  (Read 4560 times)

sasha

  • User
  • *
  • Posts: 4
hello....
« on: April 17, 2009, 09:23:34 AM »
i've a little prob wid my grocery store c++ program....i'm able to calculate da bill for my rpogram....only if the user buyz da itemz displayed in an order....but gives garbage value....if the items r bought at random....wat may b da mistake dat i made.....?

Gillius

  • Administrator
  • User
  • *****
  • Posts: 147
    • http://www.gillius.org/
Re: hello....
« Reply #1 on: April 17, 2009, 02:29:16 PM »
There really isn't enough info from this post to say what the errors might be in your code. How are you storing the list? How does the user select them? Where do the users selections go? And how do you calculate the bill from that?
Gillius
Gillius's Programming http://www.gillius.org/

sasha

  • User
  • *
  • Posts: 4
Re: hello....
« Reply #2 on: April 19, 2009, 05:13:09 AM »
i jus display ten items....i ask da user to enter their choice....
and how much of da item dey wanna buy....
den i ask dem if dey wanna buy more...if no ...den i calculate their bill... i jus used simple if-else statements...and label....should i post my program....?

Gillius

  • Administrator
  • User
  • *****
  • Posts: 147
    • http://www.gillius.org/
Re: hello....
« Reply #3 on: April 19, 2009, 12:05:57 PM »
I'm assuming that you are keeping their choices in a vector/array/list and then adding them up? You would need more than simple if/else to calculate that; you would need a loop as well. But, if you are looping and using an array make sure you aren't adding up uninitialized portions of that array or you wold get a "garbage value".

You can post the relevant portion of the code, if it is reasonably sized.
Gillius
Gillius's Programming http://www.gillius.org/

Gillius

  • Administrator
  • User
  • *****
  • Posts: 147
    • http://www.gillius.org/
Re: hello....
« Reply #4 on: April 19, 2009, 12:16:24 PM »
Sasha, please keep this topic in the same thread. Below is the program you attached. I'm deleting the other 2 topics.

Code: [Select]
#include<iostream.h>
#include<conio.h>
float b=1000;
void main()
{
clrscr();
float x,b,c,amount1,amount2,amount3,p,s,j;
float d,a,o,q,r,t,u,v,w,g,h;
int  m;
char choice;
char l,Y,y,N,n;
cout<<"\nwelcome 2 my grocery store!!!!";
cout<<"\nwould u like 2 see the items available for purchase(y/n)?";
cin>>choice;
if(choice=='Y'||choice=='y')
{
cout<<"the items available are:";
cout<<"\nno."<<'\t'<<"item         "<<'\t'<<"quantity(b)(in grams)"<<'\t'<<"m.r.p(c)";
cout<<"\n---"<<'\t'<<"----         "<<'\t'<<"---------------------"<<'\t'<<"--------";
cout<<"\n1. "<<'\t'<<"urad dal     "<<'\t'<<"1000g                "<<'\t'<<"Rs.50.25";
cout<<"\n2. "<<'\t'<<"tur  dal     "<<'\t'<<"1000g                "<<'\t'<<"Rs.60.75";
cout<<"\n3. "<<'\t'<<"tea powder   "<<'\t'<<"1000g                "<<'\t'<<"Rs.200.0";
cout<<"\nwould you like 2 purchase any item?(y/n)";
cin>>choice;
if(choice=='Y'||choice=='y')
{
cout<<"\nyou can purchase only two items( kindly press 'n' after purchasing two items";
m:
cout<<"\nenter the no:of the item chosen by u";
cin>>m;
if(m==1)
{
cout<<"\nenter the amount of the item  u want to purchase";
cin>>x;
amount1=(x*50.25)/1000.00;
cout<<"\nthe amount to be paid is:"<<amount1<<"\n";
}
else if(m==2)
{
cout<<"\nenter the amount of item ";
cin>>x;
amount2=(x*60.75)/1000.00;
cout<<"\nthe amount 2 b paid is:"<<amount2<<"\n";
}
else if(m==3)
{
cout<<"\nenter the amount of item";
cin>>x;
amount3=(x*200.00)/1000.00;
cout<<"\nthe amount 2 be paid is:"<<amount3<<"\n";
}
cout<<"\ndo u want 2 buy another item?";
cin>>l;
if(l=='y'||l=='Y')
{
goto m;
}
else{
cout<<"\nplease wait.... ur bill is being calculated....";
}
cout<<"\nenter da no: of the  items u've bought in an order(enter 0 for the second item if u've bought only one item:";
cin>>s>>j;
if(s==1&&j==0)
{
o=amount1;
cout<<"\nthe total bill is:"<<o;
}
else if(s==1&&j==1)
{
p=amount1+amount1;
cout<<"\nthe total bill is:"<<p;
}
else if(s==1&&j==2)
{
q=amount1+amount2;
cout<<"\nthe total bill is:"<<q;
}
else if(s==1&&j==3)
{
r=amount1+amount3;
cout<<"\nthe total bill is:"<<r;
}
else if(s==2&&j==0)
{
a=amount2;
cout<<"\nthe total bill is:"<<a;
}
else if(s==2&&j==1)
{
t=amount2+amount1;
cout<<"\nthe total bill is:"<<t;
}
else if(s==2&&j==2)
{
u=amount2+amount2;
cout<<"\nthe total bill is:"<<u;
}
else if(s==2&&j==3)
{
v=amount2+amount3;
cout<<"\nthe total bill is:"<<v;
}
else if(s==3&&j==0)
{
w=amount3;
cout<<"\nthe total bill is:"<<w;
}
else if(s==3&&j==1)
{
g=amount3+amount1;
cout<<"\nthe total bill is:"<<g;
}
else if(s==3&&j==2)
{
h=amount3+amount2;
cout<<"\nthe total bill is:"<<h;
}
else if(s==3&&j==3)
{
d=amount3+amount3;
cout<<"\nthe total bill is:"<<d;
}

cout<<"\nplease pay the requested amount";
cout<<"\nthank you.please visit again";
}
else if(choice=='N'||choice=='n')
{
cout<<"\nthanks for visiting my store !!!";
}
}
else if(choice=='N'||choice=='n')
{
cout<<"\nthank you.kindly try purchasing the next time u visit the store!!!!!";
}
getch();
}
Gillius
Gillius's Programming http://www.gillius.org/

Gillius

  • Administrator
  • User
  • *****
  • Posts: 147
    • http://www.gillius.org/
Re: hello....
« Reply #5 on: April 19, 2009, 12:26:12 PM »
It looks like to me the program would work as long as you purchased only 1 or two items (the program doesn't try to stop you from purchasing more), and as long as you don't purchase the same item twice. If you purchase any item more than once the old tally for that item is overwritten.

This is definitely not the way you would write a program like this, though. You really need to use a do or while loop instead of a goto, and you should keep the customer's choices in an array, vector, or list. These are things you should learn about next to do this program properly.
Gillius
Gillius's Programming http://www.gillius.org/