A consise way to get stuff.

Pages: 123
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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
#include<conio.h>
using namespace std;

float Price = 0.00, SalesTax = 0.00;
double TaxUT = 0.06, TaxAB = 0.06;
char State;

int main(void)
{
    cout << "What does your item cost?" << endl;
    cin >> Price;
    
    cout << "What state do you want to calculate tax for?" << endl;
    cin >> State;
             
    SalesTax = Price * TaxUT;
             
    cout << "This is your final price with tax " << Price + SalesTax << "." << endl;
    
    getch();
}
conditional sentences. Its better to use a string for State
An example would be:

1
2
3
if(State== "Illinois"){
TaxUT= (random price)
}
Last edited on
That would be like 50 if statements? Is there a better way to do it?
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:

1
2
3
4

array1 = state 1, state 2, state 3, ....
array2 = state 1 tax, state 2 tax, state 3 tax, ....


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.

1
2
3
4
5
6
7
8
9
10
11

int j = 0;

for (int i = 0; i < 50; i++)
{
      if (array1[i] == input)
        {
           j = i;
        }
}


array2[j] now contains the tax for that state.
Okay i tried that but i get an error with this 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
#include<iostream>
#include<conio.h>
using namespace std;

float Price = 0.00, SalesTax = 0.00;
double TaxUT = 0.06;
int TaxC = 0;

const char* State [] = {"Alabama", "Alaska", "Arizona", "Arkansas", "California",
"Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho",
"Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland",
"Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana",
"Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York",
"North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania",
"Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah",
"Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin, Wyoming"};

const double Tax [] = {0.04, 0.00, 0.066, 0.06, 0.0725, 0.029, 0.0635, 0.00, 0.06,
0.04, 0.04, 0.06, 0.0625, 0.07, 0.06, 0.063, 0.06, 0.04, 0.05, 0.06, 0.0625, 0.06,
0.06875, 0.07, 0.04225, 0.00, 0.055, 0.0685, 0.00, 0.07, 0.04225, 0.00, 0.055,
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,
0.04, 0.07, 0.0625, 0.0595, 0.06, 0.05,0.065, 0.06, 0.05, 0.04, 0.06};

int main(void)
{
    cout << "What does your item cost?" << endl;
    cin >> Price;
    
    cout << "What state do you want to calculate tax for?" << endl;
    cin >> State;
    
    for (int i = 0; i < 50; i++)
    {
      if (State[i] == input)
        {
           TaxC = i;
        }
    }
             
    SalesTax = Price * Tax[TaxC];
             
    cout << "This is your final price with tax " << Price + SalesTax << "." << endl;
    
    getch();
}


and this error

16 C:\Programs\Sales Tax.cpp expected `}' before numeric constant
16 C:\Programs\Sales Tax.cpp expected `,' or `;' before numeric constant
17 C:\Programs\Sales Tax.cpp expected declaration before '}' token
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.
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.

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
#include<iostream>
#include<conio.h>
using namespace std;

float Price = 0.00, SalesTax = 0.00;
double TaxUT = 0.06;
int TaxC = 0;

const char* State [] = {"Alabama", "Alaska", "Arizona", "Arkansas", "California",
"Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho",
"Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland",
"Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana",
"Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York",
"North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania",
"Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah",
"Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"};

double Tax [] = {0.04, 0.00, 0.066, 0.06, 0.0725, 0.029, 0.0635, 0.00, 0.06,
0.04, 0.04, 0.06, 0.0625, 0.07, 0.06, 0.063, 0.06, 0.04, 0.05, 0.06, 0.0625, 0.06,
0.06875, 0.07, 0.04225, 0.00, 0.055, 0.0685, 0.00, 0.07, 0.04225, 0.00, 0.055,
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,
0.04, 0.07, 0.0625, 0.0595, 0.06, 0.05,0.065, 0.06, 0.05, 0.04, 0.06};

