dynamic memory confuse

here is a thing.. i use a dynamic memory to control list of menus a user can choose. but i also want to use another dynamic memory to control how many times a user can input their choice. but whenever i output what the user choose in all the times he choose, the only output is the last choice he made. and it did output the number of times the user choose. how to fix this?
Where is the code ?
here is for the first set of dynamic memory:

prodref(){
pn= new (nothrow) string [11];

pn[1]=" Aluminum Gold Engraved ";
pn[2]="Aluminum Silver Engraved";
pn[3]=" Stainless Brass ";
pn[4]=" Stainless Steel ";
pn[5]=" Acrylic Imprint ";
pn[6]=" Acrylic Engraved ";
pn[7]=" Aluminum Mirrorized ";
pn[8]=" Impact Plastic ";
pn[9]=" Magnet Pair ";
pn[10]="US Safety Pin";


cout<<"\n\n ";
cout<<"Product Number (press 'L' for List): ";
cin>>n;

....}

here is for the repeating question:

addproduct()
{


cout<<"How many products would you like to add? ";
cin >> i;
k= new (nothrow) int[i];
if (k == 0)
cout <<"\n\n Error: Enter atleast 1!\n";
else
{
for (r=0; r<i; r++)
{
prodref();
}
}
douwntto();

return 0;
}
These both leak memory. You allocate some memory, and then you lose the pointer to it, and you lose the memory. The data in the dynamically allocated memory is lost forever.

Using dynamic memory for this makes no sense.

1
2
k= new (nothrow) int[i];
if (k == 0)

