Help with my code.

Pages: 12
How can I display Error for only a specific case in case either
1) If the input 0 was wrong. Only display "Invalid major code"

2) If the input 1 was wrong. Only display "Invalid status code"

3) In case both are wrong. Display both.

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

int main()
{

	string input;
	cout << "Enter two characters: ";
	getline(cin, input);

	if (!isalpha(input.at(0)) || !isdigit(input.at(1)))
	    {
			cout << "Invalid input" << endl;
			return -1;
	    }

	if (input.at(0) == 'M')
		cout << "Mathematics ";
	else if (input.at(0) == 'C')
		cout << "Computer Science ";
	else if (input.at(0) == 'I')
		cout << "Information technology ";
	else
		cout << "Invalide major code" << endl;

	int status = input.at(1);
	
	
	
	switch(status)
	{
		case 1:
			cout << "Freshman";
			break;
		case 2:
			cout << "Sophomore";
			break;
		case 3:
			cout << "Junior";
			break;
		case 4:
			cout << "Senior";
			break;
		default: cout << "Invalid status code" << endl;
	}


	return 0;
}
 
int status = input.at(1);

This doesn't convert the char '2' to an int 2, for example.

Here's a simple fix:
 
int status = input.at(1) - '0';


After fixing that, it works as intended.
Scenario 1):

Enter two characters: X1
Invalide major code
Freshman


Scenario 2):

Enter two characters: M0
Mathematics Invalid status code


Scenario 3):

Enter two characters: X0
Invalide major code
Invalid status code
Last edited on
Thanks, why did I have to subtract by a string? Also, for

Scenario 1, I only wanted

Enter two characters: X1
Invalid major code

Scenario 2, I only wanted

Enter two characters: M0
Invalid status code

Sorry if I'm not being clear, here is the my original 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
#include <iostream>
#include <string>
using namespace std;

int main()
{

	string input;
	cout << "Enter two characters: ";
	cin >> input;

	if (input[0] == 'M')
		cout << "Mathematics ";
	else if (input[0] == 'C')
		cout << "Computer Science ";
	else if (input[0] == 'I')
		cout << "Information technology ";
	else
		cout << "Invalid major code";

	int status = input[1] - 48;
	
	switch(status)
	{
		case 1:
			cout << "Freshman";
			break;
		case 2:
			cout << "Sophomore";
			break;
		case 3:
			cout << "Junior";
			break;
		case 4:
			cout << "Senior";
			break;
		default:
			cout << "Invalid status code";
			break;
	}	
	
	return 0;
}



I wanted

Scenario 1

Enter two characters: M1
Mathematics Freshman

Scenario 2

Enter two characters: C3
Computer Science Junior

Scenario 3

Enter two characters: T3
Invalid major code

Scenario 4

Enter two characters: M7
Invalid status code

Scenario 5

Enter two characters: X0
Invalid major code
Invalid status code
Last edited on

Thanks, why did I have to subtract by a string?
int status = input.at(1) - '0';
Here you are implicitly converting a char to an int, which C++ allows. However, the character '2' doesn't become the integer 2. It becomes the integer 50. 50 is the ASCII code for the character '2', as shown here: http://www.ascii-code.com/
Thus to get the integer 2 from the character '2', we need to subtract 48, which is the ASCII code for the character '0'.



Scenario 1, I only wanted

Enter two characters: X1
Invalid major code

Scenario 2, I only wanted

Enter two characters: M0
Invalid status code

That is simply a program flow control issue. Verify the input before line 18.
So how to fix the issue of the input before line 18?
So how to fix the issue of the input before line 18?
1
2
3
    if( input[0] != 'M' || input[0] != 'C' || input[0] != 'I' ) {
        cout << "Invalid major code";
    }
 
 if( input[0] != 'M' || input[0] != 'C' || input[0] != 'I' )


@integralfx - That if statement is always going to be true. if input[0] is 'C', then != 'M' will be true, making the entire condition true. Ditto for the other two values.

Should be:
 
if (! (input[0] == 'M' || input[0] == 'C' || input[0] == 'I' ))

Ok, here is the my codes's status

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

int main()
{

	string input;
	cout << "Enter two characters: ";
	cin >> input;

	if (input[0] == 'M')
		cout << "Mathematics ";
	else if (input[0] == 'C')
		cout << "Computer Science ";
	else if (input[0] == 'I')
		cout << "Information technology ";
	if (! (input[0] == 'M' || input[0] == 'C' || input[0] == 'I' ))
	{
		cout << "Invalid major code" << endl;
		return -1;
	}

	int status = input[1] - 48;
	
	switch(status)
	{
		case 1:
			cout << "Freshman";
			break;
		case 2:
			cout << "Sophomore";
			break;
		case 3:
			cout << "Junior";
			break;
		case 4:
			cout << "Senior";
			break;
		default:
			cout << "Invalid status code";
			break;
	}	
	
	return 0;
}



I got Scenario 1, 2 and 3 right but

Scenario 4

Enter two characters: M7
Mathematics Invalid status code

Scenario 5

Enter two characters: X0
Invalid major code


Please help. Thanks.

I think that you want to STORE your subject and year (say: string subject, year) and STORE your validity (say: bool validSubject, validYear) rather than using cout immediately in your if and switch blocks.

Then at the end (NOT during your if or switch blocks)
1
2
3
4
5
6
7
8
if ( validSubject && validYear )
{// code to output subject and year}

if ( !validSubject ) 
{// code to complain about the subject}

if (!validYear )
{// code to complain about the year) 


You still aren't doing what @integralfx very sensibly suggested you do:
int status = input[1] - '0';
Last edited on
I did it with the '0' and with your suggestion but I can't yield the result. Please help.
What is your current problem now?
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
#include <iostream>
#include <string>
using namespace std;

int main()
{

	string input;
	cout << "Enter two characters: ";
	cin >> input;

	if (input[0] == 'M')
		cout << "Mathematics ";
	else if (input[0] == 'C')
		cout << "Computer Science ";
	else if (input[0] == 'I')
		cout << "Information technology ";
	if (! (input[0] == 'M' || input[0] == 'C' || input[0] == 'I' ))
	{
		cout << "Invalid major code" << endl;
		return -1;
	}

	int status = input[1] - '0';
	
	switch(status)
	{
		case 1:
			cout << "Freshman";
			break;
		case 2:
			cout << "Sophomore";
			break;
		case 3:
			cout << "Junior";
			break;
		case 4:
			cout << "Senior";
			break;
		default:
			cout << "Invalid status code";
			break;
	}	
	
	return 0;
}


So I wanted


Scenario 1

Enter two characters: M1
Mathematics Freshman

Scenario 2

Enter two characters: C3
Computer Science Junior

Scenario 3

Enter two characters: T3
Invalid major code

Scenario 4

Enter two characters: M7
Invalid status code

Scenario 5

Enter two characters: X0
Invalid major code
Invalid status code


Scenario 1-3 was already fulfilled.

However, Scenario 4
I got

Enter two characters: M7
Mathematics Invalid status code

Scenario 5

Enter two characters: X0
Invalid major code


How can I fix it?
What are 'Invalid major code'?
What are 'Invalid status code'?
So I wanted each input to be their own error. For example

Enter two characters: M7
Invalid status code

because the status variable wasn't withing the range of 1-4

Enter two characters: T3
Invalid major code

because the input[0] was invalid with the specified condition

Enter two characters: X0
Invalid major code
Invalid status code

because both input of the string was invalid.
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
#include <iostream>
#include <string>
using namespace std;

int main()
{

	string input;
	cout << "Enter two characters : ";
	cin >> input;

	switch(input[0])
	{
		case 'M' : cout << "Mathematics "; break;
		case 'C' : cout << "Computer Science "; break;
		case 'I' : cout << "Information technology "; break;
		default : 
			cout << "Invalid major code" << endl; 
			return - 1;
		break;
	}

	switch(input[1])
	{
		case '1' : cout << "Freshman"; break;
		case '2' : cout << "Sophomore"; break;
		case '3' : cout << "Junior"; break;
		case '4' : cout << "Senior"; break;
		default : 
			cout << endl;
			cout << "Invalid status code";
		break;
	}	

	cout << endl;
	return 0;
}
Your code still negate scenario 4 and 5
Your code still negate scenario 4 and 5
What do you mean by "negate"?
And where is the scenario '5'?
I already posted

I wanted

Scenario 1

Enter two characters: M1
Mathematics Freshman

Scenario 2

Enter two characters: C3
Computer Science Junior

Scenario 3

Enter two characters: T3
Invalid major code

Scenario 4

Enter two characters: M7
Invalid status code

Scenario 5

Enter two characters: X0
Invalid major code
Invalid status 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
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string input;
	cout << "Enter two characters : ";
	cin >> input;

	bool bFailed = false;
	switch(input[0])
	{
		case 'M' : break;
		case 'C' : break;
		case 'I' : break;
		default : 
			cout << "Invalid major code" << endl; 
			bFailed = true;
		break;
	}

	switch(input[1])
	{
		case '1' : break;
		case '2' : break;
		case '3' : break;
		case '4' : break;
		default : 
			cout << "Invalid status code" << endl;
			bFailed = true;
		break;
	}	

	if(bFailed) return -1;

	switch(input[0])
	{
		case 'M' : cout << "Mathematics "; break;
		case 'C' : cout << "Computer Science "; break;
		case 'I' : cout << "Information technology "; break;
	}

	switch(input[1])
	{
		case '1' : cout << "Freshman"; break;
		case '2' : cout << "Sophomore"; break;
		case '3' : cout << "Junior"; break;
		case '4' : cout << "Senior"; break;
	}	

	cout << endl;
	return 0;
}
Ok, I have it figured it out. As it turned out, I just need to use the restrict condition first. Thank you for all of your help.
Pages: 12