A consise way to get stuff.

Pages: 123
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
/* Usandfriend's code for Concise way to Get Stuff. */
#include "genlib.h"
#include "simpio.h"
#include "strlib.h"
#include <stdio.h>

main()
{
      double SalesTax,price;
      string state;
      int TaxC,i;

      string State [50] = {"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 [57] = {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};

       printf ("What does your item cost?  ");
       price=GetReal();
    
       printf ("What state do you want to calculate tax for?  ");
       state=GetLine();
    
       for (i = 0; i < 50; i++)
       {
            if (StringEqual(State[i],state)==TRUE)
            {
                 TaxC = i;break;
            }
            else {continue;}
       }
             
       SalesTax = price*Tax[TaxC];
             
       printf ("Final Price is %.2f.",price + SalesTax);
    
getchar();
}


Here ya go mate.
Last edited on
There are lots of undefined things in there. line 31 has an error that say 'GetLine' No such file or directory. there is like 4 of them.
Hi, the second code snip that I put up will read the standard input (std::cin) into a std::string stopping when it encounters a newline character. It's a free function found in the <string> header. You can read about it here: http://www.cplusplus.com/reference/string/getline/

I might have misread your post, but did you figure out the first code snip?
@ bleaz (32)

You must be missing all the libraries and must have only <string><iostream> etc... Sorry, never learn't to use those.
Last edited on
@ Luc Lieber

No the first code you gave me isnt working. It doesn't give me the error when I type in something other than what is in the Map. If i type in steve as the state it says the price of it in the state of steve.

This is the 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
    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;
}


and I think i understand the second code.
Last edited on
Hello, i've written the code and it works i runned it, it works great. The code is the following (thanks for the idea of a map it really helped):
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
#include <iostream>
#include <map>
using namespace std;

int main ()
{
    string state1, answer3;
    float price;
    double tax;
    double pricewithtax;
    map<string, double> States;

    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;
    do{
    cout<<"Enter Price: ";
    cin>>price;
    cout<<"Enter the state: ";
    cin>>state1;

    if(States.find(state1) == States.end()){
        cout<<"ERROR! that state does not exist.\n";
    }
    else{
        tax=States.find(state1)->second;
        pricewithtax=price+(price*tax);


    cout<<"The total price of the product (tax included) is: "<<pricewithtax;
    cout<<"\n\n\n\nKeep running? \n1.Yes\n2.No\n";
    cin>>answer3;
    }


}while(answer3 == "Yes" || answer3 == "Y" || answer3 == "keep running" || answer3=="1");
return 0;
}


EDIT: Ask me for any doubt.
Last edited on
It doesn't give me the error when I type in something other than what is in the Map. If i type in steve as the state it says the price of it in the state of steve.


Your problem lies at line 17 when you use the [] operator. I quote from the map documentation (http://cplusplus.com/reference/map/map/operator%5B%5D/)


T& operator[] ( const key_type& x );

Access element

If x matches the key of an element in the container, the function returns a reference to its mapped value. If x does not match the key of any element in the container, the function inserts a new element with that key and returns a reference to its mapped value.


Therefore, to avoid this, only access the element if you're sure that it's in your map. You check this at line 19 in your code, so if your map contains the state, then access it within your else block at line 23.

You've almost got 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
//    TaxMap...
    
    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) );
    
    
    if(TaxMap.find(State) == TaxMap.end())
    {
      cout << "\nState Not Found: " << State << endl;
    }
    else
    {
      Tax = TaxMap[State];
      cout << "\nThe cost of your item in " << State << " is $" << (Tax * Cost) + Cost << "." << endl;
    }
}
Last edited on
Try my code if u write the states with the proper letters u get the result
Im sorry SirSen your code doesn't work like I want mine to work. Here are a few problems with yours.

1. Capitalization of the state has to be the same as it is in the map when typed.
2. States with spaces in the name IE West Virgina
3. when there is an error it doesn't keep running.

I don't however see why your error works and mine doesn't.

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
#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?\n";
    cin >> Cost;
    
    cout << "Enter a state:\n" << flush;
    cin >> State;
    
    transform( State.begin(), State.end(), State.begin(), ptr_fun <int, int> (tolower) );
    
    Tax = TaxMap[State];
    
    if(TaxMap.find(State) == TaxMap.end())
    {
      transform( State.begin(), State.end(), State.begin(), ptr_fun <int, int> (toupper) );
      cout << "State Not Found: " << State << endl;
    }
    else
    {
      transform( State.begin(), State.end(), State.begin(), ptr_fun <int, int> (toupper) );
      cout << "The cost of your item in '" << State << "' is $" << (Tax * Cost) + Cost << ".\n";
    }
    
    getch();
    return 0;
}
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
/* Usandfriend's code for Concise way to Get Stuff. */
#include <iostream>