int main(void)
{
    cout << "What does your item cost?" << endl;
    cin >> Price;
    
    cout << "What state do you want to calculate tax for?" << endl;
    cin >> State;
    
    for (int i = 0; i < 50; i++)
    {
      if (State[i] == input)
        {
           TaxC = i;
        }
    }
             
    SalesTax = Price * Tax[TaxC];
             
    cout << "This is your final price with tax " << Price + SalesTax << "." << endl;
    
    getch();
}


In functions 'int main()'
30 no match for 'operator>>' in 'std::cin>>State'

closed account (3hM2Nwbp)
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/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <map>

std::map<std::string, float> taxes
{
    std::make_pair("Alabama", 0.04),
    std::make_pair("Alaska", 0.00)
};

int main()
{
    /// print out a single tax
    std::cout << "Alabama's sales tax is: " << taxes["Alabama"] << std::endl;
  
    /// print out all taxes
    for(std::map<std::string, float>::const_iterator iter = taxes.begin(); iter != taxes.end(); ++iter)
    {
        std::cout << iter->first << "'s sales tax is: " << iter->second << std::endl;
    }

}


Good luck!
Last edited on
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

1
2
string input;
cin >> input;


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.

1
2
int i = 0;
for (i = 0; i < 50; i++)


Now after the for loop ends test to see if i is 50 (no match).

1
2
3
4
5
6
7
8
9
10
11
12
if ( i == 50 )
{
   // display an error message here and ask the user to rerun the program
}
else
{
      SalesTax = Price * Tax[TaxC];
             
       cout << "This is your final price with tax " << Price + SalesTax << "." << endl;
}

getch();

I'm not sure what to do to make it work. It doesnt display the error message.

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
#include<iostream>
#include<conio.h>
#include <string>
#include <map>
using namespace std;

float Price = 0.00, SalesTax = 0.00;
double TaxUT = 0.06;
int TaxC = 0;
char ReRun;

const char* State [] = {"Alabama", "Alaska", "Arizona", "Arkansas", "California",
"Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho",
"Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland",
"Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana",
"Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York",
"North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania",
"Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah",
"Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"};

double Tax [] = {0.04, 0.00, 0.066, 0.06, 0.0725, 0.029, 0.0635, 0.00, 0.06,
0.04, 0.04, 0.06, 0.0625, 0.07, 0.06, 0.063, 0.06, 0.04, 0.05, 0.06, 0.0625, 0.06,
0.06875, 0.07, 0.04225, 0.00, 0.055, 0.0685, 0.00, 0.07, 0.04225, 0.00, 0.055,
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,
0.04, 0.07, 0.0625, 0.0595, 0.06, 0.05,0.065, 0.06, 0.05, 0.04, 0.06};

int main(void)
{
    do
    {
      cout << "What does your item cost?" << endl;
      cin >> Price;
    
      string StateInput;
    
      cout << "What state do you want to calculate tax for?" << endl;
      cin >> StateInput;
    
      int i = 0;
    
      for (i = 0; i < 50; i++)
      {
        if (State[i] == StateInput)
        {
          TaxC = i;
        }
        else if (i == 50)
        {
          cout << "Sorry you must have spelled the state name incorrectly." << endl;
          cout << "Do you want to rerun the program?" << endl;
          cin >> ReRun;
        }
      }
      SalesTax = Price * Tax[TaxC];
             
      cout << "This is the price of your item in " << StateInput << ":" << endl << "$" << Price + SalesTax << "." << endl;
    
      getch();
    }
    while (ReRun == 'y' || ReRun == 'Y');
             

}
I am also trying to make it a map but I am having a hard time understanding it.

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
// map::begin/end
#include <iostream>
#include <map>
using namespace std;

