switch question

Below what do I add to my switch statements to code "for all the rest that are not listed"(sic). I have conditions for D, d, H, h no prob. But H and h will report a false as it should but which would be the state if something off-the-wall were entered. So I want to discriminate further.

PS I know ways of making this better without enums etc., but I am interested in the solution posted.

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
#include<iostream>
#include<array>
#include<string>

enum draw_hold
{
	D =true,
	d =true,
	H =false,
	h =false
};

bool io(bool draw_or_hold)
{
	std::string str_input;
	std::cout<<"Input string: ";
	std::cin>>str_input;
	switch (str_input[0])
		{
			case 'D' : std::cout<<"Draw";
					return D;
			case 'd' : std::cout<<"Draw";
					return d;
			case 'H' : std::cout<<"Hold";
					return H;
			case 'h' : std::cout<<"Hold";
					return h;
		}
}

int main()
{
	bool n = io(false);
	std::cout<<n;
	return 0;
}




Need additional cases of?
Last edited on
Hi,

Also make your life easier by using the std::toupper function, this will halve your logic.

Using an enum like that with bool is perhaps not a good use of an enum.

If you configure your editor to convert tabs to 4 spaces say, your code will display much better here. This site converts tabs to 8 spaces, resulting in excessive indenting.

Regards
update,

using:http://www.cplusplus.com/doc/tutorial/control/

The case statements now cover everything.

1
2
default:
std::cout << "value of x unknown";


this fixed the problem by adding a default switch:

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
#include<iostream>
#include<array>
#include<string>

enum draw_hold
{
	D =true,
	d =true,
	H =false,
	h =false
};

bool io(bool draw_or_hold)
{
	std::string str_input;
	std::cout<<"Input string: ";
	std::cin>>str_input;
	switch (str_input[0])
		{
			case 'D' : std::cout<<"Draw";
					return D;
			case 'd' : std::cout<<"Draw";
					return d;
			case 'H' : std::cout<<"Hold";
					return H;
			case 'h' : std::cout<<"Hold";
					return h;
			default:
			    std::cout << "value of input unknown";
		}
}

int main()
{
	bool n = io(false);
	std::cout<<n;
	return 0;
}



Investigating: std::toupper
The case statements now cover everything.
Last edited on
per toupper: why does 2nd one work but not 1st

get a small c here: (incorrect)
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
#include<array>
#include<string>
#include<locale>

int main()
{
	locale loc;
	std::string str = "cartwheel";
	std::toupper(str[0],loc);
	cout<<str[0];
	return 0;
}

output:c

get a high case (correct) result here:
1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
#include<array>
#include<string>
#include<locale>

int main()
{
	locale loc;
	std::string str = "cartwheel";
	cout<<std::toupper(str[0],loc);
	return 0;
}

output C
Last edited on
Hi,

In the first one, you didn't do anything with the return value of the function.

ok got that one. Can I use a for-each loop with the str array as well?

working code:

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

int main()
{
for(int i=0; i<str.size();++i)
	{
		std::cout<<toupper(str[i],loc);
	}
}

output: cartwheel

for-each below not working

1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
#include<array>
#include<string>
#include<locale>
int main()
{
        std::locale loc;
	std::string str = "cartwheel";
	for (auto & elem:str)
	{
	str[elem] = std::toupper(str[elem],loc);
	}


output: C\CALJRRRA
Last edited on
1
2
3
4
    for (auto & elem:str)
    {
        str[elem] = std::toupper(str[elem],loc);
    }

should be
1
2
3
4
    for (auto & elem:str)
    {
        elem = std::toupper(elem,loc);
    }


Note, the first version is accessing out of range elements of the string.
str[elem] uses each letter in turn as the subscript,
str['c'] which is str[99] in the ASCII table.
str['w'] which is str[119] in the ASCII table.

It leaves the string unchanged - but corrupts some other area of memory.
Last edited on
I guess in a for-each loop you can't use the array (str) as a subscripted data structure. It is the container design that you don't need to keep track of things, hence your solution has no subscripts. Or, are there any scenarios where you would do some thing like str[item#]?

Used correctly, you won't need boundary checking in the for-each scenario.
Last edited on
I guess in a for-each loop you can't use the array (str) as a subscripted data structure.
I don't see anything that says you can't use subscripts. Only that the subscript should be valid, in the range zero to str.size() -1.

It is the container design that you don't need to keep track of things, hence your solution has no subscripts.

Not the container. The for-loop.

Here:
for (auto & elem:str)

str is some sort of container - could be a string or a vector or a list etc.

elem is a single element within that container.

Using an element of a container as a subscript to access another element within that container could be valid:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main()
{
    int arr[] =  {5, 3, 4, 2, 6, 1, 0};
    

    for (auto elem:arr)
        std::cout << elem << ' ';
    std::cout << '\n';    


    for (auto elem:arr)
        std::cout << arr[elem] << ' ';
    std::cout << '\n';
    
}
5 3 4 2 6 1 0
1 2 6 4 0 3 5


IDM

If you configure your editor to convert tabs to 4 spaces say, your code will display much better here. This site converts tabs to 8 spaces, resulting in excessive indenting.


working on it
In my IDE's editor , I never need to use the tab key - it does indenting by itself, and uses spaces to do so. If I were to use the tab key, it converts those to spaces as well. There should be some settings somewhere to configure this.
Topic archived. No new replies allowed.