main()
{
      double SalesTax,price;
      std::string state;
      int TaxC,i;

      std::string State [50] = {"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 [57] = {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};

       std::cout<<"What does your item cost?  ";
       std::cin>>price;
       std::cin.ignore();
    
       std::cout<<"What state do you want to calculate tax for?  ";
       std::cin>>state;
       std::cin.ignore();
    
       for (i = 0; i < 50; i++)
       {
            if (State[i]==state)
            {
                 TaxC = i;
	         break;
            }
       }
             
       SalesTax = price*Tax[TaxC];
             
       std::cout<<"Final Price is: "<<price + SalesTax;
    
std::cin.ignore();
}


Fixed my former code.
Last edited on
That code doesn't work if there are spaces or not capitalized state names either.

Also I figured out why my code didn't recognize when a state wasn't found or not. Line 74 with code Tax = TaxMap[State]; needed to be moved into the else phrase before the computation like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    if(TaxMap.find(State) == TaxMap.end())
    {
      transform( State.begin(), State.end(), State.begin(), ptr_fun <int, int> (toupper) );
      cout << "State Not Found: " << State << endl;
    }
    else
    {
      Tax = TaxMap[State];
      transform( State.begin(), State.end(), State.begin(), ptr_fun <int, int> (toupper) );
      cout << "The cost of your item in '" << State << "' is $" << (Tax * Cost) + Cost << ".\n";
    }
    
    getch();
    return 0;
}


However I still cannot figure out how to make it recognize when I have spaces in the input. Help please?
Last edited on
Don't use std:: cin >> stringInput with strings unless you only want single words. The extraction operator delimits on whitespace. Use std::getline(cin, stringInput);
Now it skips the rest of the code completely.

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
 string State;
     float Tax, Cost;
    
     cout << "How much does your item cost? " << flush;
     cin >> Cost;
    
     cout << "Enter a state: " << flush;
     getline( cin, State);
    
     transform( State.begin(), State.end(), State.begin(), ptr_fun <int, int> (tolower) );
    
     if(TaxMap.find(State) == TaxMap.end())
     {
       transform( State.begin(), State.end(), State.begin(), ptr_fun <int, int> (toupper) );
       cout << "State Not Found: " << State << endl;
     }
     else
     {
       Tax = TaxMap[State];
       transform( State.begin(), State.end(), State.begin(), ptr_fun <int, int> (toupper) );
       cout << "The cost of your item in '" << State << "' is $" << (Tax * Cost) + Cost << ".";
     }

    cout << "\n\n\nKeep running? \n1.Yes\n2.No\n";
    cin >> ReRun;
    }
    while(ReRun == "Yes" || ReRun == "Y" || ReRun=="1" || ReRun == "y");
    return 0;
}
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
/* Usandfriend's code for Concise way to Get Stuff. */
#include <iostream>