int main ()
{
    map<char,string> States;
    map<char,int>::iterator it;

    States 	['Alabama'             ] =  0.0400;
    States 	['Alaska'              ] =  0.0000;
    States 	['Arizona'             ] =  0.0660;
    States 	['Arkansas'            ] =  0.0600;
    States 	['California'          ] = 	0.0625;
    States 	['Colorado'            ] = 	0.0290;
    States 	['Connecticut'         ] = 	0.0635;
    States 	['Delaware'            ] = 	0.0000;
    States 	['District of Columbia'] = 	0.0600;
    States 	['Florida'             ] = 	0.0600;
    States 	['Georgia'             ] = 	0.0400;
    States 	['Hawaii'              ] = 	0.0400;
    States 	['Idaho'               ] = 	0.0600;
    States 	['Illinois'            ] = 	0.0625;
    States 	['Indiana'             ] = 	0.0700;
    States 	['Iowa'                ] = 	0.0600;
    States 	['Kansas'              ] = 	0.0630;
    States 	['Kentucky'            ] = 	0.0600;
    States 	['Louisiana'           ] = 	0.0400;
    States 	['Maine'               ] = 	0.0500;
    States 	['Maryland'            ] = 	0.0600;
    States 	['Massachusetts'       ] = 	0.0625;
    States 	['Michigan'            ] = 	0.0600;
    States 	['Minnesota'           ] = 	0.0688;
    States 	['Mississippi'         ] = 	0.0700;
    States 	['Missouri'            ] = 	0.0423;
    States 	['Montana'             ] = 	0.0000;
    States 	['Nebraska'            ] = 	0.0550;
    States 	['Nevada'              ] = 	0.0685;
    States 	['New Hampshire'       ] = 	0.0000;
    States 	['New Jersey'          ] = 	0.0700;
    States 	['New Mexico'          ] = 	0.0513;
    States 	['New York'            ] = 	0.0400;
    States 	['North Carolina'      ] = 	0.0475;
    States 	['North Dakota'        ] = 	0.0500;
    States 	['Ohio'                ] = 	0.0550;
    States 	['Oklahoma'            ] = 	0.0450;
    States 	['Oregon'              ] = 	0.0000;
    States 	['Pennsylvania'        ] = 	0.0600;
    States 	['Rhode Island'        ] = 	0.0700;
    States 	['South Carolina'      ] = 	0.0600;
    States 	['South Dakota'        ] = 	0.0400;
    States 	['Tennessee'           ] = 	0.0700;
    States 	['Texas'               ] = 	0.0625;
    States 	['Utah'                ] = 	0.0470;
    States 	['Vermont'             ] = 	0.0600;
    States 	['Virginia'            ] = 	0.0400;
    States 	['Washington'          ] = 	0.0650;
    States 	['West Virginia'       ] = 	0.0600;
    States 	['Wisconsin'           ] = 	0.0500;
    States 	['Wyoming'             ] = 	0.0400;
    
    for ( it=mymap.begin() ; it != mymap.end(); it++ )
    cout << (*it).first << " => " << (*it).second << endl;

    return 0;
}
Last edited on
closed account (3hM2Nwbp)
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)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <map>
#include <string>

std::map<std::string, float> taxMap;

int main()
{
    std::string input;
    float tax;

    std::cout << "Enter a state: " << std::flush;
    std::cin >> input;
    tax = taxMap[input];
    std::cout << "\nThe tax for " << input << " is " << tax << std::endl;

}


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
I am still getting some errors about the name of the state being "too long for its type"

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
// map::begin/end
#include <iostream>
#include <map>
#include <string>
using namespace std;

map <string, float> TaxMap;

