I keep getting this errror message: error: expected unqualified-id before 'switch'

Dec 23, 2013 at 8:38pm
Hi!
I wanted to create a programm with the switch function in it, so i made a code, where you can type the date and you get a nice sentence like i whish you a happy day or something like that, but i keep getting that message.
Here is the code, i really hope you can help me with it!
Thanks.

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
switch(choice)
{
float choice;
cout << "Geben sie das heutige Datum ein: ";
cout << "Beispiel: 9.1, 14.1, 16.1, 21.1:";
cin >> choice;

case c9.1:
cout << "Ich wünsche dir, dass du arbeitest als würdest du das Geld nicht brauchen";
break;

case c14.1:
cout << "Ich wünsche dir, dass du tanzt als würde keiner hinschauen.";
break;

case c16.1:
cout << "Ich wünsche dir, dass du singst als würde keiner zuhören";
break;

case c21.1:
cout << "Ich wünsche dir, dass du lebst als wäre das Paradies auf Erden";
break;

default:
cout << "Sie haben das Datum nicht korrekt eingegeben.\n";
break;

system("PAUSE");
return 0;
}
Dec 23, 2013 at 8:56pm
Hey Ichmagkekse,
firstly, wrap your code in
[.code]
[./code]
tags(without '.', I used it so it doesn't run in my post :) )

Your switch case isn't included in any function(either main or other). Try to fix that!
Dec 23, 2013 at 8:58pm
closed account (iAk3T05o)
There is no int/void main/whatever main().
Dec 23, 2013 at 9:02pm
Even if there was, you should have either
int main() or int main(int argc, char* args[]), or something like that(char**, for example).
Don't do "void main()".

Also, you switch(case) statement won't work, since you have

 
case c9.1:

you should remove c, so it looks like this:
 
case 9.1:


Cheers!
Dec 24, 2013 at 10:56am
Thank you very much for your responses, but I I have another question: why do you declare choice as int? As far as I know the pc puts the 9.1 as. 9 comma 1 so don't you have to declare it as float? If I am completly wrong please tell me.
Dec 24, 2013 at 11:38am
The problem is, that your code makes completely no sense. It's some part of some function, that is taken completely out of context. However, that's not this bad - you're only beginner.

First off, you have to declare a variable before you use it.
So your code should look something like this:
[code]

#include <iostream>

using std::cout;
using std::cin;
//Main must return int
int main(int argc, char* args[] )
{
//We declare choice before we use it
int choice;
//We ask for choice value before we process it
cout << "Geben sie das heutige Datum ein: ";
cout << "Beispiel: 9.1, 14.1, 16.1, 21.1:";
cin >> choice;

//Switch(case) is not a loop! It will be executed only once!
switch(choice)
{
case 9:
//something
break;
//and so on...
};

return 0;
}

The problem is, switch(case) may only use integral types - check this page for more information:
http://msdn.microsoft.com/en-us/library/k0t5wee3.aspx
http://msdn.microsoft.com/en-us/library/cc953fe1.aspx
Dec 25, 2013 at 1:04pm
Thank you so much, now does it work! It was the second code i tried to write, the first one was so simple, that it didn`t really deserves the name `code`;)
Topic archived. No new replies allowed.