issue with using names variables rather than numbers...

I wrote my program to help people assign service tickets out to techs in my IT department at work. The IT manager would rather have it work off the names rather than the numbers.

In other words he wants to be able to enter wls as the OU rather than 101. He wants to be able to enter Denver rather than the number 1.

I'm brain farting on how to do that in C++. Any help is appreciated.

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

int main()
{
//variables
	int texas =1, denver=1, midland=1, dallas=1, houston=1, Oklahoma = 2, Kansas = 2;
	int wsl=101, ucb=102, ucs=103, wls=104;
	int ou;
	int area;

//statement
	cout << "Please enter the ticket OU: "<<endl;
	cin >> ou;
	cout <<endl;
	cout << "Please enter the city the ticket originates from: " <<endl;
	cin >> area;
	cout << endl << endl;

//decision loop
	while (true)
	{
		if (ou == 101 && area == 1 )
			{
			cout << "Your tech is Michael McDonald."<<endl;
			break;
			}
		
		if (ou == 102 && area == 1)
			{
			cout << "Your tech is Michael McDonald." <<endl;
			break;
			}

		if (ou == 103 && area == 1)
			{
			cout << "Your tech is Michael McDonald." <<endl;
			break;
			}

		if (ou == 104 && area == 1)
			{
			cout << "Your tech is Michael McDonald." <<endl;
			break;
			}
	}

//end of program
	system ("pause");
	return 0;
}
declare vector of strings instead of
1
2
int texas =1, denver=1, midland=1, dallas=1, houston=1, Oklahoma = 2, Kansas = 2;
int wsl=101, ucb=102, ucs=103, wls=104;


replace
1
2
int ou;
int area;


by
1
2
std::string ou;
std::string area;


loop through these vectors and use compare function of std::string

instead of the if (ou == 101 && area == 1 ) statements

On side note, It is good idea to declare each variable on separate lines.
Example of ability to enter either numeric code or OU/area name with conversion to numeric code (or exception if there is no valid stuff). I left decision logic same, just fixed potentional infinite loop and some other problems.
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
#include <algorithm>
#include <cctype>
#include <iostream>
#include <map>
#include <stdexcept>
#include <string>

int get_OU(std::string in)
{
    static const std::map<std::string, int> OU {
        {"wsl", 101}, {"ucb", 102}, {"ucs", 103}, {"wls", 104},
    };
    std::transform(in.begin(), in.end(), in.begin(), ::tolower);
    {
        auto it = OU.find(in);
        if(it != OU.end())
            return it->second;
    }
    if(all_of(in.begin(), in.end(), ::isdigit))
        return std::stoi(in);
    throw std::invalid_argument("Bad OU string");
}

int get_area(std::string in)
{
    static const std::map<std::string, int> area {
        {"texas", 1}, {"denver", 1}, {"midland", 1}, {"dallas", 1}, {"houston", 1},
        {"oklahoma", 2}, {"kansas", 2},
    };
    std::transform(in.begin(), in.end(), in.begin(), ::tolower);
    {
        auto it = area.find(in);
        if(it != area.end())
            return it->second;
    }
    if(all_of(in.begin(), in.end(), ::isdigit))
        return std::stoi(in);
    throw std::invalid_argument("Bad AREA string");
}

int main()
{
    try {
        std::string temp;
        std::cout << "Please enter the ticket OU:\n";
        std::cin >> temp;
        int ou = get_OU(std::move(temp));
        std::cout << "\nPlease enter the city the ticket originates from:\n";
        std::cin >> temp;
        int area = get_area(std::move(temp));
        std::cout << "\n\n";

        if (ou == 101 && area == 1 ) {
            std::cout << "Your tech is Michael McDonald.\n";
        } else if (ou == 102 && area == 1) {
            std::cout << "Your tech is Michael McDonald.\n";
        } else if (ou == 103 && area == 1) {
            std::cout << "Your tech is Michael McDonald.\n";
        } else if (ou == 104 && area == 1) {
            std::cout << "Your tech is Michael McDonald.\n";
        }
    } catch(std::exception& e) {
        std::cerr << e.what();
        return EXIT_FAILURE;
    }
}
Please enter the ticket OU:
WSL

Please enter the city the ticket originates from:
DenVEr


Your tech is Michael McDonald.
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
#include <iostream>
#include <string>
#include <utility>
#include <map>
#include <cctype>

int main()
{
    std::map< std::pair<int, std::string>, std::string > ou_and_area_to_tech_map
    {
        { { 101, "denver" }, "Michael McDonald" },
        { { 102, "oklahoma" }, "Mickey Mouse" },
        { { 103, "dallas" }, "Woody Woodpecker" },
        // ...
    };

    const std::string ou_str = "102" ;
    std::string area = "OKLAHOMA" ;

    for( char& c : area ) c = std::tolower( c ) ;
    int ou = 0 ;
    try { ou = std::stoi( ou_str ) ; }
    catch( const std::exception& ) { std::cerr << "invalid ou\n" ; return 1 ; }

    auto iter = ou_and_area_to_tech_map.find( { ou, area } ) ;
    if( iter != ou_and_area_to_tech_map.end() ) std::cout << "tech is '" << iter->second << "'\n" ;
    else std::cerr << "invalid ou/area '" << ou_str << '/' << area << "'\n" ;
}

http://coliru.stacked-crooked.com/a/1f20fc1155af84da
@MiiNiPaa

I tried your code and I only have two issues coming up when compiling. I get a error on line 16 and 33. The { is red lined. It states the compiler is looking for a ;

static const std::map<std::string, int> OU {
{"wsl", 101}, {"ucb", 102}, {"ucs", 103}, {"wls", 104}
};


static const std::map<std::string, int> area {
{"texas", 1}, {"denver", 1}, {"midland", 1}, {"dallas", 1}, {"houston", 1},
{"oklahoma", 2}, {"kansas", 2},
};
Both are functions.


int get_OU(std::string in)
{
static const std::map<std::string, int> OU {
{"wsl", 101}, {"ucb", 102}, {"ucs", 103}, {"wls", 104}
};
std::transform(in.begin(), in.end(), in.begin(), ::tolower);
{
auto it = OU.find(in);
if(it != OU.end())
return it->second;
}
if(all_of(in.begin(), in.end(), ::isdigit))
return std::stoi(in);
throw std::invalid_argument("Bad OU string");
}

int get_area(std::string in)
{
static const std::map<std::string, int> area {
{"texas", 1}, {"denver", 1}, {"midland", 1}, {"dallas", 1}, {"houston", 1},
{"oklahoma", 2}, {"kansas", 2},
};
std::transform(in.begin(), in.end(), in.begin(), ::tolower);
{
auto it = area.find(in);
if(it != area.end())
return it->second;
}
if(all_of(in.begin(), in.end(), ::isdigit))
return std::stoi(in);
throw std::invalid_argument("Bad AREA string");
}
Last edited on
You need to compile in C++11 mode.
C++98 does not support universal initialization, aggregate initialisaton, trailing comma in initializers, automatic type deduction, all_of, stoi, move semantics, range-for-loops. So both mine and JLBorges' code will not work as is in C++98.
Last edited on
thanks MiiniPaa. I'll try that out.
Worked like a charm guys. Thanks for all the good ideas to get me out of the brain jam.

high five!
Topic archived. No new replies allowed.