int main ()
{
    string States;
    float Tax;
    
    cout << "Enter a state: " << flush;
    cin >> States;
    
    Tax = TaxMap[States];
    
    cout << "\nThe Tax for " << States << " is " << Tax << endl;

    States 	['Alabama'             ] =  0.0400;
    States 	['Alaska'              ] =  0.0000;
    States 	['Arizona'             ] =  0.0660;
    States 	['Arkansas'            ] =  0.0600;
    States 	['California'          ] = 	0.0625;
    States 	['Colorado'            ] = 	0.0290;
    States 	['Connecticut'         ] = 	0.0635;
    States 	['Delaware'            ] = 	0.0000;
    States 	['District of Columbia'] = 	0.0600;
    States 	['Florida'             ] = 	0.0600;
    States 	['Georgia'             ] = 	0.0400;
    States 	['Hawaii'              ] = 	0.0400;
    States 	['Idaho'               ] = 	0.0600;
    States 	['Illinois'            ] = 	0.0625;
    States 	['Indiana'             ] = 	0.0700;
    States 	['Iowa'                ] = 	0.0600;
    States 	['Kansas'              ] = 	0.0630;
    States 	['Kentucky'            ] = 	0.0600;
    States 	['Louisiana'           ] = 	0.0400;
    States 	['Maine'               ] = 	0.0500;
    States 	['Maryland'            ] = 	0.0600;
    States 	['Massachusetts'       ] = 	0.0625;
    States 	['Michigan'            ] = 	0.0600;
    States 	['Minnesota'           ] = 	0.0688;
    States 	['Mississippi'         ] = 	0.0700;
    States 	['Missouri'            ] = 	0.0423;
    States 	['Montana'             ] = 	0.0000;
    States 	['Nebraska'            ] = 	0.0550;
    States 	['Nevada'              ] = 	0.0685;
    States 	['New Hampshire'       ] = 	0.0000;
    States 	['New Jersey'          ] = 	0.0700;
    States 	['New Mexico'          ] = 	0.0513;
    States 	['New York'            ] = 	0.0400;
    States 	['North Carolina'      ] = 	0.0475;
    States 	['North Dakota'        ] = 	0.0500;
    States 	['Ohio'                ] = 	0.0550;
    States 	['Oklahoma'            ] = 	0.0450;
    States 	['Oregon'              ] = 	0.0000;
    States 	['Pennsylvania'        ] = 	0.0600;
    States 	['Rhode Island'        ] = 	0.0700;
    States 	['South Carolina'      ] = 	0.0600;
    States 	['South Dakota'        ] = 	0.0400;
    States 	['Tennessee'           ] = 	0.0700;
    States 	['Texas'               ] = 	0.0625;
    States 	['Utah'                ] = 	0.0470;
    States 	['Vermont'             ] = 	0.0600;
    States 	['Virginia'            ] = 	0.0400;
    States 	['Washington'          ] = 	0.0650;
    States 	['West Virginia'       ] = 	0.0600;
    States 	['Wisconsin'           ] = 	0.0500;
    States 	['Wyoming'             ] = 	0.0400;

    return 0;
}

closed account (3hM2Nwbp)
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.

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
// map::begin/end
#include <iostream>
#include <map>
#include <string>
using namespace std;

map <string, float> TaxMap;

int main ()
{
    
    /// populate your map using double quotes on the state
    TaxMap["Alabama"             ] =  0.0400;
    /// the rest of the states...
    /// or better yet, you could read the states in from a file!
    
    
    string State;
    float Tax;
    
    cout << "Enter a state: " << flush;
    cin >> State;
    
    Tax = TaxMap[State];
    
    cout << "\nThe Tax for " << State << " is " << Tax << endl;

}


Good luck!
Last edited on
How do I read it in from a file?
I'm not sure what to do to make it work. It doesnt display the error message.


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:
1
2
3
4
5
6
7
8
9
10
11
12
13
      for (i = 0; i < 50; i++)
      {
        if (State[i] == StateInput)
        {
          TaxC = i;
        }
        else if (i == 50)
        {
          cout << "Sorry you must have spelled the state name incorrectly." << endl;
          cout << "Do you want to rerun the program?" << endl;
          cin >> ReRun;
        }
      }


to

