Algorithm
step1: display 3 choices
step2: user inputs one choice
step3: Choice is store in one variable
step4: selected choice is transfered to switch function
step5: choice is compare to cases
step6: a text document is created stating you choice.

This is layout of the code:

#include<stdio.h>

void main()
{
char chois;
printf("Enter your choise");
scanf("%c",&chois);

switch(chois)
{
    case 1:
    FILE *fp;
        fp=fopen("file1.txt","w");
        fprintf(fp,"%s","You choosed 1");
        fclose(fp);

    case 2:
FILE *fp;
        fp=fopen("file2.txt","w");
        fprintf(fp,"%s","You choose 2");
        fclose(fp);
    break;

    case 3:
FILE *fp;
        fp=fopen("file3.txt","w");
        fprintf(fp,"%s","You choose 3");
        fclose(fp);
    break;

    default:
FILE *fp;
        fp=fopen("file4.txt","w");
        fprintf(fp,"%s","Invalid CHooise");
        fclose(fp);
    break;
}

 }

Recommended Answers

All 9 Replies

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.

Thanx a lot I had made the changes and it is working correctly

#include<stdio.h>

void main()
{
int a;
printf("Enter your choise ");
scanf("%d",&a);
FILE *fp;
switch(a)
{
    case 1:
    printf("printing");
        fp=fopen("file1.txt","w");
        fprintf(fp,"%s","chois 1");
        fclose(fp);
printf("successfully");
    break;
    case 2:
    printf("printing");
fp=fopen("file2.txt","w");
        fprintf(fp,"%s","chois 2");
        fclose(fp);
        printf("successfully");
    break;
    case 3:
    printf("printing");
fp=fopen("file3.txt","w");
        fprintf(fp,"%s","chois 3");
        fclose(fp);
        printf("successfully");
    break;
    default:
    printf("printing");
fp=fopen("file4.txt","w");
        fprintf(fp,"%s","invalid chois 1");
        fclose(fp);
        printf("successfully");
    break;
}


 }

Sir Please check this code .Is it correct or is there any way to make it still small

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.

Thanxx alot once agin sir.. :)

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;
}

It is givning an Error "Call to undefined function'free' in function main()"

just delete line 21, not needed since those pointers were not allocated by calls to malloc()

Great sir, Hats off.....
It worked

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.