Hello. I'm working on an assignment which involves opening a file, asking a user for input, and then writing their input into the opened file. I think I've got that down fairly decently, but I can't figure out how to allow the user to input more than once before closing the file.
The program is supposed to run until the user inputs "NOMORE" for the country name (meaning after they hit enter for the city, it should ask for another country name):
$test="FILEPATH/testing.txt";
if(-e $test) {
print "File already exists.\n";
print "Overwrite(1) exsiting file or append(2) existing file? (1/2): ";
$choice=<STDIN>;
if($choice=="2"){
open TEST, ">>$test" or die "$test could not be opened for append: $!";
} else{
open TEST, ">$test" or die "Couldn't overwrite $test: $!";
}
} else {
open TEST, ">>$test" or die "$test could not be opened for writing: $!";
}
print "\nWhen you are done entering countries, type NOMORE.\n";
print "\nEnter a country: ";
$country=<STDIN>;
chomp ($country);
if ($country eq "NOMORE"){
close (TEST);
} else {
print "Enter the capital of $country: ";
$capital=<STDIN>;
chomp ($capital);
print TEST "$country, $capital.\n";
}
open(TEST, "<$test") or die "Could not open $test: $!\n";
while(defined($a=<TEST>)) {
print "\n$a";
}
close (TEST);
When I try to add a while loop, the program skips over everything I have in the loop:
print "\nWhen you are done entering countries, type NOMORE.\n";
while(<TEST>){
print "\nEnter a country: ";
$country=<STDIN>;
chomp ($country);
if ($country eq "NOMORE"){
close (TEST);
} else {
print "Enter the capital of $country: ";
$capital=<STDIN>;
chomp ($capital);
print TEST "$country, $capital.\n";
}
}
open(TEST, "<$test") or die "Could not open $test: $!\n";
Any help or suggestions you can offer would be fantastic.
Thanks
What I came up with:
$test="FILEPATH/testing.txt"; # assigns file to $test
if(-e $test) { # check to see if file already exists
print "File already exists.\n"; # if it does, tell the user
print "Overwrite(1) exsiting file or append(2) existing file? (1/2): "; # ask user if they want to overwrite or append file
$choice=<STDIN>; # user inputs choice
if($choice=="2"){ # if user wants to append...
open TEST, ">>$test" or die "$test could not be opened for append: $!"; # open the file to be appended
} else{ # otherwise...
open TEST, ">$test" or die "Couldn't overwrite $test: $!"; # open the file to be overwritten
}
} else { # if the file does not exist...
open TEST, ">>$test" or die "$test could not be opened for writing: $!"; # create it
}
print "\nWhen you are done enter NOMORE as country name.\n"; # give user instructions
print "\nEnter a country: "; # ask user for name of country
$country=<STDIN>; # assign user input to $country
chomp ($country); # remove newline from $country
if ($country eq "NOMORE") {
close (TEST); # close the file if user enters NOMORE
}else{ # otherwise...
print "Enter the capital of $country: "; # ask them for the capital of $country
$capital=<STDIN>; # user inputs capital
chomp ($capital); # remove newline from $capital
print TEST "$country, $capital.\n"; # print the country and capital to the file
}
# the following will repeat until user inputs NO MORE
while ($country ne "NOMORE"){
print "\nEnter a country: ";
$country=<STDIN>;
chomp ($country);
if ($country eq "NOMORE") {
close (TEST); # close file if user enters NOMORE
}else{
print "Enter the capital of $country: ";
$capital=<STDIN>;
chomp ($capital);
print TEST "$country, $capital.\n"; # print to file
}
}
open(TEST, "<$test") or die "Could not open $test: $!\n"; # re-open file as read-only
# print contents of file
while(defined($a=<TEST>)) {
print "\n$a";
}
close (TEST); # close file after contents have been listed
This post has been edited by CatchThi5Drift: 27 October 2009 - 07:22 PM

New Topic/Question
Reply



MultiQuote




|