1
2
3
4
5
6
7
8
9
10
11
12
13
      for (i = 0; i < 50; i++)
      {
        if (State[i] == StateInput)
        {
          TaxC = i;
        }
      }
      if (i == 50)
      {
          cout << "Sorry you must have spelled the state name incorrectly." << endl;
          cout << "Do you want to rerun the program?" << endl;
          cin >> ReRun;
      }
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
#include<iostream>
#include<conio.h>
#include <string>
#include <map>
using namespace std;

float Price = 0.00, SalesTax = 0.00;
double TaxUT = 0.06;
int TaxC = 0;
char ReRun;

const char* State [] = {"Alabama", "Alaska", "Arizona", "Arkansas", "California",
"Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho",
"Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland",
"Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana",
"Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York",
"North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania",
"Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah",
"Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"};

double Tax [] = {0.04, 0.00, 0.066, 0.06, 0.0725, 0.029, 0.0635, 0.00, 0.06,
0.04, 0.04, 0.06, 0.0625, 0.07, 0.06, 0.063, 0.06, 0.04, 0.05, 0.06, 0.0625, 0.06,
0.06875, 0.07, 0.04225, 0.00, 0.055, 0.0685, 0.00, 0.07, 0.04225, 0.00, 0.055,
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,
0.04, 0.07, 0.0625, 0.0595, 0.06, 0.05,0.065, 0.06, 0.05, 0.04, 0.06};

int main(void)
{
    do
    {
      cout << "What does your item cost?" << endl;
      cin >> Price;
    
      string StateInput;
    
      cout << "What state do you want to calculate tax for?" << endl;
      cin >> StateInput;
    
      int i = 0;
    
      for (i = 0; i < 50; i++)
      {
        if (State[i] == StateInput)
        {
          TaxC = i;
        }
      }
      if (i == 50)
      {
          cout << "Sorry you must have spelled the state name incorrectly." << endl;
          cout << "Do you want to rerun the program?" << endl;
          cin >> ReRun;
      }
      
      SalesTax = Price * Tax[TaxC];
             
      cout << "This is the price of your item in " << StateInput << ":" << endl << "$" << Price + SalesTax << "." << endl;
    
      getch();
    }
    while (ReRun == 'y' || ReRun == 'Y');
    
}


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.
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?

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
#include <iostream>
#include<conio.h>
#include <map>
#include <string>
using namespace std;

map <string, float> TaxMap;

