Code help

Hey all, I am having some problems with a program I am writing. It has to do with shipping a product. The program is a bit long, but I am very much a beginner, which is why I posted here. The program needs to ask for a Base price, a weight, whether it is shipping in state or out, and a code for hazards materials, along with compensating for user error. After which, it adds all the relevant data together and outputs and price. I put everything together, but the program ends after the weight is asked for. I was hoping someone here could help me and point out which of the many things I am doing wrong.

Here is the code itself
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
//Variable Declerations
double WEIGHT;
double COST;
double BasePRICE;
double in_outSHIPPING;
double TOTALCOST;
double ASHIP;
bool VALIDdata;
bool STATE;
bool HAZARD;

cout <<"Please enter the base price of the produce and press <ENTER>: ";
cin >> BasePRICE;
if (BasePRICE > 0)
{
	cout << "Please enter the weight of the item to be shipped and press <ENTER>: ";
	cin >> WEIGHT;
	if (WEIGHT > 0 && WEIGHT <= 20)
		COST = 14.95;
	else
		if (WEIGHT > 20 && WEIGHT <= 50)
			COST = 19.95;
		else
			if (WEIGHT > 50 && WEIGHT <= 100)
				COST = 29.95;
			else
				if (WEIGHT > 100 && WEIGHT <= 200)
					COST = 49.95;
				else
					if (WEIGHT > 200)
						COST = 49.95;
					else 
						if (WEIGHT < 0)
							cout << "Please enter a valid weight above 0 lbs \n";
							VALIDdata = false;
							system("pause");
							return 0;
}
else (BasePRICE < 0);
	cout <<"Please enter a valid price above $0 \n" ;
	VALIDdata = false;
	system("pause");
	return 0;

cout <<"Please enter the hazard code as A, B, or C in uppercase letters";
cin >> HAZARD;
if (HAZARD = "A")
	ASHIP = 0;
else
	if (HAZARD = "B")
		ASHIP = 25;
	else
		if (HAZARD = "C")
			ASHIP = 50;
		else
			cout<<"Please enter a valid answer of A, B, or C in uppercase letters";
			VALIDdata = false;
			system("pause");
				return 0;
cout <<"Will the product be shipping in state? Please answer yes or no in lowercase.";
cin >> STATE;
if (STATE = "yes")
	in_outSHIPPING = (BasePRICE * 1.06) - 10;

else
	if (STATE = "no")
		in_outSHIPPING = BasePRICE;
	else 
		cout<<"Please use a valid answer in all lowercase letters \n";
		VALIDdata = false;
		system("pause");
		return 0;

TOTALCOST = COST + ASHIP + in_outSHIPPING;

cout <<"The total cost to purchase and ship your product is $ "<< TOTALCOST<<


system("pause");
return 0;
}
You should look up the syntax on "if - else if - else" statements. You're missing a few {curly braces}

Note: In C++ indentation is purely for readability. It only implies control structure, but doesn't enforce anything. It is just the convention!
1
2
3
4
if(b)
	...; //executes only if b is true
	...; //executes regardless, shouldn't be indented (by convention.)
...; //executes regardless 
1
2
3
4
5
if(b) {
	...; //executes only if b is true
	...; //executes only if b is true
} //<-- note the curly braces for multi-line "if" statements
...; //executes regardless 
1
2
3
if(b)
...; //executes only if b is true, should be indented or on the same line as the if (by convention.)
...; //executes regardless 
Last edited on
You mean to say something like this?
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
 #include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;

