### SOS-Arrays and Input

alrite, so I have to ask the user for a choice, once the choice is selected, say
1 which corresponds to cheese,I have to use the arrays to cross check the cost for bread and minus it with the amount given by the user.

 ``12345678910111213141516171819202122232425`` `````` #include using namespace std; int main(){ int Price[] = {150,200,75,350};//the price for each item in cents int TotalItems[] = { 5, 5, 5, 5,}; //The number of items cout<<"1. Cheese \$1.50\n"; cout<<"2. Butter \$2.00\n"; cout<<"3. Bread \$0.75\n"; cout<<"4. Jam \$3.50\n"; cout<<"5. End of transaction\n"; cout<>Input; cout<<"Enter money figure:"; cin>>Money; if(Input == 1){ //Lost here }``````

Oh, and if the user inputs 2234 meaning 2 Butter 1 Bread and 1 Jam, how do you
carter for that.....thanks.....
Last edited on
if user inputs 2234, where each digit represents an item, then what you could do is input that value into a variable( i dont think you declared Input as a variable yet, although you wrote Input there) and then use a loop to decipher that number. for instance, i would use a while loop and divide the number by ten constantly, and get the remainder for the if statements:

 ``1234567891011121314`` ``````while(Input!=0){ int i = Input%10;//you will get the last digit. for instance if it was 2234, then i = 4 because the remainder of input divided by 10 is 4 if(i ==1){ //give cheese } if(i==2){ //give butter } //....so on and so forth Input/=10; } ``````

for the code inside the if's, make a new variable called total_cost and each time that if statement is called, it will add a specific price to total_cost each time
Last edited on
Thanks, you're a life saver....
Oh I forgot to mention, say if the user enters 2.00 and selects cheese, right...
so 2.00 - 1.50 will give 50c change....how would you decrement the value of 50 cents coins to 4(Because the total for each coin is 5).Lets say that there is an array that stores number of coins, for each denomination there are 5 coins, so once a coin is withdrawn it will decrease the amount from the array, how would I do that, decrement the value of a particular denomination and decreament the number of items array since an item will be taken out...thanks
Last edited on
im a bit confused at what youre saying... i assume there are five items each available, aka 5 cheese, 5 butter, etc. so if you want to show that you bought one cheese, then you would do: TotalItems[0]--; however, i dont understand the coin concept
Alrite, I myself too was confused, say you have an array.

int Coins[] = {100,50,20,10};//each denomination in cents
int NumberCoins[] = { 5, 5, 5, 5,}; //The number of coins

And yes, there are 5 \$1.00, 5 50cent coins etc.
so, if the user enters 2.00 and picks cheese so ( 2.00 - 1.50) which is 50 cents
how do I decrement the value of 50 cents in the int NumberCoins[] array to 4 since, 50 cents will be given away as change, make sense...thanks

Last edited on
So, the while loop fits like a glove, thanks again...
just one more question, if you don't mind...
Alrite, so the user would type the choice and the amount the user puts in...
If the user were to enter an amount less than the cost price..say the user picks bread, jam and butter which come to a total of 625 and the user enters 400 in the amount variable.

[code]

cin>>Input;

cout<<"Enter amount: ";
int amount;
cin>>amount;

while(Input!=0){

int i = Input%10;
if(i ==1){

cout<< "cheese"<<endl;
if(amount<150){
cout<<"Error, input more money:";
cin>>amount;
}
else
{
cout<<" "<<amount-150<<endl;
}
}

[code]
what am i doing wrong here.

how do I keep asking the user to enter more money so the amount is more than or equal to the total, in this case total = 625, amount = 400, so the user needs another 225.....thanks
Last edited on
change the if statement to a while loop, and print out the change after the while loop stops iterating. If you are after extra money, you need to change the variable name for the amount added, and then add the input to the original amount.
thanks , manudude03,
but I've tried using a do while loop to keep asking the user to enter more money until it reaches the desired amount... but there seems to be a problem..

cout<<"Enter money figure: "
cin>>amount;
if(amount<Cost){
do{
cout<<"Enter more money: ";
cin>>newamount;
newamount = newamount + amount;
}while(newamount<=Cost);
cout<<"Thank you";
}

lets say all variables are declared

where am I going wrong....
newamount = newamount + amount;
}while(newamount<=Cost);

should be:

amount= newamount + amount;
}while(amount<=Cost);

when you do a cin, the value the user enters replaces whatever it was before.
Last edited on
Now, Im getting somewhere....
just a slight complication..

when the amount entered is 300.
and the Cost is 600,

so, I come to the place where it says to enter more money, right.
it looks something like this:
Cost: 600
Enter amount: 300
Enter more money:100
Enter more money:100
Enter more money:100 - should the loop stop here since its equal to 600
Enter more money:100
Thank You
Last edited on
Change <= to < in the while condition.
THANK YOU SO MUCH

MAY YOU LIFE BE BLESSED ABUNDANTLY.......Modu Out...
Topic archived. No new replies allowed.