I was wondering if someone could help me figure out why the if-statements refuse
ID: 3834545 • Letter: I
Question
I was wondering if someone could help me figure out why the if-statements refuse to function properly.
Whether I use "eq", "<=>" or "==", it keeps going into the "yes" condition regardless of whether I type "y" or "n", or even any other answer. Can someone tell me why it's doing this and how to correct it?
It's a Perl scripting language.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
open(DATA,"<info.txt") or die "Can't open data";
$file[0]=<DATA>;
$file[1]=<DATA>;
$file[2]=<DATA>;
$file[3]=<DATA>;
close (DATA);
print " Full Name: $file[0] Age: $file[1] Gender: $file[2] Marital Status: $file[3]";
print " Do you want to change the age? (Y/y or N/n): ";
$a = <STDIN>;
print "$a";
if($a=='Y' or $a=='y')
{
print "What is the new age? ";
$file[1]= <STDIN>;
$c=$a;
}
print " Do you want to change the Marital Status? (Y/y or N/n): ";
$b = <STDIN>;
print "$b";
if($b=='Y' or $b=='y')
{
print "What is the new marital status? ";
$file[3] = <STDIN>;
$c=$b;
}
print "$c";
if($c=='Y' or $c=='y')
{
$info2 = join('',$file[0], $file[1], $file[2], $file[3]);
print "$info2";
open($info2,">info.txt") or die "Can't open data";
}
open(DATA,"<info.txt") or die "Can't open data";
$file[0]=<DATA>;
$file[1]=<DATA>;
$file[2]=<DATA>;
$file[3]=<DATA>;
close (DATA);
print "The result is: Full Name: $file[0] Age: $file[1] Gender: $file[2] Marital Status: $file[3]";
Explanation / Answer
Modified code:
#!/usr/bin/perl
print " Do you want to change the age? (Y/y or N/n): ";
$a = <STDIN>;
chomp($a);
print "$a";
if($a eq 'Y' or $a eq 'y')
{
print "What is the new age? ";
$file[1]= <STDIN>;
$c=$a;
}
print " Do you want to change the Marital Status? (Y/y or N/n): ";
$b = <STDIN>;
chomp($b);
print "$b";
if($b eq 'Y' or $b eq 'y')
{
print "What is the new marital status? ";
$file[3] = <STDIN>;
$c=$b;
}
chomp($c);
print "$c";
if($c eq 'Y' or $c eq 'y')
{
$info2 = join('',$file[0], $file[1], $file[2], $file[3]);
print "$info2";
open($info2,">info.txt") or die "Can't open data";
}
Explanation:
Above is the code modified from the code you have pasted by removing the parts which are not available in my system. Note that string comparison is done using "eq" but not "==". The '==' is meant for number comparision. Also since you are comparing a single character from $a/$b/$c, try to chomp the value to remove ' ' as i did. Because it can cause issues some times. With all these changes i am able to execute the script you have given. Below is the output.
Execution and output:
186590cb0725:Perl bonkv$ ./check_code.pl
Do you want to change the age? (Y/y or N/n): y
yWhat is the new age? 23
Do you want to change the Marital Status? (Y/y or N/n): y
yWhat is the new marital status? married
y23
married
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.