at the top, before the piece I posted. When I run the program as is, this is my output:chomp($guess = <STDIN>);
As you can see, no matter correct, or incorrect, always get "You Suck", then I have to enter an extra \n at the end to end the loop, after that, three lines later, it says "You Rock!, for no reason whatsoever. So, here's the whole code:Name the definitive rock band.. The Who You Suck! What is their best song? Moonlight Mile You Suck! What is their best album? Sticky Fingers You Suck! You Rock!
#!usr/bin/perl use strict; my @questions = ("Name the definitive rock band.." , "What is their be +st song?" , "What is their best album?"); my @correct = ("The Rolling Stones" , "Moonlight Mile" , "Sticky Finge +rs"); my $grade = 100 - length(my @incorrect) * 33.3; my @incorrect; my $i = 0; my $guess; # This program asks user three questions, then grades the user's ans +wers. while ($i <= scalar(@incorrect)) { # loop through questions print $questions[$i] . "\n"; chomp($guess = <STDIN>); $i++; if ($guess eq $correct[$i]) { # if guess is correct, reply print "You Rock!\n"; $i++; } else { print "You Suck!\n"; # if guess is incorrect, reply and save +answer to be calculated @incorrect = (@incorrect, $guess); #while ($guess ne @correct[$i]) { # if guess is incorrect, pro +mpt user to try again # print "Try again!\n"; # chomp($guess = <STDIN>); # $i++; # } # end while } # end if } # end while print $grade . "\n"; # calculate and print user's grade print "\nRockstar Programming Inc. All Rights Reserved";
Note the loop commented out at bottom is a whole other topic, just included it so ya'll could see, because I certainly don't understand this output at all.
Also, eosbuddy, is this, I'm assuming a regular expression?
I do know what they are, lol, just not used to seeing that type at this point. I can't use them, but it is helpful to see how you put my data together to make it work.my @correct = qw / 1 2 3 4 5 6 7 8 9 10 /;
But, does anyone understand this output I'm getting? I know my knowledge is very limited, but I just can't seem to place my mistake. There are, I'm sure several re-writes I could do, but I really want to understand why this won't work, to further my skill in the language, because theoretically I really think it should??
Also, please note that my output includes me entering answers as the user, I entered correct and incorrect to show that it didn't change the response.
Also, I am very much aware of how useless a trivia game is written this way, especially without any regular expressions. It's more of a learning exersise, to give me a small example of doing something on my own in Perl, and I also do know a bit more these days, but regardless of the fact that it is a useless game lol, I want to understand my mistake more for the experience than the game :)
In reply to Re^2: Control Structure problem, mistake can't be found
by koolgirl
in thread Control Structure problem, mistake can't be found
by koolgirl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |