Making an 'if' statement accept upper and lower case letters.

I am creating a console application, it is basically a simple 10 question multiple choice quiz. I am using the 'if' statement to detect if the user entered the correct letter (a,b,c or d) and then output as "Question 1 was answered correct / incorrect" I also want the 'if' statement to accept the upper-case version of the letter answer. This is the code I am using to do so at the moment:

1
2
3
4
  if (stricmp ("b" || "B", ans1) == 0) {
            cout << "\n1: CORRECT" << endl; }
    else
        cout << "\n1: INCORRECT" << endl;


But when I attempt to build it, this error is shown: "cannot convert 'bool' to 'const char*' for argument '1' to 'int stricmp(const char*, const char*)".

The code works just fine if I don't use the || and simply settle for just the lowercase. Can anyone shed some light on this for me? Bear in mind I am very new to c++ and this code is probably scrappy. Thanks in advance


stricmp takes two parameters and not 3, for example:
1
2
3
4
5
6
7

	if (stricmp("softrix", "SOFTRIX") == 0)
		printf("The strings are equivalent.\n");
	else
		printf("The strings are not equivalent.\n");



why don't you just convert whatever is in ans1 to uppercase then check that once its converted - that way you don't need to check both.

Something like this....


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

#include <stdio.h>
#include <conio.h>		// for getch
#include <ctype.h>		// for toupper

char ans1;

int main()
{
		
	printf("Enter your answer: ");
	ans1 = _getch();

	ans1 = toupper(ans1);

	if (ans1 == 'A')
		printf("You clicked A");
	else if (ans1 == 'B')
		printf("You clicked B");

		// and so on...

	return 0;

}
Last edited on
"why don't you just convert whatever is in ans1 to uppercase then check that once its converted - that way you don't need to check both."

I completely overlooked this. Thanks for your help, really appreciate it.
Your welcome :)
Actually, since I have a thread going. Another issue I have is, before I tell the user if they answered the questions correctly. I would like to give them a mark out of 10. Any idea how I would go about doing this? Here is my code so far. (Again, I am REALLY noobie at this so the code will be scrappy).

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream>
#include <windows.h>

using namespace std;

