strings

Pages: 12
I'm starting to get confused on what is needed.

I'm trying to write a program that displays an appropriate shipping charge based on zip code entered by the user.
To be valid the ZIP code must contain exactly 5 digits and the first three digits must be “605”, or “606”. The shipping charge for the “605” ZIP code is $25.00. The shipping charge for the “606” ZIP code is $30.00.

Here is what I have to this point ....


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

	const string VALID_MSG	= "Valid Length" ;
	const string INVALID_MSG = "Invalid Length" ;
	const string ALL_NUMBERS = "All numbers" ;
        const string NOT_ALL_NUMBERS = "Not all numbers" ;
	string zipCode	= "";
	char foundNonNumber = 'N' ;
	double shippingCharge;



	cout << "Five-character ZIP code (-1 to end): ";
	cin >> zipCode:

	while (zipCode != "-1")
	{
	   if (zipCode.length() == 5)
	   {
	     cout << VALID_MSG << endl;
		
		//determine charge per zipcode
		if zipCode = 606 << shippingCharge = 25.00
		 cout << shippingCharge
		
		else zipCode = 605 << shippingCharge = 30.00
		
                 
	         //end if

	     }
	     else
             cout << INVALID_MSG << endl:
	
	     //end if

	} end while
	




 system("pause");

 return 0;       

Dude you could just have them enter the zip code regularly like 60648 and then do a substr(0,2) or substr(0,3) not sure which to get the string '606' or '605'. thats how i would do it.
I'm new to programming so I'm not real familiar... what would that look like in the code?
the other part is I want to be sure the shipping charge display, displays correctly
if (zipCode.substr(0,3) == "605")



Like I said for the substr you might need if (zipCode.substr(0,2) == "605")

I'm not familiar with this statement and not understanding what you are trying to do.

if zipCode = 606 << shippingCharge = 25.00

so I guess I don't really understand your second question, I might be a little to new to C++ to know the answer
Last edited on
I'm trying to equate a zipcode that starts with "606"to have a shipping charge of $25.00 and
equate a zipcode that starts with "605" to have a shipping charge of $30.00.

make sense?

he's a little update but still getting errors aroung the shipping charges

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

while (zipCode != "-1")
	{
	   if (zipCode.length() == 5)
	   {
	     cout << VALID_MSG << endl;
		
		//determine charge per zipcode
		if zipCode = 606 << shippingCharge = 25.00
		 cout << "shipping charge: ";
		
		else zipCode = 605 << shippingCharge = 30.00
		  cout << "shipping charge: ";
                 
	         //end if

	     }
	     else
             cout << INVALID_MSG << endl:
	
	     //end if

	} end while
It should somehow read if the zip starts with "606"" then $25.00 charge
then..

1
2
3
4
5
6
7
8
9
10
if zipCode.substr(0,3) == "606" {
     shippingCharge=25.00
     cout << "shipping charge: " << shippingCharge
}else if {

   look at code for "606"  

}else{
 invalid message
}
ok ... here is the full code.
I have several syntax errors and cannot resolve them.

One question .... with this will it only accept the first three numbers of the zipcode as 605 or 606? .... this is a requirement

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


#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

   
 
