It reads If but skips Else

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.

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
#include <iostream>

using namespace 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;
}
    }

    else if (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;
}


Any help is appreciated!

PS: I know I can just leave it at else...
Last edited on
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;
}
Now you don't need the if else at all.
Last edited on
I think this should be posted in the Articles section...it's a nice write-up:
http://www.cplusplus.com/forum/articles/3483/

EDIT: The Articles Forum is only 3 pages, so it's not hard to find or anything.
Last edited on
(stem != 1||2||3||4)

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).

Do you see why?

-Albatross
OMG so stupid of me! THANKS hamsterman!

And thanks moorecm!
Are you sure? because it is in dutch...
And I'm sure it can be done much more efficient since I only know the very basics...

Yes I do now.. thanks alba! (PS: do you think I should post it in articles?

But thanks anyway!
Last edited on
(PS: do you think I should post it in articles?


Nope. The information is already there...
Topic archived. No new replies allowed.