Review HW Code If I can make it simpler?

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 /*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>
using namespace 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;
	
	


}

Don't include the same header twice. (#include <iostream>)
Also, using namespace std; when you have finished including all the headers.

Thus :
1
2
3
4
5
6
#include <iostream>
using namespace std;
#include <iostream>
#include <string>
#include <sstream>
#include <limits> 


==>
1
2
3
4
5
6
#include <iostream>
#include <string>
#include <sstream>
#include <limits>

using namespace std;
Some are totally redundant :
1.
1
2
3
#include <string>
#include <sstream>
#include <limits> 


2.
string mystr;
Double iostream was a copy paste typo,. I will double check redundant headers as well thank you
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 /*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? ";
        unsigned int 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;
        }
        unsigned float price = 0;
        /*loop statement for(initialization; test part; end of every loop pass instruction) */
	for (unsigned float 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.
Last edited on
why do some people use using namespace std; and some don't?
@thisdudehenry

When you use using namespace 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.
Last edited on
For studying purpose, there is no problem with using namespace std;.

However, on large-scale programs or projects, writing code without using namespace std; is much better.
Topic archived. No new replies allowed.