line 12: Do not declare variables in switch statements like that, but declare FILE* at the top of main() so that it is global to the entire function.
Ancient Dragon
Achieved Level 70
32,274 posts since Aug 2005
Reputation Points: 5,852
Solved Threads: 2,590
Skill Endorsements: 70
Except for line 3 it seems to meet the requirements of the instructions you originally posted. line 3 should be: int main() because main() always returns an int.
Ancient Dragon
Achieved Level 70
32,274 posts since Aug 2005
Reputation Points: 5,852
Solved Threads: 2,590
Skill Endorsements: 70
Well, to make it smaller, you could do like this:
#include <stdio.h>
#include <string.h>
int main(){
FILE *f;
char cho;
char filename[] = "text .txt";
char text[] = "Your choice is .\n";
char* error,* errtxt;
printf("Insert choice: ");
scanf ("%c", &cho);
switch(cho){
case '1': filename[4] = text[15] = cho; break;
case '2': filename[4] = text[15] = cho; break;
case '3': filename[4] = text[15] = cho; break;
default:
error = "error.txt";
errtxt = "Invalid choice.\n";
strcpy(filename, error);
strcpy(text, errtxt);
free (error); free (errtxt);
break;
}
f = fopen(filename, "w");
fputs(text, f);
fclose(f);
return 0;
}
Lucaci Andrew
Practically a Master Poster
690 posts since Jan 2012
Reputation Points: 108
Solved Threads: 97
Skill Endorsements: 13
just delete line 21, not needed since those pointers were not allocated by calls to malloc()
Ancient Dragon
Achieved Level 70
32,274 posts since Aug 2005
Reputation Points: 5,852
Solved Threads: 2,590
Skill Endorsements: 70
Question Answered as of 4 Months Ago by
Ancient Dragon
and
Lucaci Andrew