int main()
{
//Variable Declerations
double WEIGHT;
double COST;
double BasePRICE;
double in_outSHIPPING;
double TOTALCOST;
double ASHIP;
bool VALIDdata;
bool STATE;
bool HAZARD;

cout <<"Please enter the base price of the produce and press <ENTER>: ";
cin >> BasePRICE;
if (BasePRICE > 0)
{
	cout << "Please enter the weight of the item to be shipped and press <ENTER>: ";
	cin >> WEIGHT;
	if (WEIGHT > 0 && WEIGHT <= 20)
	{
		COST = 14.95;
	}
	else
	{
		if (WEIGHT > 20 && WEIGHT <= 50)
		{
			COST = 19.95;
		}
		else
		{
			if (WEIGHT > 50 && WEIGHT <= 100)
			{
				COST = 29.95;
			}
			else
			{
				if (WEIGHT > 100 && WEIGHT <= 200)
				{
					COST = 49.95;
				}
				else
				{
					if (WEIGHT > 200)
					{
						COST = 49.95;
					}
					else
					{
						if (WEIGHT < 0)
						{
							cout << "Please enter a valid weight above 0 lbs \n";
							VALIDdata = false;
							system("pause");
							return 0;
						}
					}
				}
			}
		}
	}
}
else
{
	if (BasePRICE < 0);
	{
	cout <<"Please enter a valid price above $0 \n" ;
	VALIDdata = false;
	system("pause");
	return 0;
	}
}


cout <<"Please enter the hazard code as A, B, or C in uppercase letters: ";
cin >> HAZARD;
if (HAZARD == 'A')
{
	ASHIP = 0;
}
else
{
	if (HAZARD == 'B')
	{
		ASHIP = 25;
	}
	else
	{
		if (HAZARD == 'C')
		{
			ASHIP = 50;
		}
		else
		{
			cout<<"Please enter a valid answer of A, B, or C in uppercase letters: ";
			VALIDdata = false;
			system("pause");
			return 0;
		}
	}
}
cout <<"Will the product be shipping in state? Please answer yes or no in lowercase: ";
cin >> STATE;
if (STATE == 'yes')
{
	in_outSHIPPING = (BasePRICE * 1.06) - 10;
}

else
{
	if (STATE == 'no')
	{
		in_outSHIPPING = BasePRICE;
	}
	else
	{
		cout<<"Please use a valid answer in all lowercase letters \n";
		VALIDdata = false;
		system("pause");
		return 0;
	}
}
system("pause");

TOTALCOST = COST + ASHIP + in_outSHIPPING;

cout <<"The total cost to purchase and ship your product is $ "<< TOTALCOST<<
system("pause");
return 0;
}


Ok, now it asks all the proper questions, but doesent recognize the response for the A,B, or C, and just skips to the error for entering A,B, or C no matter what is entered, and I think it might do that for yes or no as well.
Again, any help would be appreciated.
Last edited on
Let me help a bit, because this is getting a bit unreadable:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	if (WEIGHT > 0 && WEIGHT <= 20)
		COST = 14.95;
	else if (WEIGHT > 20 && WEIGHT <= 50)
		COST = 19.95;
	else if (WEIGHT > 50 && WEIGHT <= 100)
		COST = 29.95;
	else if (WEIGHT > 100 && WEIGHT <= 200)
		COST = 49.95;
	else if (WEIGHT > 200)
		COST = 49.95;
	else if (WEIGHT < 0)
        {
		cout << "Please enter a valid weight above 0 lbs \n";
		VALIDdata = false;
		system("pause");
		return 0;
	}


That is, I think, more acceptable and readable.

One more thing, I see you don't really check if weight == 0, so all that would be skipped if the user entered a zero.

Another tip, if you put the WEIGHT < 0 at the top of the list of conditionals, you can drop the lower limits of each of the checks, since the if statements are chained, they are one long list, each being part of the whole, progressing upwards.

To illustrate, say I'm range checking a number and the ranges are defined as 0 - 9, 10 - 19, 20 - 29 and it's assured the number is >= 0 and < 30:

1
2
3
4
5
6
7
8
if ( num < 10 )
   do this;
else if (num < 20)
  do this instead;
else if( num < 30 )
  do this otherwise;

execution picks up here.


If it finds the number is < 10, it will execute the first if statement and then drop down to the pickup line.

Just a tip to help make your code a little easier to read.

Also, you could indent a single tab to minimize lines wrapping over to the next line.

On to your current issues, this is what I'm getting for warnings/errors:
1
2
3
4
5
6
1>c:\users\roberts\documents\visual studio 2010\projects\testbed\testbed\test.cpp(72): warning C4390: ';' : empty controlled statement found; is this the intent?
1>c:\users\roberts\documents\visual studio 2010\projects\testbed\testbed\test.cpp(83): warning C4806: '==' : unsafe operation: no value of type 'bool' promoted to type 'char' can equal the given constant
1>c:\users\roberts\documents\visual studio 2010\projects\testbed\testbed\test.cpp(89): warning C4806: '==' : unsafe operation: no value of type 'bool' promoted to type 'char' can equal the given constant
1>c:\users\roberts\documents\visual studio 2010\projects\testbed\testbed\test.cpp(95): warning C4806: '==' : unsafe operation: no value of type 'bool' promoted to type 'char' can equal the given constant
1>c:\users\roberts\documents\visual studio 2010\projects\testbed\testbed\test.cpp(110): warning C4806: '==' : unsafe operation: no value of type 'bool' promoted to type 'int' can equal the given constant
1>c:\users\roberts\documents\visual studio 2010\projects\testbed\testbed\test.cpp(117): warning C4806: '==' : unsafe operation: no value of type 'bool' promoted to type 'int' can equal the given constant


 
if (BasePRICE < 0);


It sees that semi-colon as the end of the if statement body, so the code within the brackets is executed regardless of the validity of baseprice.