int main ()
{   
    TaxMap 	["alabama"             ] =      0.0400;
    TaxMap 	["alaska"              ] =      0.0000;
    TaxMap 	["arizona"             ] =      0.0660;
    TaxMap 	["arkansas"            ] =      0.0600;
    TaxMap 	["california"          ] = 	0.0625;
    TaxMap 	["colorado"            ] = 	0.0290;
    TaxMap 	["connecticut"         ] = 	0.0635;
    TaxMap 	["delaware"            ] = 	0.0000;
    TaxMap 	["district of columbia"] = 	0.0600;
    TaxMap 	["florida"             ] = 	0.0600;
    TaxMap 	["georgia"             ] = 	0.0400;
    TaxMap 	["hawaii"              ] = 	0.0400;
    TaxMap 	["idaho"               ] = 	0.0600;
    TaxMap 	["illinois"            ] = 	0.0625;
    TaxMap 	["indiana"             ] = 	0.0700;
    TaxMap 	["iowa"                ] = 	0.0600;
    TaxMap 	["kansas"              ] = 	0.0630;
    TaxMap 	["kentucky"            ] = 	0.0600;
    TaxMap 	["louisiana"           ] = 	0.0400;
    TaxMap 	["maine"               ] = 	0.0500;
    TaxMap 	["maryland"            ] = 	0.0600;
    TaxMap 	["massachusetts"       ] = 	0.0625;
    TaxMap 	["michigan"            ] = 	0.0600;
    TaxMap 	["minnesota"           ] = 	0.0688;
    TaxMap 	["mississippi"         ] = 	0.0700;
    TaxMap 	["missouri"            ] = 	0.0423;
    TaxMap 	["montana"             ] = 	0.0000;
    TaxMap 	["nebraska"            ] = 	0.0550;
    TaxMap 	["nevada"              ] = 	0.0685;
    TaxMap 	["new hampshire"       ] = 	0.0000;
    TaxMap 	["new jersey"          ] = 	0.0700;
    TaxMap 	["new mexico"          ] = 	0.0513;
    TaxMap 	["new york"            ] = 	0.0400;
    TaxMap 	["north carolina"      ] = 	0.0475;
    TaxMap 	["north dakota"        ] = 	0.0500;
    TaxMap 	["ohio"                ] = 	0.0550;
    TaxMap 	["oklahoma"            ] = 	0.0450;
    TaxMap 	["oregon"              ] = 	0.0000;
    TaxMap 	["pennsylvania"        ] = 	0.0600;
    TaxMap 	["rhode island"        ] = 	0.0700;
    TaxMap 	["south carolina"      ] = 	0.0600;
    TaxMap 	["south dakota"        ] = 	0.0400;
    TaxMap 	["tennessee"           ] = 	0.0700;
    TaxMap 	["texas"               ] = 	0.0625;
    TaxMap 	["utah"                ] = 	0.0470;
    TaxMap 	["vermont"             ] = 	0.0600;
    TaxMap 	["virginia"            ] = 	0.0400;
    TaxMap 	["washington"          ] = 	0.0650;
    TaxMap 	["west virginia"       ] = 	0.0600;
    TaxMap 	["wisconsin"           ] = 	0.0500;
    TaxMap 	["wyoming"             ] = 	0.0400;
    
    string State;
    float Tax, Cost;
    
    cout << "How much does your item cost?" << endl;
    cin >> Cost;
    
    cout << "Enter a state: " << flush;
    cin >> State;
    
    transform( State.begin(), State.end(), State.begin(), ptr_fun <int, int> ( tolower ) );
    
    Tax = TaxMap[State];
    
    cout << "\nThe cost of your item in " << State << " is $" << (Tax * Cost) + Cost << "." << endl;
    
    getch();
    return 0;
}


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
closed account (3hM2Nwbp)
Hello, sorry for the delayed response.

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?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <map>

std::map<std::string, float> taxes;

int main()
{
  std::string state;
  std::cout << "State Name: " << std::flush;
  std::cin >> state;

  // If the map does not contain the provided state
  if(taxes.find(state) == taxes.end())
  {
     std::cout << "\nState Not Found: " << state << std::endl;
  }
  else // if the map did contain the state
  {
    
  }
}


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?


1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>

int main()
{
  std::string input;
  std::cout << "Type something with spaces: " << std::flush;
  std::getline(std::cin, input);
  std::cout << "\nYou typed: " << input << std::endl;
}


I didn't run these snips through a compiler, so there may be syntax errors.
Last edited on
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

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
    TaxMap 	["virginia"            ] = 	0.0400;
    TaxMap 	["washington"          ] = 	0.0650;
    TaxMap 	["west virginia"       ] = 	0.0600;
    TaxMap 	["wisconsin"           ] = 	0.0500;
    TaxMap 	["wyoming"             ] = 	0.0400;
    
    string State;
    float Tax, Cost;
    
    cout << "How much does your item cost?" << endl;
    cin >> Cost;
    
    cout << "Enter a state: " << flush;
    cin >> State;
    
    transform( State.begin(), State.end(), State.begin(), ptr_fun <int, int> ( tolower ) );
    
    Tax = TaxMap[State];
    
    if(TaxMap.find(State) == TaxMap.end())
    {
      cout << "\nState Not Found: " << State << endl;
    }
    else
    {
      cout << "\nThe cost of your item in " << State << " is $" << (Tax * Cost) + Cost << "." << endl;
    }
    
    getch();
    return 0;
}
Pages: 123