Write an introductory C++ program that displays the appropriate shipping charge based on the region code entered by the user. To be valid, the region code must contain exactly three characters: a letter (either A or B) followed by two numbers. The shipping charge for region A is $25. The shipping charge for region B is $30. Display an appropriate message if the region code is invalid. Use a sentinel value to end the program. Save and then run the program. Test the program using the following region codes: A11, B34, C7, D2A, A3N, C45, and 74TV.
The following is the code I have so far and the error messages I keep getting:
Please use code tags; it preserves indentation and allows running code (once you strip out the VS stuff) in C++-shell.
Note that the changes below simply make your code COMPILE AND RUN. They don't carry out the VALIDATION of your strings (letter, followed by two digits). You are going to have to restructure your code considerably for that. Validation will require:
- string length is 3 - string's .size() member function
- first character is an (upper-case?) character - isalpha(), possibly isupper()
- second and third characters are numeric - isdigit()
Have a look at header <cctype> for isalpha, isupper, isdigit
Error C2228 left of '.find' must have class/struct/union Lancaster_Chp13Ex20 c:\users\jim_l\documents\saint leo\com207 c++\weekly exercises\lancaster_chp13ex20\lancaster_chp13ex20\lancaster_chp13ex20.cpp 19
Error C2653 'string': is not a class or namespace name Lancaster_Chp13Ex20 c:\users\jim_l\documents\saint leo\com207 c++\weekly exercises\lancaster_chp13ex20\lancaster_chp13ex20\lancaster_chp13ex20.cpp 19
Error C2228 left of '.find' must have class/struct/union Lancaster_Chp13Ex20 c:\users\jim_l\documents\saint leo\com207 c++\weekly
Error C2653 'string': is not a class or namespace name Lancaster_Chp13Ex20 c:\users\jim_l\documents\saint leo\com207 c++\weekly exercises\lancaster_chp13ex20\lancaster_chp13ex20\lancaster_chp13ex20.cpp 24
Try my code in C++-shell (the little cog-wheel item at the top RH corner of the code sample).
I had to strip the (non-C++-standard) Visual Studio header out to run with my compiler. You may have to reinstate it, I suppose.
You appear not to have access to <string> or anything in <iostream>. Have you cut and pasted the whole code ... INCLUDING THE HEADERS? Is your environment wrongly set up so that it doesn't find those headers?
When I copy and paste it exactly, I get the following error:
Severity Code Description Project File Line Suppression State
Error C1010 unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source? Lancaster_Chp13Ex20 c:\users\jim_l\documents\saint leo\com207 c++\weekly exercises\lancaster_chp13ex20\lancaster_chp13ex20\lancaster_chp13ex20.cpp 37
But then when I add #include "stdafx.h", it gives me all those errors that I listed above. I feel like it has me running in circles.
Oh, and yes, your code works perfectly in C++ shell, every time.
I don't use Visual Studio - someone else would be better advising you. I have a vague feeling that stdafx.h might have to be the FIRST include - but I could be completely wrong, I'm afraid.