main()
{
      double SalesTax,price;
      std::string state;
      int TaxC,i;

      std::string State [50] = {"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 [57] = {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};
	
   	while(true)
	{
		std::cout<<"If you need help on states and how to enter them, please enter \"help\" when you get to states. If you enter something wrong, the program might not work."<<std::endl<<std::endl;
		
       	std::cout<<"What does your item cost?  ";
       	std::cin>>price;
       	std::cin.ignore();
    	

    	while(true)
		{
       		std::cout<<"What state do you want to calculate tax for?  ";
       		std::getline(std::cin,state);
       		if(state=="help")
       		{
            	std::cout<<"Here are the list of states:"<<std::endl;
                for(i=0;i<50;i++)
                {
                    std::cout<<i+1<<". "<<State[i]<<std::endl;
                }
                std::cout<<"An example of putting in a state is: \"West Virginia\"."<<std::endl<<std::endl;
       		}
       		else
			{
				break;
			}
		}
        for (i = 0; i < 50; i++)
        {
            if (State[i]==state)
            {
                TaxC = i;
	            break;
            }
        }
		             
       SalesTax = price*Tax[TaxC];
             
       std::cout<<"Final Price is: "<<price + SalesTax;
	   std::cin.ignore();
	   while(true)
	   {
	        std::cout<<std::endl<<std::endl<<"Would you like to rerun the program (enter \"yes\" or \"no\")?  ";
	   		std::getline(std::cin,state);
	   		if((state=="no"))
	   		{
	             break;
	   		}
	   		else if (state=="yes")
	   		{
	   			 main();
	   		}
	   		else
	   		{
	       		 std::cout<<"Invalid option."<<std::endl;
	   		}
	   		std::cout<<std::endl<<std::endl<<std::endl;
	   }
	    break;
	}
}


And yes, you can use main() recursively (it works on Orwell Dev C++) ;D.
Last edited on
I'm sorry my getline fuction isn't working. whats wrong?

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

using namespace std;

string ReRun;
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;
    
    do 
    {
     string State;
     float Tax, Cost;
    
     cout << "How much does your item cost? " << flush;
     cin >> Cost;
    
     cout << "Enter a state: ";
     getline (cin,State);
    
     transform( State.begin(), State.end(), State.begin(), ptr_fun <int, int> (tolower) );
    
     if(TaxMap.find(State) == TaxMap.end())
     {
       transform( State.begin(), State.end(), State.begin(), ptr_fun <int, int> (toupper) );
       cout << "State Not Found: " << State << endl;
     }
     else
     {
       Tax = TaxMap[State];
       transform( State.begin(), State.end(), State.begin(), ptr_fun <int, int> (toupper) );
       cout << "The cost of your item in '" << State << "' is $" << (Tax * Cost) + Cost << ".";
     }

    cout << "\n\n\nKeep running? \n1.Yes\n2.No\n";
    cin >> ReRun;
    }
    while(ReRun == "Yes" || ReRun == "Y" || ReRun=="1" || ReRun == "y");
    return 0;
}
And yes, you can use main() recursively (it works on Orwell Dev C++) ;D.


No you can't (the standard forbids it, regardless of your anecdotal experience with one IDE.)
I'm sorry my getline fuction isn't working. whats wrong?


It works. You just have a newline in the pipeline from the previous input. The getline encounters the newline and extracts an empty string.

1
2
3
4
5
6
7
     cout << "How much does your item cost? " << flush;
     cin >> Cost;

     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n') ;
    
     cout << "Enter a state: ";
     getline (cin,State);


You'll need to #include <limits> for std::numeric_limits and, you should already be including <functional> for ptr_fun and <algorithm> for transform (but you're not.)
cire wrote:
No you can't (the standard forbids it, regardless of your anecdotal experience with one IDE.)

Well I'm a rebel -_-.

EDIT: Okay, fine, fine, I see what you are trying to say, don't teach the beginners bad habits. Beginners: main() is not to be used recursively. Please disregard my program.
Last edited on
Here is the final code, use a "_" for spaces,
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
#include <iostream>
#include <map>
using namespace std;

int main ()
{
    string state1, answer3;
    float price;
    double tax;
    double pricewithtax;
    map<string, double> States;

    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;
    do{
    cout<<"Enter Price: ";
    cin>>price;
    cout<<"Enter the state: ";
    cin>>state1;

    if(States.find(state1) == States.end()){
        cout<<"ERROR! that state does not exist.\n";
    }
    else{
        tax=States.find(state1)->second;
        pricewithtax=price+(price*tax);


    cout<<"The total price of the product (tax included) is: "<<pricewithtax;
    }
    cout<<"\n\n\n\nKeep running? \n1.Yes\n2.No\n";
    cin>>answer3;


}while(answer3 == "Yes" || answer3 == "Y" || answer3 == "keep running" || answer3=="1");
return 0;
}

EDIT: Changed the part of the code that in case u get an error u have the option to keep running
Last edited on
#include<iostream>
using namespace std;
//===============================================================
int main(){
char State[32];
cout<<"\n Type a string "; cin>>State;
cout<<"\n The string is"<<State<<".\n\n";
return 0;
}

std::cin is a STREAM. Need a buffer to put comming data. char* is a ponter to byte. We need a predefined string to put byte data like State[32] or "hello world...". . If std::cin have more than 32 or string characters the same error hapens. On 64bit systems std::cin may take 2byte characters unicode.
Last edited on
Pages: 123