# C++ homework help

Show 40 post(s) from this thread on one page
Page 1 of 2 1 2 Last
• 10-16-2007, 12:09 PM
kibbles
C++ homework help
"Project 1

For this project, create a grocery store system. Users of the system can choose how many items they want to buy and then are told how much they owe. Users will have the option of buying apples and oranges. Here are some parameters that your program should include:

* Program should ask users how many apples and oranges they want to buy.
* Apples cost \$1.00 and Oranges cost \$1.50.
* Today's Special! Apples are 2 for 1. Buy one get one free! (1 apple = \$1.00, 2 apples = \$1.00, 3 apples = \$2.00, 4 apples = \$2.00, etc.)
* Another Special! How ever many oranges you buy will give you the same number of percentage off your total bill. (If you buy 10 oranges you get 10% off your total bill.) (For now, ask the user to enter a number between 1 and 100. If they don't the numbers will not make sense. Later we will learn how to capture user input errors.
* If you have bought 2 apples = \$1.00 and 10 oranges = \$15.00 your total bill would be \$16.00. But you get 10% off! (use percentage/100*amount = discount amount) 10% of \$16.00 = (10/100) * 16.00 = \$1.60 discount. Total bill = \$16.00 - \$1.60 = \$14.40
* Your program should tell the user how much they owe."

alright this is the project im working on now. i am almost done, actually everything is done except for the the things in bold. (there is more to the project but i just pasted the part i need help with). how would i make it recognize that you get 2 apples for 1.00 (currently i have it set up so 1 apple is 1.00, but i have a second variable called freeapple, and i thought about making it 1 apple for 50 cents, so when u buy 2 it cost a dollar, but that wouldnt look good)......and i have no idea about the second problem i have, with the discount, any help would be appreciated.

im not asking u to do this for me, just try to explain or give me a example on how i would do the 2 things im having trouble with.
• 10-16-2007, 12:18 PM
Realn0whereman
use modulo

if apples%2=0 then apples/2 = price
if apples%2 = 1 then apples/2 +.5 = price

btw i dont know the actual C++ language. i just know it has a modulo operator or method, and thats how u would think it through

the second part:

(applePrice+orangePrice)* .9 = finalPrice
• 10-16-2007, 12:27 PM
kibbles
Quote:

Originally Posted by Realn0whereman
use modulo

if apples%2=0 then apples/2 = price
if apples%2 = 1 then apples/2 +.5 = price

btw i dont know the actual C++ language. i just know it has a modulo operator or method, and thats how u would think it through

the second part:

(applePrice+orangePrice)* .9 = finalPrice

trying this now. will get back to u. thanks.

OMG LOL, at the bottom of the page it says " if you have previous programming knowledge do not use the IF statement. we will learn it in the next unit, plus refrain from using it in this program"

lol i just got ownd
• 10-16-2007, 12:28 PM
Realn0whereman
Quote:

Originally Posted by kibbles
trying this now. will get back to u. thanks.

np. god i love programming. if i only had time.
• 10-16-2007, 12:36 PM
kibbles
Quote:

Originally Posted by Realn0whereman
np. god i love programming. if i only had time.

look up i edited my post. cannot use if statements any other ideas?
• 10-16-2007, 12:41 PM
Realn0whereman
Quote:

Originally Posted by kibbles
look up i edited my post. cannot use if statements any other ideas?

wtf.... maybe theres some trick in C++, but i dont know any way i would do it in perl or java....

you have to check if the amount of apples is even or odd....

are you allowed to use loops?
• 10-16-2007, 12:59 PM
kibbles
Quote:

Originally Posted by Realn0whereman
wtf.... maybe theres some trick in C++, but i dont know any way i would do it in perl or java....

you have to check if the amount of apples is even or odd....

are you allowed to use loops?

ya we've done loops. ( i think lol)
• 10-16-2007, 01:07 PM
Realn0whereman
Quote:

Originally Posted by kibbles
ya we've done loops. ( i think lol)

loop (i=0;i<1 and apples%2=0;i++){

apples/2 = price
}

loop (i=0;i<1 and apples%2=0){
apples/2 +.5 = price
}

basically, create a loop (i dont know what the C++ syntax is)

but create a loop, initiate an integer i, and as long as i<1 and the apples%2 =0 itll run through the loop and assign the price. what makes it stop looping, is that when apples%2=0, itll increase i by 1. and then the loop stops and you have a price. it wont go through the second loop because if apples%2=0, then apples%2 cannot equal 1

so there u go
• 10-16-2007, 02:10 PM
kibbles
i guess i dont no loops

all we've done so far are variables with
int
float
int const

(and maybe a few other basic ones)
• 10-16-2007, 02:17 PM
HippyG
I wish I could program. I'm at the tutorial part where you have to make a fahr - cel table :P
• 10-16-2007, 02:27 PM
Realn0whereman
Quote:

Originally Posted by kibbles
i guess i dont no loops

all we've done so far are variables with
int
float
int const

(and maybe a few other basic ones)

well go learn em. cz your book is telling you to be smart, but not TOO smart, cz then that just defeats the purpose.
• 10-16-2007, 02:27 PM
kibbles
I GIVE UP!!!!!!!!!!!!!!!!!!!!!!!

#include <iostream>
using namespace std;
int main()

{
int const APPLE = 1.00;
int const ORANGE = 1.50;
int applequanity;
int orangequanity;
int discount;

// title of project
cout << " __ " << endl;
cout << " _____ _ _ _____ | |" << endl;
cout << "| _ |___ ___| |___ ___ ___ ___ _| | | |___ ___ ___ ___ ___ ___| |" << endl;
cout << "| | . | . | | -_|_ -| | .'| | . | | | | _| .'| | . | -_|_ -|__|" << endl;
cout << "|__|__| _| _|_|___|___| |__,|_|_|___| |_____|_| |__,|_|_|_ |___|___|__|" << endl;
cout << " |_| |_| |___| " << endl;

// description of project
cout << "This grocery store system will ask you how many apples and/or oranges you would like to buy and will tell the buyers how much they owe." << endl << endl;

// display special offers
cout << "Today's Special!!! Apples are buy one get one free! " << endl;
cout << "Weekly Special!!! However many oranges you buy, you will receive that percentage off your bill! " << endl << endl;

// ask user how many apples and/or oranges they want
cout << "How many apples would you like to buy?" << endl;
cin >> applequanity;

cout << "How many oranges would you like to buy? (Must be between 1 and 100)" << endl;
cin >> orangequanity;

cout << " You owe a total of: " << endl;
cout << (APPLE * applequanity) + (ORANGE * orangequanity) << endl;

}

THATS MY CODE SO FAR HOW DO I PUT IN THE BUY ONE GET 1 APPLE FREE AND 1% DISCOUNT PER ORANGE BOUGHT?!!?!?!??!!??! I'M READY TO THROW MY LAPTOP OUT THE DAM WINDOW PLEASE HELP!!!

err that big blob of lines is ascii art but it just messed up when i pasted it, so ignore that if nesscesary.
• 10-16-2007, 02:43 PM
Realn0whereman
Quote:

Originally Posted by kibbles
I GIVE UP!!!!!!!!!!!!!!!!!!!!!!!

#include <iostream>
using namespace std;
int main()

{
int const APPLE = 1.00;
int const ORANGE = 1.50;
int applequanity;
int orangequanity;
int discount;

// title of project
cout << " __ " << endl;
cout << " _____ _ _ _____ | |" << endl;
cout << "| _ |___ ___| |___ ___ ___ ___ _| | | |___ ___ ___ ___ ___ ___| |" << endl;
cout << "| | . | . | | -_|_ -| | .'| | . | | | | _| .'| | . | -_|_ -|__|" << endl;
cout << "|__|__| _| _|_|___|___| |__,|_|_|___| |_____|_| |__,|_|_|_ |___|___|__|" << endl;
cout << " |_| |_| |___| " << endl;

// description of project
cout << "This grocery store system will ask you how many apples and/or oranges you would like to buy and will tell the buyers how much they owe." << endl << endl;

// display special offers
cout << "Today's Special!!! Apples are buy one get one free! " << endl;
cout << "Weekly Special!!! However many oranges you buy, you will receive that percentage off your bill! " << endl << endl;

// ask user how many apples and/or oranges they want
cout << "How many apples would you like to buy?" << endl;
cin >> applequanity;

cout << "How many oranges would you like to buy? (Must be between 1 and 100)" << endl;
cin >> orangequanity;

cout << " You owe a total of: " << endl;
cout << ((APPLE * applequanity) + (ORANGE * orangequanity))*((100-ORANGE)/100) << endl;

}

THATS MY CODE SO FAR HOW DO I PUT IN THE BUY ONE GET 1 APPLE FREE AND 1% DISCOUNT PER ORANGE BOUGHT?!!?!?!??!!??! I'M READY TO THROW MY LAPTOP OUT THE DAM WINDOW PLEASE HELP!!!

err that big blob of lines is ascii art but it just messed up when i pasted it, so ignore that if nesscesary.

i did the part in bold for you. its not 10% off. its ORANGE% off... so if u buy 100 oranage,100% off and u get free apples and orange. if u buy 200 oranages, the cashier owes u money.
• 10-16-2007, 02:50 PM
FranzH
Ugh, I wish we did C++ in college. We just learn VB.

Edit: Hehe I made a lame program that opened up a gaypr0nz website in your browser on a loop, then I hid the program. I then went about sending it to my friends :) ah fun times.
• 10-16-2007, 03:04 PM
Sir_Axxewraith
FH,