The rest is a type mismatch. You shouldn't compare a bool to a character and that is probably throwing off the comparisons. A bool is either 0 for false or not 0 for true.
Last edited on
So I assume what you are trying to tell me to do is something like this?
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;

int main()
{
//Variable Declerations
double WEIGHT;
double COST;
double BasePRICE;
double in_outSHIPPING;
double TOTALCOST;
double ASHIP;
bool VALIDdata;
bool STATE;
bool HAZARD;

cout <<"Please enter the base price of the produce and press <ENTER>: ";
cin >> BasePRICE;
if (BasePRICE > 0)
{
	cout << "Please enter the weight of the item to be shipped and press <ENTER>: ";
	cin >> WEIGHT;
		if (WEIGHT > 0 && WEIGHT <= 20)
		COST = 14.95;
	else if (WEIGHT > 21 && WEIGHT <= 49)
		COST = 19.95;
	else if (WEIGHT > 50 && WEIGHT <= 100)
		COST = 29.95;
	else if (WEIGHT > 101 && WEIGHT <= 200)
		COST = 49.95;
	else if (WEIGHT > 201)
		COST = 99.95;
	else if (WEIGHT <= 0)
		cout << "Please enter a valid weight above 0 lbs \n";
		VALIDdata = false;
		system("pause");
		return 0;
	
}
else if (BasePRICE < 0);
	cout <<"Please enter a valid price above $0 \n" ;
	VALIDdata = false;
	system("pause");
	return 0;
	



cout <<"Please enter the hazard code as A, B, or C in uppercase letters: ";
cin >>HAZARD;
if (HAZARD == 'A')
	ASHIP = 0;

else if (HAZARD == 'B')
		ASHIP = 25;
	
	else if (HAZARD == 'C')
		ASHIP = 50;
		
		else
		    cout<<"Please enter a valid answer of A, B, or C in uppercase letters: ";
			VALIDdata = false;
			system("pause");
			return 0;
		
	

cout <<"Will the product be shipping in state? Please answer yes or no in lowercase: ";
cin >> STATE;
if (STATE == 'yes')
	in_outSHIPPING = (BasePRICE * 1.06) - 10;


	else if (STATE == 'no')
		in_outSHIPPING = BasePRICE;
	
	else
		cout<<"Please use a valid answer in all lowercase letters \n";
		VALIDdata = false;
		system("pause");
		return 0;
	

system("pause");

TOTALCOST = COST + ASHIP + in_outSHIPPING;

cout <<"The total cost to purchase and ship your product is $ "<< TOTALCOST<<
system("pause");
return 0;
}


When I try to run it that way, I can get up to the question about the weight of the theoretical product, but then it ends after that. And I run into the problem I faced in the beginning. I am really not trying to be difficult, I have only been learning C++ for a month reading from a book suppose I just cannot seem to grasp what you all are trying to tell me.
1
2
3
4
5
	else if (WEIGHT <= 0)
		cout << "Please enter a valid weight above 0 lbs \n";
		VALIDdata = false;
		system("pause");
		return 0;


I assume you want this entire body to run if WEIGHT <=0, but the problem: because it's not within {}, it runs only the cout << "Please enter a valid weight above 0 lbs \n"; part if WEIGHT <= 0, but it runs the rest of it regardless of any of the conditionals, picking up at:

 
		VALIDdata = false;


And stepping through from there, so it goes on to system("pause") and then return 0;

The {} brackets are scope delimiters. They define an entire body of execution, which is why you see functions delimited by {} brackets. If you want a section of code that is more than one line, limited to a conditional, you need to enclose it within {} brackets:

1
2
3
4
5
6
7
	else if (WEIGHT <= 0)
        {
		cout << "Please enter a valid weight above 0 lbs \n";
		VALIDdata = false;
		system("pause");
		return 0;
        }


Out of curiosity, what compiler are you using?
Last edited on
MS Visual Studio 2010
Using the debugger with VC++ 10 is a good way to see what your program is doing.
Yea, I have been trying to do that. The problem is that I have issues understanding exactly what the problem is in laymen's terms, so I look up the error code and have to decipher the problem to the best of my ability, and I might fix it but I either end up with a syntax error or a mistake in the code itself that screws up the final program when compiled.
And before you say it, no offense, but yes, I know, welcome to programming.
I'm not going to say that at all. I'm an old ansi c hand who really never adapted to C++, but because it's so prevalent, I'm trying to learn it since my game project is rather grand and if I get a team together someday, it's going to have to be in a modern language.
Yea, I managed to get it figured out, but I waned to say thanks. I just need to work out the math better to get the result, but everything is working now, you helped out a lot.
Topic archived. No new replies allowed.