Hello,
i am currently making a quiz app and i have a score label which i need to either add 1 for a correct answer or delete one for a incorrect answer. so i have this code
Hi, First of all, I would keep the score counting seperate from the actual text, it will make it alot easier, 2nd, use NSString's isEqualTo method to compare strings.
Hello,
i am currently making a quiz app and i have a score label which i need to either add 1 for a correct answer or delete one for a incorrect answer. so i have this code
Its still not working, when i run it the score stays the same do you know what else i can try
Add an NSLog in the if statement to see if it evaluates to true. If it doesn't, NSLog the value of question.text right before the if to see what it is.
Add an NSLog in the if statement to see if it evaluates to true. If it doesn't, NSLog the value of question.text right before the if to see what it is.
i dont understand what you mean could you send me the code i am new to iphone development
Its still not working, when i run it the score stays the same do you know what else i can try
May I know how are you setting the question.text? If there are a space after the ? at the end, this code wont work too. I cant really help u without more details to the codes.
May I know how are you setting the question.text? If there are a space after the ? at the end, this code wont work too. I cant really help u without more details to the codes.
I have checked that all the questions are the same. this is all the code i have.
Code:
- (IBAction)A:(UIButton *)sender {
result.text = @"Correct";
score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] + 1)];
NSLog(question.text);
if ([question.text isEqualToString:@"What was the first programmable computer called?"]) {
score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];
result.text = @"Incorrect";
}
}
- (IBAction)B:(UIButton *)sender {
result.text = @"Incorrect";
score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];
if ([question.text isEqualToString:@"What was the first programmable computer called?"]) {
score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] + 1)];
result.text = @"Correct";
}
}
- (IBAction)C:(UIButton *)sender {
result.text = @"Incorrect";
score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];
if ([question.text isEqualToString: @"What was the first programmable computer called?"]) {
result.text = @"Incorrect";
score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];
}
}
- (IBAction)D:(UIButton *)sender {
result.text = @"Incorrect";
score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];
if ([question.text isEqualToString: @"What was the first programmable computer called?"]) {
score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];
result.text = @"Incorrect";
}
}
- (IBAction)back:(UIButton *)sender {
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)next:(UIButton *)sender {
question.text = @"What was the first programmable computer called?";
answerA.text = @"ZX";
answerB.text = @"Z1";
answerC.text = @"ZZ";
answerD.text = @"Z0";
number.text = @"2";
I have checked that all the questions are the same. this is all the code i have.
Code:
- (IBAction)A:(UIButton *)sender {
result.text = @"Correct";
score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] + 1)];
NSLog(question.text);
if ([question.text isEqualToString:@"What was the first programmable computer called?"]) {
score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];
result.text = @"Incorrect";
}
}
- (IBAction)B:(UIButton *)sender {
result.text = @"Incorrect";
score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];
if ([question.text isEqualToString:@"What was the first programmable computer called?"]) {
score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] + 1)];
result.text = @"Correct";
}
}
- (IBAction)C:(UIButton *)sender {
result.text = @"Incorrect";
score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];
if ([question.text isEqualToString: @"What was the first programmable computer called?"]) {
result.text = @"Incorrect";
score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];
}
}
- (IBAction)D:(UIButton *)sender {
result.text = @"Incorrect";
score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];
if ([question.text isEqualToString: @"What was the first programmable computer called?"]) {
score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];
result.text = @"Incorrect";
}
}
- (IBAction)back:(UIButton *)sender {
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)next:(UIButton *)sender {
question.text = @"What was the first programmable computer called?";
answerA.text = @"ZX";
answerB.text = @"Z1";
answerC.text = @"ZZ";
answerD.text = @"Z0";
number.text = @"2";
Hi.. I think I may have guessed at what is wrong (or not wrong rather).
I see you are adding the score before the if statement (see the Red fonts i highlighted above). I think the question is detected, but since u are +1 to the score before the if statement, so after the IF STATEMENT is TRUE, it would -1 to score again which sums up to, 0. So no changes in the score was made.
I may be wrong though.. Is that line correct? Seems a bit weird for me.
Hi.. I think I may have guessed at what is wrong (or not wrong rather).
I see you are adding the score before the if statement (see the Red fonts i highlighted above). I think the question is detected, but since u are +1 to the score before the if statement, so after the IF STATEMENT is TRUE, it would -1 to score again which sums up to, 0. So no changes in the score was made.
I may be wrong though.. Is that line correct? Seems a bit weird for me.
i dont get errors but i think you are correct do you know what i could do to fix this
i dont get errors but i think you are correct do you know what i could do to fix this
Hi. Maybe u can do it like this:
Code:
- (IBAction)A:(UIButton *)sender {
if ([question.text isEqualToString:@"What was the first programmable computer called?"]) {
score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];
result.text = @"Incorrect";
return;
}
result.text = @"Correct";
score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] + 1)];
NSLog(question.text);
}
So if the result is wrong, it would deduct 1 point from score and exit the method (using return). Then all the green code is ignored. If it is right, the if statement is ignored, and the green code is executed. I probably use a complete if..else statement.. but im not sure how ur game work. Give the above code a try.
- (IBAction)A:(UIButton *)sender {
if ([question.text isEqualToString:@"What was the first programmable computer called?"]) {
score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];
result.text = @"Incorrect";
return;
}
result.text = @"Correct";
score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] + 1)];
NSLog(question.text);
}
So if the result is wrong, it would deduct 1 point from score and exit the method (using return). Then all the green code is ignored. If it is right, the if statement is ignored, and the green code is executed. I probably use a complete if..else statement.. but im not sure how ur game work. Give the above code a try.
now it is not adding 2 but still adding 1 instead of taking away 1.
i must be doing the if statement wrong because now the result.text sais correct but in the if statement it is supposed to say incorrect.
Thanks to everyone. I got it working, what was wrong was I hadn't added one or subtracted 1 on the first question and therefore the others were not working.
I am reading data from a TCP/IP stream and am successfully receiving a byte array from the pre-existing server. I am now trying to find a way to convert that array to an NSString.
But you still have not taken the very important advice of QuantumDoja in the very first response to your posting. You definitely should not use score.text as the primary storage of the value of the score. You will have trouble later on if you don't fix it now. You should store the score in an int value as part of some object other than the view. Perhaps the view controller, or even maybe the app delegate, if the score is essentially a global singleton.
For example, suppose you push another view controller onto the navigation stack and the system decided to unload the score view. That will destroy your one and only memory of the actual score. So when the view needs to be re-created the next time it loads, you won't have any value to put in for the score.
Or, suppose someone is playing your game and they get a phone call, which forces your app to exit. After the call, they return to your app, only to find that their score has disappeared. It is much easier to save the state of an int than to save the state of the text of a view.