Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include int main(){ char filename [30]; FILE * ifp = NULL; int keys[7], i=0, num, input[7], hasht[101] = {0}, flag = 0; int count

#include #include

int main(){ char filename [30]; FILE * ifp = NULL; int keys[7], i=0, num, input[7], hasht[101] = {0}, flag = 0; int count = 0;

while(ifp == NULL){ printf("What is the name of the file? "); scanf("%s", filename); ifp = fopen(filename, "r"); }

// fclose(file);

printf("To get to me treasure you'll have to figure out which of me 100 keys are used in the 7 locks of me treasure chest. ");

while(1){ printf("Which keys will ye use? "); for(i = 0; i < 7; i++) scanf("%d", &input[i]);

for(i = 0; i < 7; i++){ if(hasht[(input[i] - 1)] == 0) hasht[(input[i] - 1)] = 1;

else{ printf("You can only use each key once, matey! "); flag = 1; } } if(flag != 1){ for(i = 0; i < 7; i++){ if(hasht[(keys[i] - 1)] != 1) break; else count++; } if(count != 7) printf("%d of those keys are correct, matey! But are they in the right order? ", count); else{

for(i = 0; i < 7; i++){ if(input[i] != keys[i]) break; }

if(i == 7){ printf("Arr! You've opened me treasure chest and found... "); printf("A map! To the rest of me treasure on Treasure Island. "); printf("Hahaha! "); break;

}else{ printf("7 of those keys are correct, matey! But are they in the right order? "); } } } } return 0; }

I have this code im prompting the use to input a .txt file

'input.txt'

(which has the information below on it)

2 14 74 8 36 4 11

however after testing the code I get a program crash.

The first WHILE loop seems to be causing the issue but im not to sure how to fix it

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transact SQL Cookbook Help For Database Programmers

Authors: Ales Spetic, Jonathan Gennick

1st Edition

1565927567, 978-1565927568

More Books

Students also viewed these Databases questions

Question

List the benefits of strategic planning.

Answered: 1 week ago