int main()
{
	// declare 
	
	const string VALID_MSG	= "Valid Length" ;
	const string INVALID_MSG = "Invalid Length" ;
	const string ALL_NUMBERS = "All numbers" ;
    const string NOT_ALL_NUMBERS = "Not all numbers" ;
	double string zipCode = "";
	double shippingCharge;



	cout << "Five-character ZIP code (-1 to end): ";
	cin >> zipCode;

	while (zipCode != "-1")
	{
	   if (zipCode.length() == 5)
	   {
	     cout << VALID_MSG << endl;
		
		//determine charge per zipcode

		if zipCode.substr(0,3) == "606" {shippingCharge=25.00
     	cout << "shipping charge: " << shippingCharge;
		else if 
		zipCode.substr(0,3) == "605" {shippingCharge=30.00
     	cout << "shipping charge: " << shippingCharge;
                    
                 
	         //end if

	     }
	     else
             cout << INVALID_MSG << endl:
	
	     //end if

	} end while




 system("pause");

 return 0;       


}  //end of main function

no it will accept anything as the first 3 integers, if its a requirement why don't you just have a menu selection where they can choose what their zip code starts with ie( 1. 606, 2. 605, 3. other) ((you would probably do nested if statments for this like if(selection==1){body} else if(selection==2){body} else{quit program})) then if they choose other ie 3 then quit the program. if they choose 1 then go from there and if they choose 2 go from there.

if you want to stick with that you got your on the right track with your code your errors are with the if else if.

if you are not going to have a else that goes with the if else if then I would just make the else if an else.

Here is the code for your first if you can do the 2nd

1
2
3
4
5
6
if(zipCode.substr(0,3) == "606")
 {
    shippingCharge = 25.00;
    cout << "shipping charge: " << shippingCharge << endl;
 }


If the zip code thing is a requirement just make an else after the if else if and have an error message saying "your zip code does not start with the required 606 or 605." or something if its a big deal
Last edited on
I've updated but
I keep getting these syntax errors ....

error C2146: syntax error : missing ';' before identifier 'zipCode'
error C2065: 'zipCode' : undeclared identifier



also ... where would the (selection ==1) if statements be nested ... where at within the if's?
error C2146: syntax error : missing ';' before identifier 'zipCode'
error C2065: 'zipCode' : undeclared identifier

for the first find where you don't have a semi colon.....
for the 2nd you don't declare zipCode apparently both of those are easy fixes.

I'd say for the selections just keep going with what you are doing.
I'm still getting a syntax error but don't see the problem.

On the line shippingCharge = 30.00; ... it says I'm missing a ; but I'm not

1
2
3
4
5
6
}
		
		 else(zipCode.substr(0,3) == "605")
		
		shippingCharge = 30.00;
     		cout << "shipping charge: " << shippingCharge << endl;

else(zipCode.substr(0,3) == "605")

This isn't valid. You need an if in there if you want to put a condition on the else.
that was just the section with the error...
here is more

where do you see the issue?
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

while (zipCode != "-1")
   {
	   if (zipCode.length() == 5)
      {
	     cout << VALID_MSG << endl;
		
		//determine charge per zipcode

		  if(zipCode.substr(0,3) == "606")
		{
			shippingCharge = 25.00;
			cout << "shipping charge: " << shippingCharge << endl;
		}
		
		  else(zipCode.substr(0,3) == "605")
		
			shippingCharge = 30.00;
     		cout << "shipping charge: " << shippingCharge << endl;
		            
               
	      //end if
      }
	     else
             cout << INVALID_MSG << endl;
	
	     //end if

   } //end while

Line 16.
ok ... the errors are corrected... BUT
it just continues to loop and I have to ctrl-c to break out.

what am I missing

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
int main()
{
	// declare 
	
	const string VALID_MSG	= "Valid Length" ;
	const string INVALID_MSG = "Invalid Length" ;
	string zipCode		= "" ;
	double shippingCharge;



	cout << "Five-character ZIP code (-1 to end): ";
	cin >> zipCode;

	while (zipCode != "-1")
   {
	   if (zipCode.length() == 5)
      {
	     cout << VALID_MSG << endl;
		
		//determine charge per zipcode

		  if(zipCode.substr(0,3) == "606")
		{
			shippingCharge = 25.00;
			cout << "shipping charge: " << shippingCharge << endl;
		}
		
		  else(zipCode.substr(0,3) == "605");
		
			shippingCharge = 30.00;
     		cout << "shipping charge: " << shippingCharge << endl;
		            
               
	      //end if
      }
	     else
             cout << INVALID_MSG << endl;
	
	     //end if

   } //end while




 system("pause");

 return 0;       


}  //end of main function 
plus the program isn't working correctly ... when I type in a 603** area code it accepts it instead of saying invalid
I'm getting frustrated
Line 29: Else-if and you have a semi-colon that shouldn't be there. Furthermore, you're missing some curly brackets. That said, you'll also need an else block to deal with invalid area codes around that area. :)

-Albatross
Last edited on
when that semicolon wasn't there I kept getting errors saying I needed a semicolon .... uggh.

What would you suggest for the else block to deal with invalid area codes?
Pages: 12