| bleaz (29) | |||
I am trying to write a code that asks user for the price of an item and then the state that they want to calculate tax for. I know Im going to have to type out all of them but how does the program recognise the text?
| |||
|
|
|||
| SirSen (8) | |||
|
conditional sentences. Its better to use a string for State An example would be:
| |||
|
Last edited on
|
|||
| bleaz (29) | |
| That would be like 50 if statements? Is there a better way to do it? | |
|
|
|
| Hucaru (39) | |||||
Could you not make 2 arrays? 1 array would contain the state names, the other would contain the rate of each state in the same position e.g:
You could then loop to see if the input matches any of the first array. If so, use the same position for the second array which you then use in your calculation.
array2[j] now contains the tax for that state. | |||||
|
|
|||||
| bleaz (29) | ||||
Okay i tried that but i get an error with this code.
and this error
| ||||
|
|
||||
| Alrededor (88) | |
|
As a start: line 21 0.0685, 0.00, 0.07, 0.05125, 0.04,0.0475, 0.05 0.055, 0.045, 0.00, 0.06, 0.07, 0.06,is missing a comma between 0.05 and 0.055 line 16 "Wisconsin, Wyoming" is run together as one string instead of two line 34 The variable input was never declared. Did you intend for it to be std::string input;?line 30 State is an array of const char* so the identifier State used by itself is a pointer to the first element of that array. I think you intended to have cin >> input;I have not looked beyond those errors. | |
|
|
|
| bleaz (29) | ||||
|
Okay I fixed the grammatical errors but I guess I just don't understand arrays and what not very well because i'm not sure what you are saying. I am just trying to do what I was told in the other post before yours. Here is the latest code and errors though.
| ||||
|
|
||||
| Luc Lieber (907) | |||
|
Hello there, As far as I can see, you have problems on lines: 30 -- You're providing your array 'State' to std::cin.operator>>, 34 -- 'input' is not declared anywhere. Errors aside, this looks like a good case for a map (or a multimap) if your problem allows for it. http://www.cplusplus.com/reference/map/
Good luck! | |||
|
Last edited on
|
|||
| Alrededor (88) | |||||||
You can't read the user's state into the State array. On line 34 you are using a variable called input but you never declared it. One simple way to fix this is to add a declaration for a string called input right before line 30. Then replace line 30 with cin >> input; This gives you a place to store the state name the user enters. Replace:cin >> State; by
Now down in the if statement you will actually be comparing the user's state with each state in the array as you loop through the array. You will need to add #include <string> at the top with your other includes.I believe this should fix the basic functionality of the program, however there is another more subtle problem. What happens if the user makes a spelling mistake when they enter the state name? The for loop will execute all 50 times without finding a match and hence line 36 TaxC = i; will never execute. But TaxC was initialized at the top of your program to zero. This means the SalesTax computation line will use index zero which is the sales tax for Alabama of 0.04 which is the wrong thing to do. What you need is some way to know that no match was found in the loop. One possible solution would be to move the declaration of the loop variable i outside of the loop. That way it will still be accessible when the loop ends.
Now after the for loop ends test to see if i is 50 (no match).
| |||||||
|
|
|||||||
| bleaz (29) | |||
I'm not sure what to do to make it work. It doesnt display the error message.
| |||
|
|
|||
| bleaz (29) | |||
I am also trying to make it a map but I am having a hard time understanding it.
| |||
|
Last edited on
|
|||
| Luc Lieber (907) | |||
|
Hello, You might want to consider re-reading the map documentation found at http://cplusplus.com/reference/map/map/ Maps are a kind of associative array where values are accessed through their keys. The map template in the C++ standard library takes two parts: the key type, and the value type. std::map<int, float> // a map whose keys are ints, and whose values are floats or in your case: std::map<std::string, float> // a map whose keys are strings (state names), and whose values are floats (the taxes)
For your problem, you won't have to iterate over your map at all, so if you have any specific questions, let me know. | |||
|
Last edited on
|
|||
| bleaz (29) | |||
I am still getting some errors about the name of the state being "too long for its type"
| |||
|
|
|||
| Luc Lieber (907) | |||
You're getting that warning because you're using single quotes with your state names. Strings use double quotes, characters use single quotes.char a = 'a';
std::string s = "some string";You also need to populate your states map before trying to read from it to achieve your desired output.
Good luck! | |||
|
Last edited on
|
|||
| bleaz (29) | |
| How do I read it in from a file? | |
|
|
|
| Alrededor (88) | ||||||
Testing for i == 50 needs to be done after the for loop has ended. Your test is inside the for loop. i will only be 50 after the for loop has ended and no match was found. Change:
to
| ||||||
|
|
||||||
| bleaz (29) | |||
Now it runs the price with the first state anyway then it redoes the program. It doesnt work when I put the rerun before the code for computing the tax either. | |||
|
|
|||
| bleaz (29) | |||
This code finally works pretty well but is there a way to give an error if the entry doesn't match any of the states?
EDIT: I also just realize that it doesn't work with strings that have spaces like "District of Columbia" is there a way to fix that? | |||
|
Last edited on
|
|||
| Luc Lieber (907) | |||||||
Hello, sorry for the delayed response.
I didn't run these snips through a compiler, so there may be syntax errors. | |||||||
|
Last edited on
|
|||||||
| bleaz (29) | |||
Thank you for the reply and help. But I do not unerstand what the second code you profided will do and It doesnt give any errors but the new code for the first just gives me the cost of whatever i typed and the state in lowercase letters
| |||
|
|
|||