int main() {
    char ans1[10];
    char ans2[10];
    char ans3[10];
    char ans4[10];
    char ans5[10];
    char ans6[10];
    char ans7[10];
    char ans8[10];
    char ans9[10];
    char ans10[10];

    cout << "ANSWER THE FOLLOWING QUESTIONS WITH (A,B,C OR D)\n\n";

    cout << "1. Hitler party which came into power in 1933 is known as: ";
    cout << "\n\na) Labour Party b) Nazi Party c) Ku-Klux-Klan d) Democratic Party\n " << endl; // b) Nazi Party
    cin >> ans1;

    cout << "_________________________________________________________\n";

    cout << "\n2. 9/11 Happened in which major USA city? ";
    cout << "\n\na) Los Angeles b) Chicago c) New York d) Boston\n " << endl; // c) New York
    cin >> ans2;

    cout << "_________________________________________________________\n";

    cout << "\n3. In our solar system, which planet comes after Saturn? ";
    cout << "\n\na) Venus b) Mars c) Uranus d) Pluto\n " << endl;              // c) Uranus
    cin >> ans3;

    cout << "_________________________________________________________\n";

    cout << "\n4. What is the capital city of Australia? ";
    cout << "\n\na) Camberra b) Sydney c) Perth d) Melbourne\n " << endl; // a) Camberra
    cin >> ans4;

    cout << "_________________________________________________________\n";

    cout << "\n5. What is the most spoken language in the World? ";
    cout << "\n\na) Spanish b) English c) Chinese d) Mandarin\n " << endl; // d) Mandarin
    cin >> ans5;

    cout << "_________________________________________________________\n";

    cout << "\n6. Who won the world cup in 1966? ";
    cout << "\n\na) England b) Brazil c) Spain d) Italy\n " << endl; // a) England
    cin >> ans6;

    cout << "_________________________________________________________\n";

    cout << "\n7. The largest mountain in the UK is: ";
    cout << "\n\na) Snowdon b) Tryfan c) Ben Nevis d) Skiddaw\n " << endl; // c) Ben Nevis
    cin >> ans7;

    cout << "_________________________________________________________\n";

    cout << "\n8. What is the currency used in Japan? ";
    cout << "\n\na) Euro b) Yen c) Dollar d) Rupee\n " << endl; // b) Yen
    cin >> ans8;

    cout << "_________________________________________________________\n";

    cout << "\n9. Who is the current CEO of Microsoft? ";
    cout << "\n\na) Satya Nadella b) Bill Gates c) Indra Nooyi d) Adam Arnold\n " << endl; // a) Satya Nadella
    cin >> ans9;

    cout << "_________________________________________________________\n";

    cout << "\n10. When was the Berlin Wall knocked down? ";
    cout << "\n\na) 1989 b) 1967 c) 1985 d) 1991\n " << endl;   // a) 1989
    cin >> ans10;
    

    // Grade out of 10 should go here


    // Now for the Answers

    cout << "_______\n\n";
    cout << "RESULTS" << endl;
    cout << "_______\n";

    if (stricmp ("B", ans1) == 0) {
            cout << "\n1: CORRECT" << endl; }
    else
        cout << "\n1: INCORRECT     The correct answer was: b) Nazi Party" << endl;

    if (stricmp ("C", ans2) == 0) {
            cout << "\n2: CORRECT" << endl; }
    else
        cout << "\n2: INCORRECT     The correct answer was: c) New York" << endl;

    if (stricmp ("C", ans3) == 0) {
            cout << "\n3: CORRECT" << endl; }
    else
        cout << "\n3: INCORRECT     The correct answer was: c) Uranus" << endl;

    if (stricmp ("A", ans4) == 0) {
            cout << "\n4: CORRECT" << endl; }
    else
        cout << "\n4: INCORRECT     The correct answer was: a) Camberra" << endl;

    if (stricmp ("D", ans5) == 0) {
             cout << "\n5: CORRECT" << endl; }
    else
        cout << "\n5: INCORRECT     The correct answer was: d) Mandarin" << endl;

    if (stricmp ("A", ans6) == 0) {
             cout << "\n6: CORRECT " << endl; }
    else
        cout << "\n6: INCORRECT     The correct answer was: a) England" << endl;

    if (stricmp ("C", ans7) == 0) {
            cout << "\n7: CORRECT" << endl; }
    else
        cout << "\n7: INCORRECT     The correct answer was c) Ben Nevis" << endl;

    if (stricmp ("B", ans8) == 0) {
            cout <<"\n8: CORRECT" << endl; }
    else
        cout << "\n8: INCORRECT     The correct answer was b) Yen" << endl;

    if (stricmp ("A", ans9) == 0) {
            cout << "\n8: CORRECT" << endl; }
    else
        cout << "\n8: INCORRECT     The correct answer was a) Satya Nadella" << endl;

    if (stricmp ("A", ans10) == 0) {
            cout << "\n9: CORRECT" << endl; }
    else
        cout << "\n9: INCORRECT     The correct answer was a) 1989" << endl;









    return 0;
}

setup a integer called score and increment it when a question is answered correctly. for example...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

	int score = 0;		// initialise it to 0
	
	// ive just used ans10 check as an example..
		
	if (stricmp("A", ans10) == 0) {
		score++;      // increase score, we got it right
		cout << "\n9: CORRECT" << endl;
	}
	else
		cout << "\n9: INCORRECT     The correct answer was a) 1989" << endl;


	// after all the questions checks, you could do this..
	cout << "Your score was " << score << " out of 10."



Just looking at your code I noticed you are setting up arrays for the 1 letter answer (A - D), you don't need arrays here and a simple:

 
char ans1;


then you wouldn't need stricmp either, you could just check using the following...

1
2
if (ans1 == 'A')
    .....


Last edited on
Nice, the score integer worked perfectly. And thanks for pointing out I didn't have to use the array, you see I've learned all I know about C++ so far on this forum, so I kinda skipped on some really simple stuff, like that. You've been a real help, thanks man.
I removed my arrays, and used what you suggested instead, but when I run the code it executes the 'if' no matter what, for example when I was prompted to answer the question, I could literally answer with anything and it would execute anything in my if block. Here's my code:

