how do you use char for multiple charector input

closed account (SEbXoG1T)
i've been trieng to use char for multiple charector input, but it wont work! someone please help
closed account (SEbXoG1T)
this is the program i want to use it in:

#include <iostream>
using namespace std;
#include <cstring>
#include <cstdlib>



int main () {
std::string name;

cout<<"your name? \n\n";
getline (cin,name);

char answer;



cout<<""<<name<<" what is 5*2 ";
cin>>answer;


switch (answer) {

case '1':
std::cout << "GOOD JOB!! ";
break;

default:
cout<<""<<name<<" what is 5*2 ";
cin>>answer;
switch (answer) {
case '1':

std::cout << "GOOD JOB!! \n";
break;
case 'N':
case 'n':
std::cout << "sorry, that is not the answer. \n";
break;
default:
cout<<"you only get 2 shots \n";
break;
}
break;
}


getline (cin, name);
std::cin.ignore(100, '\n');
return 0;
}
I don't understand your switch...
closed account (SEbXoG1T)
if you get the answer wrong the first time, it haves you retry
This is probably better as an if-else statement.

Also, I think it should be <string> not cstring in your header.

Unfortunately your switch statement makes no sense.
If you want a switch then make the 'answer' an int not a char.
If you want to repeat the switch then put it in a do-while loop as shown.

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

int main () {
string name;

cout<<"your name? \n\n";
getline (cin,name);
int answer;

do
{
cout<<""<<name<<" what is 5*2 \n";
cin>>answer;

switch (answer) 
{

case 10:
cout << "GOOD JOB!! ";
break;

default:
cout<<""<<name<<" That is incorrect. \n";
break;
}
}while (answer!=10);

cin.ignore().get();
return 0;
}


Last edited on
If you want a switch then make the 'answer' an int not a char.


Actually if it is a single char, switch construct does support it. Of cuz normally we use int but I have seen other ppl code where they use a single char.
I think that was the problem though, 2 characters were needed for his char. And since it was 2*5 the simplest solution is to make it an int so user can input 10 or 2032 etc. I guess I should have been clear that this was for his example only and not a universal c++ rule.
Last edited on
Topic archived. No new replies allowed.