Function+String

Ok... after struggling a while trying to figure it out I decided to as my question here; didn't find anything related on search.
Question is; I'm trying to make a function to compare char's.
Something like this (example):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//Includes + use std space are here!

void question(char)
{
 switch(toupper(char))
 {
 case 'Y' && 'y': printf("Here you go");
 case 'N' && 'n': printf("Goodbye then.");
 }

}

int main()
{
char q1;
printf("Do you want coffee?");
cin >> q1;
question(q1);
}


But it states that "expected primary expression before char".
How to make it work, or what would be similar way to do this function, so that it can be used multiple times?

Thanks in forward,
-A4
A function parameter needs a type and if you want to use it, a name.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void question(char& ask)
{
 switch(toupper(char& ask))
 {
 case 'Y' && 'y': printf("Here you go");
 case 'N' && 'n': printf("Goodbye then.");
 }

}

int main()
{
char q1;
printf("Do you want coffee?");
cin >> q1;
question(q1);
}

Sorry if this is dumb question but what is wrong with above?
case 'Y' && 'y':

&& looks at expressions to the left and to the right of it. If both are nonzero, && returns 1 (true).

'Y' and 'y' are both nonzero.

Therefore 'Y' && 'y' evaluates to 1. So case 'Y' && 'y': is the same as case 1:

Notice that case 'N' && 'n': is ALSO the same as case 1:, so you have two cases that are exactly the same, and neither of them check to see if the user pressed Y or N.

What you probably wanted was this:

1
2
case 'Y':  case 'y':  cout << "Here you go.";  break;  // don't forget to break;
case 'N':  case 'n':  cout << "Goodbye then.";  break;


But note that you don't even need to check for lowercase 'y' or 'n' becaues you're calling toupper. (although you're calling it incorrectly)


==================

As for your toupper problem:

 
switch(toupper(char& ask))


When calling functions, you do not give the type. You just give the parameters. The correct way to call toupper here would be like so:

 
switch(toupper(ask))  // note:  no 'char&' 



Other notes:

- you should use cout instead of printf. printf isn't typesafe
- 'question' should take 'ask' by value, not by reference. (ie: void question(char ask) is better than void question(char& ask)).
Ok thank's Disch!
Now when I am trying to make it type-proof and in case user takes coffee he gets another question. Here is 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
//Includes and namespace here!
void question(char ask, int answer, int answer2)
{
 switch(toupper(ask))
 {
 case 'Y':{ answer==1, answer2==0; break;}
 case 'N':{ answer==2; break;}
 default:{answer=0; break;}
 }

}

int main()
{
char q1,q2;
int ans1=0, ans2=3;

do{
cout << "Do you want coffee?\n";
cin >> q1;
question(q1, ans1, ans2);
} while (ans1=0);

do{
do{
cout << "Do you want sugar to your coffee?\n";
ans1==3;
cin >> q2;
question(q2, ans1,ans2);
}while (ans2=0);

do{
cout << "I leave you to drink your coffee.";
getch();
return 0;
} while (ans2=2);

do{
cout << "Here you go!";
ans2==3;
getch();
return 0; 
} while (ans2=1);

} while (ans1==1);

do{
cout << "Goddbye then.\n";
getch();
return 0;
} while (ans1==2);

}


But whatever I type, it always types "Do you want sugar to your coffee" first, and whatever I type "I leave you to drink your coffee." second. So what is wrong with it?

EDIT: Sorry for my stupidity fixed few thing on the code, but it still does not work.
Last edited on
1) you are mixing up = and == all over the place. = is assignment (if you want to change a variable's value). == is comparison (if you want to see if a variable is a certain value or not).

2) parameters passed by value cannot be "out" parameters. That is, changing 'answer1' in your question function will not chance 'ans1' in your main function. You can accomplish this with passing by reference, but even that I wouldn't suggest.

For funciton output, it's better to use a return value:

1
2
3
4
5
6
7
8
9
int question(char ask)
{
  switch(toupper(ask))
  {
    case 'Y':  return 1;
    case 'N':  return 2;
    default:  return 0;
  }
}


3) Your heavy use of magic numbers (ie: '1' means something special, '2' means something else, etc) makes your code very hard to follow and error prone. I would recommend you start using enums but I don't think you've learned about those yet, so whatever

4) You really need to learn to indent properly. Your lack of whitespace makes it very difficult to know which braces belong to which loop and where your program is going after which statement. It's all very confusing. Touch this up with proper indentation and hopefully your problem will be easier to spot.
Thanks! Got it working :D And thanks again for the tips.
Topic archived. No new replies allowed.