I wish I came up with schemes like that.

I just go home do homeowork, sleep, eat, sleep. hah
• 10-16-2007, 03:21 PM
Realn0whereman
Quote:

Originally Posted by FranzH
Ugh, I wish we did C++ in college. We just learn VB.

Edit: Hehe I made a lame program that opened up a gaypr0nz website in your browser on a loop, then I hid the program. I then went about sending it to my friends :) ah fun times.

we learn java, and its quite fun...

i read a little ahead and learned to loop and saved myself 2 weeks of work

day 1 - draw square
day 2 - draw triangle
...
day 9 - draw pentagon

well i just wront something to draw an ngon and it accept the number of sides and the length of one side as a paramter and i just sat on here for the two weeks..
• 10-16-2007, 03:30 PM
FreePlay
...apple price = 2 * ((apple count + 1) / 2)

use an integer. the remainder is lost. you'll only get an increase of \$2 per multiple of two.

C-style code example:
Code:

```#include <stdio.h> int main() {         int applecount;         for(applecount=0;applecount<10;applecount++)                 printf("applecount = %i, price = %i\n", applecount, 2 * ((applecount + 1) / 2));        return 1; }```
Output:
Code:

```HP_Administrator@FreePlay / \$ gcc wut.c -o wut HP_Administrator@FreePlay / \$ ./wut applecount = 0, price = 0 applecount = 1, price = 2 applecount = 2, price = 2 applecount = 3, price = 4 applecount = 4, price = 4 applecount = 5, price = 6 applecount = 6, price = 6 applecount = 7, price = 8 applecount = 8, price = 8 applecount = 9, price = 10```
Also:
Code:

```int const APPLE = 1.00; int const ORANGE = 1.50;```
APPLE will be 1, and ORANGE will be 1. Integers have no decimals.
• 10-16-2007, 04:56 PM
kibbles
Quote:

Originally Posted by FreePlay
...apple price = 2 * ((apple count + 1) / 2)

use an integer. the remainder is lost. you'll only get an increase of \$2 per multiple of two.

C-style code example:
Code:

```#include <stdio.h> int main() {         int applecount;         for(applecount=0;applecount<10;applecount++)                 printf("applecount = %i, price = %i\n", applecount, 2 * ((applecount + 1) / 2));        return 1; }```
Output:
Code:

```HP_Administrator@FreePlay / \$ gcc wut.c -o wut HP_Administrator@FreePlay / \$ ./wut applecount = 0, price = 0 applecount = 1, price = 2 applecount = 2, price = 2 applecount = 3, price = 4 applecount = 4, price = 4 applecount = 5, price = 6 applecount = 6, price = 6 applecount = 7, price = 8 applecount = 8, price = 8 applecount = 9, price = 10```
Also:
Code:

```int const APPLE = 1.00; int const ORANGE = 1.50;```
APPLE will be 1, and ORANGE will be 1. Integers have no decimals.

it works when i do that, but were only suppose to do stuff that we learned already and we havent learned that lol. thanks freeplay i think i can figure it out based of this though.
• 10-16-2007, 05:15 PM
Judas
I really don't feel like reading all the posts, so here it goes. I don't know C++, but here's how this would go in C. (The first one, about 2 for 1 deal.)

price = ceil(quantity/2);

As for 10% off,

price = price * .9;
• 10-16-2007, 05:17 PM
kibbles
am i suppose to use constants?

i get
g:\visual studio 2005\projects\grocery store\grocery store\grocery store.cpp(6) : warning C4244: 'initializing' : conversion from 'double' to 'const int', possible loss of data
g:\visual studio 2005\projects\grocery store\grocery store\grocery store.cpp(7) : warning C4244: 'initializing' : conversion from 'double' to 'const int', possible loss of data

errors when i use constant

if i use float instead of const it doesnt give me that error
• 10-16-2007, 05:17 PM
explosions
Quote:

Originally Posted by Judas
I really don't feel like reading all the posts, so here it goes. I don't know C++, but here's how this would go in C. (The first one, about 2 for 1 deal.)

