HelpC++ HW Help

Posts 1–6 of 6 · Page 1 of 1
C++ HW Help
Hey guys,

I am having issues with a C++ homework I am doing. I would like some help please.



Create your program, zip up the main.cpp file ONLY and load it to Mimir - make sure your cpp file is named main.cpp!.

You have a book club that awards discounts to the purchasers of books based on the number of books they purchase. Discounts are awarded as follows.

Points Books Purchased Points Awarded 0 0 1 1% 2 2% 3 3% 4+ 5%

Your program should ask the user for the number of books purchased,

Then for a given book: Advanced C++ Programming by Gaddis, Store Price $39.95

Display the sale as:

Name: The actual name of the book from above
Store Cost:
Discount awarded: as a percent and as a
Final Cost:




This is what I have and I know I am doing something super wrong:

#include<iostream>

using namespace std;

int main()
{
int books, points;


cout << "Enter the number of books purchased this month: ";
cin >> books;

if(books == 0)
{
points = 0;
cout << "You have earned points: " << points <<endl;
}
if(books == 1)
{
points = 1;
cout << "You have earned points : " << points <<endl;
}
if(books == 2)
{
points = 2;
cout << "You have earned points: " << points <<endl;
}
if(books == 3)
{
points = 3;
cout << "You have earned points: " << points <<endl;
}
if(books >= 4)
{
points = 4;
cout << "You have earned points: " << points <<endl;
}
else
{
if(books < 0)
cout << "Number of books cannot be negative!! \n\n";
}



return 0;
}


Can somebody help me figure this out please. I have posted a link from imgur that shows what the homework is supposed to be.

https://imgur.com/a/7VfJkMk
Code:
#include<iostream>

struct Book 
{
	char* name;
	float price;
};

const Book Library[] = 
{
	{ "Advanced C++ Programming by Gaddis", 39.95f },
	{ "Poor C++ Programming by Mike", 1.99f },
};

char* get_pretty_float(float f)
{
	static char prettyFloat[255];
	sprintf_s(prettyFloat, "%.2f", f);
	return prettyFloat;
}

int get_priceclass(int bookCount)
{
	if (bookCount >= 4)
		return 5;
	return bookCount;
}

float get_price(float price, int bookCount)
{
	if (bookCount == 0)
		return price;
	return price - (price / 100) * get_priceclass(bookCount);
}

int main()
{
	int iBookCount;
	size_t iBook;
	std::cout << "Enter the number of books purchased this month: " << std::endl;
	std::cin >> iBookCount;

	std::cout << "Enter the number of the Book you wanna buy" << std::endl;
	for (size_t i = 0; i < std::size(Library); i++) 
	{
		std::cout << "[" << i << "]" << Library[i].name << ", " << get_pretty_float(get_price(Library[i].price, iBookCount)) << "$" << std::endl;
	}
	std::cin >> iBook;
	if (iBook < std::size(Library)) 
	{
		std::cout << "You bought " << Library[iBook].name << " for " << get_pretty_float(get_price(Library[iBook].price, iBookCount)) << "$" << std::endl;
	} 
	else 
	{
		std::cout << "No valid Number, no books" << std::endl;
	}
	
	return 0;
}
Quote Originally Posted by MikeRohsoft View Post
Code:
#include<iostream>

struct Book 
{
	char* name;
	float price;
};

const Book Library[] = 
{
	{ "Advanced C++ Programming by Gaddis", 39.95f },
	{ "Poor C++ Programming by Mike", 1.99f },
};

char* get_pretty_float(float f)
{
	static char prettyFloat[255];
	sprintf_s(prettyFloat, "%.2f", f);
	return prettyFloat;
}

int get_priceclass(int bookCount)
{
	if (bookCount >= 4)
		return 5;
	return bookCount;
}

float get_price(float price, int bookCount)
{
	if (bookCount == 0)
		return price;
	return price - (price / 100) * get_priceclass(bookCount);
}

int main()
{
	int iBookCount;
	size_t iBook;
	std::cout << "Enter the number of books purchased this month: " << std::endl;
	std::cin >> iBookCount;

	std::cout << "Enter the number of the Book you wanna buy" << std::endl;
	for (size_t i = 0; i < std::size(Library); i++) 
	{
		std::cout << "[" << i << "]" << Library[i].name << ", " << get_pretty_float(get_price(Library[i].price, iBookCount)) << "$" << std::endl;
	}
	std::cin >> iBook;
	if (iBook < std::size(Library)) 
	{
		std::cout << "You bought " << Library[iBook].name << " for " << get_pretty_float(get_price(Library[iBook].price, iBookCount)) << "$" << std::endl;
	} 
	else 
	{
		std::cout << "No valid Number, no books" << std::endl;
	}
	
	return 0;
}
yep that's about it
Thank you so much for the quick reply!!!
...am i the only one who thinks the professors going to be scratching his head when he sees the code above submitted compared to his previous submissions? just judging from the original post
Quote Originally Posted by gogogokitty View Post
...am i the only one who thinks the professors going to be scratching his head when he sees the code above submitted compared to his previous submissions? just judging from the original post
The code came to him in a dream, and he wrote it down the following morning. :P
Posts 1–6 of 6 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us