I believed I did this correctly. But I have the tendency to over think things. Just wanted to know if there are things I can make any simpler or cut down on code?
/*Jason typically uses the Internet to buy various items. If the total cost of the
items ordered, at one time, is $200 or more, then the shipping and handling is
free; otherwise, the shipping and handling is $10 per item. Design an algorithm
that prompts Jason to enter the number of items ordered and the price of each
item. The algorithm then outputs the total billing amount. Your algorithm
must use a loop (repetition structure) to get the price of each item. (For
simplicity, you may assume that Jason orders no more than five items at a time.)*/
#include <iostream>
usingnamespace std;
#include <iostream>
#include <string>
#include <sstream>
#include <limits>
int main()
{
/*These will calculate prices and quantity and input */
string mystr;
float price = 0;
int quantity = 0;
float temp_price;
float total = 0;
cout << "How many items are you purchasing? ";
cin >> quantity; /* inputting a quantity*/
for (int i = 0; i < quantity; i++) /*loop statement for(initialization; test part; end of every loop pass instruction) */
{
cout << "Please enter the price of the item. $";
cin >> temp_price;
price += temp_price;
}
if (price >= 200)
{
cout << "Your shipping is free" << endl;
total = price;
}
else
{
cout << "You must pay for shipping, the fee is $10 per item, this equals: $" << quantity * 10 << endl;
total = price + (quantity * 10);
}
cout << "Your total fee is: $" << total << endl;
system("PAUSE") ;
return 0;
}
/*Jason typically uses the Internet to buy various items. If the total cost of the
items ordered, at one time, is $200 or more, then the shipping and handling is
free; otherwise, the shipping and handling is $10 per item. Design an algorithm
that prompts Jason to enter the number of items ordered and the price of each
item. The algorithm then outputs the total billing amount. Your algorithm
must use a loop (repetition structure) to get the price of each item. (For
simplicity, you may assume that Jason orders no more than five items at a time.)*/
#include <iostream>
void pause()
{
std::cin.ignore(); // Don't use 'system' anything
}
int main()
{
/*These will calculate prices and quantity and input */
std::cout << "How many items are you purchasing? ";
unsignedint quantity;
// Check for bad input
while (! (std::cin >> quantity)) /* inputting a quantity*/
{
std::cout << "Enter a postive number: ";
std::cin.clear();
std::cin.ignore(1000000, '\n');
std::cin >> quantity;
}
unsignedfloat price = 0;
/*loop statement for(initialization; test part; end of every loop pass instruction) */
for (unsignedfloat i = 0, temp_price; i < quantity; i++)
{
std::cout << "Please enter the price of the item. $";
while (! (std::cin >> temp_price)) // Check for bad input
{
std::cout << "Enter a positive number: ";
std::cin.clear();
std::cin.ignore(1000000, '\n');
std::cin >> temp_price;
}
price += temp_price;
}
if (price >= 200)
{
std::cout << "Your shipping is free" << std::endl;
}
else
{
std::cout << "You must pay for shipping, the fee is $10 per item, this equals: $" <<
quantity * 10 << std::endl;
price += quantity * 10;
}
std::cout << "Your total fee is: $" << price << std::endl;
pause(); // To make things simpler
return 0;
}
Removed unnecessary headers.
Replaced system("pause"); with std::cin.ignore(); (using system(); is bad practice).
Made a function for pause to make things simpler.
Declared/Initialized variable where they are used to make things clearer.
Removed unnecessary string mystr.
Removed unnecessary variable float total;.
Error checking for bad input.
Made variables unsigned since it makes sense they only be positive values.
When you use usingnamespace std; it defeats the purpose why cout and cin (other things too) were put into the std namespace... To prevent naming collisions.
So it is better to fully qualify the std namespace instead.
To prevent having names clash with each other.
Do not use using namespace std;.
Professionals avoid this too.