So i made a program where users have to vote for things. Everything works fine , except when you enter another number then 1,2,3 or 4. It just skips else says again: persoon x mag nu stemmen.
#include <iostream>
usingnamespace std;
int a = 1;
int NVA = 0, VLD = 0, SPA = 0, GROEN = 0;
float nva, vld, spa, groen;
int stem;
int personen;
int main ()
{
cout<<"Hallo en welkom bij [Wie Verkies Je? 2010] !";
cout<<"\n\n1 = NV-A\n2 = Open VLD\n3 = SP.A\n4 = Groen!\n\n";
cout<<"Hoeveel personen zullen stemmen?\n";
cin>>personen;
while (a < personen + 1){
cout<<"\nPersoon "<<a<<" mag nu stemmen\n\n";
cin>> stem;
if (stem == 1 || 2 || 3 || 4){
switch (stem)
{
case 1:
NVA++;
a++;
break;
case 2:
VLD++;
a++;
break;
case 3:
SPA++;
a++;
break;
case 4:
GROEN++;
a++;
break;
}
}
elseif (stem != 1||2||3||4)
{
cout<<"Stem op 1 van de partijen aub!\n";
}
}
nva = NVA;
NVA = (nva/personen)*100;
cout<<"\n\nNV-A heeft "<<NVA<<"% van de stemmen.\n";
vld = VLD;
VLD = (vld/personen)*100;
cout<<"Open VLD heeft "<<VLD<<"% van de stemmen.\n";
spa = SPA;
SPA = (spa/personen)*100;
cout<<"SP.A heeft "<<SPA<<"% van de stemmen.\n";
groen = GROEN;
GROEN = (groen/personen)*100;
cout<<"Groen! heeft "<<GROEN<<"% van de stemmen.\n";
return 0;
}
line 23 is wrong
It should be if (stem == 1 || stem == 2 || stem == 3 || stem == 4) (the same for line 49)
Though, generally, there is a faster way to do what you want. Use the default case. example:
1 2 3 4 5 6 7 8 9 10 11
switch(stem){
case 1:
NVA++;
a++;
break;
case 2:
//and so on...
default:
cout << "ERROR!";
break;
}
This like the if statement above does not do what you think it does. You probably wanted: (stem != 1|| stem != 2|| stem != 3|| stem != 4)
(which is redundant, you could have just used a plain else statement, but you knew that).