Here, k is a pointer. If the user entered zero, so i is zero, k will not be zero. I don't think you understand what new does.
Last edited on
but how come it still repeatedly asking?
all i want to do is to refer a dynamic memory to another dynamic memory... =((
Your half posted, un-formatted, 'human-mangled-function' names make this code hard to read. I'm guessing all your variables are global? Your also leaking all over the place. What is still repeating? None of your posted code shows any input as output, so we cant help you there.

Using dynamic memory for this makes no sense.
As Moschops already said, I don't even see why you are using dynamic memory anywhere, you could just as easily make your products a static array and be done, help us help you make it make sense.
Last edited on
what i want to happen is every time a user would like to order,, this will be the output:

"Product code:" (user will input a number that refers to the dynamic memory i created)
"Quantity:" (after that the user have the option to select whether to add another product or compute total)

if the user wants to add another product i will ask,,
"How many products would you like to add:"
(whatever the user answer will be the number of times that "product code:" and "Quantity:" will appear then if the user will choose "compute total",, it will output the Product name of the corresponding code then the quantity then the base price then the amount then the total of everything...
here is the code:

#include <iostream>
#include <string>
using namespace std;

print (), prodref(), addproduct(), douwntto();
string * pn;
int n, qty;
int up, amt;
char wntto;
int i, r;
int * k;

struct products
{
struct quantt
{
int belowten;
int belowfifty;
int belowonehundred;
int moreonehundred;
}price, noofdays;

}alg, als, br, st, acre, acri, imp, mirr, mag, usp;

prodref()
{
products alg, als, br, st, acre, acri, imp, mirr, mag, usp;

//Prices
alg.price.belowten=140;
alg.price.belowfifty=85;
alg.price.belowonehundred=75;
alg.price.moreonehundred=70; //aluminum gold

als.price.belowten=140;
als.price.belowfifty=85;
als.price.belowonehundred=75;
als.price.moreonehundred=70; //aluminum silver

br.price.belowten=250;
br.price.belowfifty=200;
br.price.belowonehundred=155;
br.price.moreonehundred=115; //brass

st.price.belowten=250;
st.price.belowfifty=200;
st.price.belowonehundred=155;
st.price.moreonehundred=115; //steel

acre.price.belowten=130;
acre.price.belowfifty=75;
acre.price.belowonehundred=65;
acre.price.moreonehundred=60; //engrave

acri.price.belowten=130;
acri.price.belowfifty=75;
acri.price.belowonehundred=65;
acri.price.moreonehundred=70; //imprint

imp.price.belowten=100;
imp.price.belowfifty=80;
imp.price.belowonehundred=75;
imp.price.moreonehundred=70; //imprint

mirr.price.belowten=130;
mirr.price.belowfifty=75;
mirr.price.belowonehundred=65;
mirr.price.moreonehundred=60; //mirrorized

mag.price.belowten=55;
mag.price.belowfifty=55;
mag.price.belowonehundred=50;
mag.price.moreonehundred=50; //magnet

usp.price.belowten=15;
usp.price.belowfifty=15;
usp.price.belowonehundred=10;
usp.price.moreonehundred=10; //usp


pn= new (nothrow) string [11];

pn[1]=" Aluminum Gold Engraved ";
pn[2]="Aluminum Silver Engraved";
pn[3]=" Stainless Brass ";
pn[4]=" Stainless Steel ";
pn[5]=" Acrylic Imprint ";
pn[6]=" Acrylic Engraved ";
pn[7]=" Aluminum Mirrorized ";
pn[8]=" Impact Plastic ";
pn[9]=" Magnet Pair ";
pn[10]="US Safety Pin";


cout<<"\n\n ";
cout<<"Product Number (press 'L' for List): ";
cin>>n;



do{
cout<<" Quantity: ";
cin>>qty;



if (n>0 && n<11)
{
if (qty!=0)
{
if (qty>0 && qty<10001)
{
if (n==1)
{
if (qty<=10)
up=alg.price.belowten;
else if (qty>10 && qty<=50)
up=alg.price.belowfifty;
else if (qty>50 && qty<=100)
up=alg.price.belowonehundred;
else
up=alg.price.moreonehundred;
}
else if (n==2)
{
if (qty<=10)
up=als.price.belowten;
else if (qty>10 && qty<=50)
up=als.price.belowfifty;
else if (qty>50 && qty<=100)
up=als.price.belowonehundred;
else
up=als.price.moreonehundred;
}
else if (n==3)
{
if (qty<=10)
up=br.price.belowten;
else if (qty>10 && qty<=50)
up=br.price.belowfifty;
else if (qty>50 && qty<=100)
up=br.price.belowonehundred;
else
up=br.price.moreonehundred;
}
else if (n==4)
{
if (qty<=10)
up=st.price.belowten;
else if (qty>10 && qty<=50)
up=st.price.belowfifty;
else if (qty>50 && qty<=100)
up=st.price.belowonehundred;
else
up=st.price.moreonehundred;
}
else if (n==5)
{
if (qty<=10)
up=acre.price.belowten;
else if (qty>10 && qty<=50)
up=acre.price.belowfifty;
else if (qty>50 && qty<=100)
up=acre.price.belowonehundred;
else
up=acre.price.moreonehundred;
}
else if (n==6)
{
if (qty<=10)
up=acri.price.belowten;
else if (qty>10 && qty<=50)
up=acri.price.belowfifty;
else if (qty>50 && qty<=100)
up=acri.price.belowonehundred;
else
up=acri.price.moreonehundred;
}
else if (n==7)
{
if (qty<=10)
up=imp.price.belowten;
else if (qty>10 && qty<=50)
up=imp.price.belowfifty;
else if (qty>50 && qty<=100)
up=imp.price.belowonehundred;
else
up=imp.price.moreonehundred;
}
else if (n==8)
{
if (qty<=10)
up=mirr.price.belowten;
else if (qty>10 && qty<=50)
up=mirr.price.belowfifty;
else if (qty>50 && qty<=100)
up=mirr.price.belowonehundred;
else
up=mirr.price.moreonehundred;
}
else if (n==9)
{
if (qty<=10)
up=mag.price.belowten;
else if (qty>10 && qty<=50)
up=mag.price.belowfifty;
else if (qty>50 && qty<=100)
up=mag.price.belowonehundred;
else
up=mag.price.moreonehundred;
}
else if (n==10)
{
if (qty<=10)
up=usp.price.belowten;
else if (qty>10 && qty<=50)
up=usp.price.belowfifty;
else if (qty>50 && qty<=100)
up=usp.price.belowonehundred;
else
up=usp.price.moreonehundred;
}
}
else if (qty!=0 && qty>10000)
cout<<"\n Sorry, We cannot cater too many orders.\n\n";

}

else
cout<<"\n Order atleast 1 Quantity!\n\n";
}
else
{
cout<<"edit";
// refer to wrong input
}

}while (qty==0 || qty>10000);

}

douwntto()
{
do{
cout<<"Do you want to...\n";
cout<<" A-Add Product T-Compute Total\n";
cin>>wntto;
if (wntto=='A' || wntto=='a')
{
addproduct();
}
else if (wntto=='T'|| wntto=='t')
print();
else
{
cout<<"\n\n Wrong Choice...\n";
}
}while (wntto!='A' && wntto!='a' && wntto!='T' && wntto!='t');

}
print()
{

amt= qty*up;

cout<<"\n ";
cout<<"Item Description Qty Unit Price Amount\n";
cout<<" "<<pn[n]<<" "<<qty<<" "<<up<<" "<<amt;



cout<<"\n";
delete [] pn;
return 0;
}

main()
{
prodref();
douwntto();
}


addproduct()
{


cout<<"How many products would you like to add? ";
cin >> i;
k= new (nothrow) int[i];

for (r=0; r<i; r++)
{
prodref();
}

douwntto();

return 0;
}
ok.. i have change my code..

all i want to know is how to store the data in every event the user answers my question no matter how repeatedly he answers it.

ex.

What product you like to order? <answer 1>
quantity: <quantity 1>

would you like to order again? y-yes n-no

if yes:

what product you like to order? <answer 2>
quantity: <quantity 2>

would you like to order again? y-yes n-no

if no:

print

answer 1 quantity1
answer 2 quantity 2

total of quantity 1 and 2
Use a vector and push_back. It will expand as needed without you having to worry about it.
we havent discuss it at school
Well then I suppose you'll have to -yikes- learn something yourself.
as i have looked at the vector::push_back it only count the number of input
Perhaps you could push_back the number of the product, and then push_back the quantity.
i did some reading and created this for test,, here is the code:

#include <iostream>
#include <vector>
using namespace std;

input()
{
vector<int> vectorint;
vector<char> vectorchar;
char letter, yon;
int no;

do{
cout<<"Please enter a letter: ";
cin>>letter;
vectorchar.push_back (letter);
cout<<"Please enter a number: ";
cin>>no;
vectorint.push_back (no);

cout<<"\n\nDo you want to enter again? Y-Yes N-No: ";
cin>>yon;
cout<<"\n\n";

}while (yon!='n');

vector<int>::iterator it;
vector<char>::iterator ip;


for ( ip=vectorchar.begin() ; ip < vectorchar.end(); ip++ )
{
for ( it=vectorint.begin() ; it < vectorint.end(); it++ )
cout<<*ip<<" "<<*it<<"\n";
}
}

main ()
{

input();

}

then this is what just happen:

Please enter letter: y
Please enter number: 6

Do you want to enter again? Y-yes N-no: y

Please enter letter: u
please enter number: 9

do you want to enter again? y-yes N-no: n

y 6
y 9
u 6
u 9
Perhaps you meant this:

1
2
3
4
5
6
it=vectorint.begin()
for ( ip=vectorchar.begin() ; ip < vectorchar.end(); ip++ )
{
  cout<<*ip<<"	"<<*it<<"\n";
  it++;
}
thank you.. you resolved the issue.. but do you know how to add all the integer inputs?=)
hahaha.. got it already..

#include <numeric>

...

then..

sum=std::accumulate (myvector.begin(), myvector.end(), 0)

=)
Topic archived. No new replies allowed.