1
2
3
4
5
if (ans1 == 'B' || 'b') {
            cout << "\nCORRECT" << endl;
            score++; }
    else
        cout << "\nINCORRECT" << endl;

Sorry for the delay, been watching some Supernatural with the wife lol - summoned to the TV :)

your IF statement is incorrectly formatted. It should reference the variable after the OR, i.e.
 
if(ans1 == 'B' || ans1 == 'b')


But had you just made it uppercase as I suggested you could avoid that.

Anyway, I wrote you some code just to give you an example on how all those IF statements etc. could be removed, and your code made much smaller in size. Use it as you wish my friend, if there's anything you are unsure about please ask and I will explain it - hopefully it will be self explanatory :)


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

#include <iostream>
#include <conio.h>		// for getch
#include <ctype.h>		// for toupper

using namespace std;

char Questions[10][200] = {		// 200 chars for question length should cover it  :)
	"1. Hitler party which came into power in 1933 is known as\na) Labour Party b) Nazi Party c) Ku-Klux-Klan d) Democratic Party\n\n",
	"2. 9/11 Happened in which major USA city?\na) Los Angeles b) Chicago c) New York d) Boston\n\n",
	"3. In our solar system, which planet comes after Saturn?\na) Venus b) Mars c) Uranus d) Pluto\n\n",
	"4. What is the capital city of Australia?\na) Camberra b) Sydney c) Perth d) Melbourne\n\n",
	"5. What is the most spoken language in the World?\na) Spanish b) English c) Chinese d) Mandarin\n\n",
	"6. Who won the world cup in 1966?\na) England b) Brazil c) Spain d) Italy\n\n",
	"7. The largest mountain in the UK is:\na) Snowdon b) Tryfan c) Ben Nevis d) Skiddaw\n\n",
	"8. What is the currency used in Japan?\na) Euro b) Yen c) Dollar d) Rupee\n\n",
	"9. Who is the current CEO of Microsoft?\na) Satya Nadella b) Bill Gates c) Indra Nooyi d) Adam Arnold\n\n",
	"10. When was the Berlin Wall knocked down?\na) 1989 b) 1967 c) 1985 d) 1991\n\n"
};

char Answers[10] = { 'B', 'C', 'C', 'A', 'D', 'A', 'C', 'B', 'A', 'A' };

int score;

int main()
{
	score = 0;
	char myAns;

	// loop through the 10 questions 1 by 1
	for (int qNum = 0; qNum < 10; qNum++) {

		// show the question using the array and index
		cout << Questions[qNum];
		cout << "Enter Answer (A-D): ";

		// keep reading the keyboard unless we get a A to D keypress
		do { 
			myAns = _getch(); 
			myAns = toupper(myAns);
		} while (myAns < 'A' || myAns > 'D');

		// display what i pressed.
		cout << myAns << endl;

		// we use the index of Questions to grab the correct answer from the
		// answers array.
		if (myAns == Answers[qNum]) {
			cout << "Well done you got it correct!"<<endl<<endl;
			score++;
		}
		else
			cout << "Unlucky, the answer was " << Answers[qNum]<<endl<<endl;

	}

	// display our score.
	cout << endl << "You scored " << score << " out of a possible 10.";

	return 0;

}

 


The above includes the scoring code.
Nice, the score integer worked perfectly. And thanks for pointing out I didn't have to use the array, you see I've learned all I know about C++ so far on this forum, so I kinda skipped on some really simple stuff, like that. You've been a real help, thanks man.


Glad you got that sorted and your welcome. I started learning to code at the age of 12 and at that time I didn't have a computer lol - id write programs on scrap bits of paper and take them to my friends on a Saturday and type them in to test them on his Commodore VIC20

The internet helps in a huge way for learning new skills now, we didn't have internet when I was 12 so I used the library a lot. I came across this website today, and only joined today - its the kind of website I enjoy contributing to, and to help others learn to code.

Hope you don't mind me reworking your code a little :)
why use deprecated getch instead of std::cin.get(myAns);
getch() is deprecated, _getch() isnt.
Topic archived. No new replies allowed.