Obj-C: ++variable is increasing by two instead of one
- by Eli Garfinkel
I am writing a program that asks users yes/no questions to help them decide how to vote in an election. I have a variable representing the question number called questionnumber. Each time I go through the switch-break loop, I add 1 to the questionnumber variable so that the next question will be displayed.
This works fine for the first two questions. But then it skips the third question and moves on to the fourth. When I have more questions in the list, it skips every other question. Somewhere, for some reasons, the questionnumber variable is increasing when I don't want it to.
Please look at the code below and tell me what I'm doing wrong.
Thank you!
Eli
import "MainView.h"
import
@implementation MainView
@synthesize Question;
@synthesize mispar;
int conservative = 0;
int liberal = 0;
int questionnumber = 1;
(IBAction)agreebutton:(id)sender {
++liberal;
}
(IBAction)disagreebutton:(id)sender {
++conservative;
}
(IBAction)nextbutton:(id)sender {
++questionnumber;
switch (questionnumber) {
case 2:
Question.text = @"Congress should pass a law that would ban Americans from earning more than one hundred million dollars in any given year.";
break;
case 3:
Question.text = @"It is not fair to admit people to a university or employ them on the basis of merit alone. Factors such as race, gender, class, and sexual orientation must also be considered.";
break;
case 4:
Question.text = @"There are two Americas - one for the rich and one for the poor.";
break;
case 5:
Question.text = @"Top quality health care should be free for all.";
break;
default:
break;
}
}
@end