price = ceil(quantity/2);

As for 10% off,

price = price * .9;

It's 1% off per orange.
• 10-16-2007, 05:19 PM
foXtrot
Those are warnings not errors, the compiler is letting you know that because you are assigning a float value to an integer variable you will lose the fractional part.
• 10-16-2007, 05:23 PM
Judas
Quote:

Originally Posted by kibbles
am i suppose to use constants?

i get
g:\visual studio 2005\projects\grocery store\grocery store\grocery store.cpp(6) : warning C4244: 'initializing' : conversion from 'double' to 'const int', possible loss of data
g:\visual studio 2005\projects\grocery store\grocery store\grocery store.cpp(7) : warning C4244: 'initializing' : conversion from 'double' to 'const int', possible loss of data

errors when i use constant

if i use float instead of const it doesnt give me that error

Those are warnings. They give you that because when you convert a double to an integer, you're dropping the decimal information. If the double value is 15.928, the integer value will be only 15. But you can still run it like that.

BTW- Could you post this entire project, so I can have a look at it? Gives me something to do.
• 10-16-2007, 05:30 PM
kibbles
Quote:

Originally Posted by Judas
Those are warnings. They give you that because when you convert a double to an integer, you're dropping the decimal information. If the double value is 15.928, the integer value will be only 15. But you can still run it like that.

BTW- Could you post this entire project, so I can have a look at it? Gives me something to do.

the coding part of the project is on the 1st page, the rest is what he is grading based on, and what we can and cannot do (if statements and loops).
• 10-16-2007, 06:04 PM
Judas
Here's a quick C sample, maybe it'll help. I don't know, I was bored.

Code:

```#include <stdio.h> #include <string.h> float balance; int oranges,apples; char input[10]; int main() {         printf("Welcome to The Market!\n");         printf("Items for Sale \n\n");         printf("Apples - \$1.00\n");         printf("Oranges - \$1.50\n");         printf("**Today's Specials**\n");         printf("But one get one free apples!\n");         printf("Get a 1%% discount off your total bill for each orange you buy!\n\n");         printf("Please make your selction.\n");         printf("How many apples would you like? ");         gets(input);         apples = atoi(input);         printf("\nHow many oranges would you like? ");         printf("\nBut, please, not too many. We only have 100.");         //if (oranges > 100) { oranges = 100; } You said no if statements, so....         gets(input);         oranges = atoi(input);         balance = (ceil(apples/2) + (oranges * 1.5)) * (1 - oranges/100);         printf("\nAfter discounts, your total comes to %f",balance);         printf("\nThank you, and have a nice day.\n\n");         getchar(); }```
• 10-16-2007, 06:37 PM
Anti-QuickJay
Quote:

Originally Posted by Judas
Here's a quick C sample, maybe it'll help. I don't know, I was bored.

Code:

```#include <stdio.h> #include <string.h> float balance; int oranges,apples; char input[10]; int main() {     printf("Welcome to The Market!\n");     printf("Items for Sale \n\n");     printf("Apples - \$1.00\n");     printf("Oranges - \$1.50\n");     printf("**Today's Specials**\n");     printf("But one get one free apples!\n");     printf("Get a 1% discount off your total bill for each orange you buy!\n\n");     printf("Please make your selction.\n");     printf("How many apples would you like? ");     gets(input);     apples = atoi(input);     printf("\nHow many oranges would you like? ");         printf("\nBut, please, not too many. We only have 100.");         //if (oranges > 100) { oranges = 100; } You said no if statements, so....     gets(input);     oranges = atoi(input);     balance = (ceil(apples/2) + (oranges * 1.5)) * (1 - oranges/100);     printf("\nAfter discounts, your total comes to %f",balance);     printf("\nThank you, and have a nice day.\n\n");         getchar(); }```

When I compiled that, this is the out put.

Welcome to The Market!
Items for Sale

Apples - \$1.00
Oranges - \$1.50
**Today's Specials**
But one get one free apples!
Get a 1 2346192iscount off your total bill for each orange you buy!

How many apples would you like?
• 10-16-2007, 06:49 PM
Realn0whereman
try using exit sequences around the % thing
• 10-16-2007, 06:50 PM
Judas
Quote:

Originally Posted by Anti-QuickJay
When I compiled that, this is the out put.

Welcome to The Market!
Items for Sale

Apples - \$1.00
Oranges - \$1.50
**Today's Specials**
But one get one free apples!
Get a 1 2346192iscount off your total bill for each orange you buy!