For an assignment that im working on, we needed to accomplish the following:
Asks the user for their name, accepts and stores the answer as a string.
Opens a text file: questions.txt for reading
�Opens a text file: studentName.ans for writing, where studentName is the one
entered
Reads each line in questions.txt and passes it to choose() for display.
Writes to studentName.ans the question number, left parenthesis, and answer
number, then a newline character.
When the end of questions.txt is reached, prints "Thank you, test is completed.",
closes files, and exits.

In Addition,
write a function that fits the following declaration

int choose(char *list);
list is a pointer to a string. The string is separated into sections by semicolon ";"
characters. The first section is text for the question. Each following section is a
possible multiple choice answer. The choose() function prints the question to the
screen, and then each possible answer on a separate line. To each answer, it
prepends a lower case letter, a right parenthesis, and a space:
a) First answer \n
b) Second answer \n etc.
There is no fixed number of answers (but it is less than 27).
After the last answer, choose() prints "Indicate your answer with a letter: " and
then accepts an entered letter.
choose() returns an integer indicating which answer the user chose. a = 1, b = 2,
etc. It also will accept and correctly identify the corresponding upper case letters.
If the user enters any key other than the available choices, choose() returns a 0
to indicate the question was skipped.


now so far i have:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define INFILE "questions.txt"







/*switch(chg) {
		case 'a' : return rt=1;      //attempt at the list function
		break;
		case 'b' : return rt=2;
		break;
		case 'c' : return rt=3;
		break;
		case 'd' : return rt=4;
		break;
		case 'A' : return rt=1;
		break;
		case 'B' : return rt=2;
		break;
		case 'C' : return rt=3;
		break;
		case 'D' : return rt==;
		break;
		default: return 0;
		}*/
int main(int argc, char *argv[]) {
FILE *fin,*fout;
fin = fopen(INFILE, "r");
            char mc;
            char rt[80];
            char filename[80];
            char line[255];
		char chg;


printf("ECE 15 Assignment 4\n");
printf("Richard Fang\n");



printf("What is your name?\n");
fgets(line, 255, stdin);

if( line[strlen(line)-1] == '\n')
	line[strlen(line)-1] = '\0';
sprintf(filename,"%s.txt",line);
fout = fopen(filename, "w");

            
 // reads the questions file
    do {
    mc=getc(fin);


if (mc=='\n')      //at end of line asks for response
        { printf("\n");}
	else if(mc==1) {
 printf("please enter your response as a letter:");
        fgets(rt, 50, stdin);
if( line[strlen(line)-1] == '\n')
	line[strlen(line)-1] = '\0';
            printf("\n");
fputs(rt, fout); //writes to the answers file

}
else if(mc=='\t') {
     printf("\n");
     }
        else if(mc!='\t') { //prints questions to screen
             printf("%c", mc);


        }
 else if(mc==EOF) break;

        }
while ( mc!=EOF );   //closes file and ends program
getchar();
fclose (fin);

return 0;
}

the questions contained in my txt file are

How many eggs are in a dozen?
A) 35
B) 42
C) 11
D) 12

What rhymes with boot?
A) girl
B) root
C) hen
D) book

i figured out how to generate the student name into a file and how to write to it, but the problem arises when i try asking the question. how do i stop displaying text after question D) and prompt for an answer which then has to be converted to a number? i tried using the switch but for some reason it doesnt work out.

also, im not too sure what writing an int choose function means. does it mean it exists outside main and is simply refered to like a switch?

Comments
Thanks for using code tags correctly

line 36: filename needs to be defined a lot larger than that. If you are using MS-Windows operating system the max filename is 260 (or MAX_PATH if you include windows.h)

>>how do i stop displaying text after question D)
I suppose you mean that do loop that starts on line 56? change it to a while loop, then you can delete the checks for EOF inside that loop.

while( (mc=getc(fin)) != EOF)
{

}

>>and prompt for an answer which then has to be converted to a number

int answer = 0;
printf("Enter a number\n");
scanf("%d", &answer);

how do i get it to read until the end of question d? what i mean is, i try a loop until '\n' but that only would display one line. is there something that represents '\n\n'?
i also tried using

if (strlen(mc)==1)

but that didnt work either

any idea on how to solve that?

You need to read each line. The \n\n will take two lines. You can tell the second \n because the line will be extremely short. == may not be good, but < might work better. Depends on how consistent the blank lines are -- extra SPACEs for example.

FILE *fin,*fout;
fin = fopen(INFILE, "r");

When you try to open a file for reading you need to check afterwards if that file has been openned or does exist.
example:

FILE *fin;
fin = fopen( INFILE, "r" );
if ( fin == NULL )
{
    printf( "Error openning %s\n", INFILE );
    exit( 1 );
}

also, im not too sure what writing an int choose function means. does it mean it exists outside main and is simply refered to like a switch?

By not understanding this part, you are writing your code in a completely different direction that is requiered by the specifications.
You have to define a function prototyped as int choose(char *list); ( Yes, this is a function you have to create outside of main(). This function accepts a pointer to a string of characters as a parameter, and return an integer after is finished.

list is a pointer to a string. The string is separated into sections by semicolon ";" characters. The first section is text for the question. Each following section is a possible multiple choice answer.

This point named list is a long string where the question and the possible answers are delimited by ";". Meaning like this: How many eggs are in a dozen?;a) 35;b) 42;c) 11;d) 12;. Whether is in the file question.txt like that or not it doesn't say, but you need to pass every string like that to function choose(). That ";" is a token that you need to look for it to separate the sections into, and display the questions and multiple choice answer like:
How many eggs are in a dozen?
a) 35
b) 42
c) 11
d) 12
Click here for a link to some example of doing that.

To each answer, it prepends a lower case letter, a right parenthesis

The letters need to be in lower case, that's the specification.

After the last answer, choose() prints "Indicate your answer with a letter: " and
then accepts an entered letter.
choose() returns an integer indicating which answer the user chose. a = 1, b = 2,
etc. It also will accept and correctly identify the corresponding upper case letters.
If the user enters any key other than the available choices, choose() returns a 0
to indicate the question was skipped.

Display a printf to ask the user and create a switch that would process the entered input by the user, returning an integer value according to the answer.

after fiddling around with the code, ive progressed to :

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define INFILE "questions.txt"



int choose( char *list)
{
		char chg;
		char mc;
	char rt[80];

if (mc==';' && ' ')      //at end of line asks for response
        { printf("\n");		
			
			}

	else if(mc=='\n' && ';') {
 printf("please enter your response as a letter:");
  			scanf("%c", &chg);

switch(chg) {
		case 'a' :
		case 'A':
			 rt=="A) 1";
		break;
		case 'b' : 
		case 'B':
			rt=="B) 2";
		break;
		case 'c' : 
		case 'C':
			rt=="C) 3";
		break;
		case 'd' :
		case 'D':
			 rt=="D) 4";
		break;
		default: return 0;
		}

            printf("\n");
          
//writes to the answers file

}
else if(mc=='\t') {
     printf("\n");
     }
        else if(mc!='\t') { //prints questions to screen
             printf("%c", mc);


        }
 //else if(mc==EOF) break;

       return 0; }



int main(int argc, char *argv[]) {
FILE *fin,*fout;
fin = fopen(INFILE, "r");
if ( fin == NULL )
{
printf( "Error openning %s\n", INFILE );
return 0;

}
            char mc;
            char filename[260];
            char line[255];
	
		char rt[80];
     char mt;
	


printf("ECE 15 Assignment 4\n");
printf("Richard Fang\n");



printf("What is your name?\n");
fgets(line, 255, stdin);

if( line[strlen(line)-1] == '\n')
	line[strlen(line)-1] = '\0';
sprintf(filename,"%s.txt",line);
fout = fopen(filename, "w");

            
 // reads the questions file
    while(mc != EOF){
		
    mc=fgetc(fin);
		
choose(mc);
fputs(rt, fout); 
  //closes file and ends program
getchar();
fclose (fin);
fclose (fout);

return 0;}

but i still cant get main to point to my choose function, and also, when the program prints the answers to the txt file, it doesnt print the string but instead prints gibberish. any idea how to represent that string so that it is equal to char rt?

Perhaps you can work on indenting your code as well.
It's so bad that I'm not going to look at it, and I suspect that you're finding it hard to follow the flow of it in your editor as well.

when the program prints the answers to the txt file, it doesnt print the string but instead prints gibberish. any idea how to represent that string so that it is equal to char rt?

So what do you think these lines do?

rt=="A) 1";
rt=="B) 2";
rt=="C) 3";
rt=="D) 4";

Is this how your book recommends loading a string into a variable?

for some reason my program now only prints once character before prompting for a user response, though it was working before. im not too sure why...

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define INFILE "questions.txt"

int main(int argc, char *argv[]) {
FILE *fin,*fout;
fin = fopen(INFILE, "r");

if ( fin == NULL )
       {
       printf( "Error openning %s\n", INFILE );
       return 0;
       }
  char mc;
  char filename[260];
  char line[255];
  char rt[80];
  char mt;
  char chg;
		
printf("ECE 15 Assignment 4\n");
printf("Richard Fang\n");
printf("What is your name?\n");

fgets(line, 255, stdin);
    if( line[strlen(line)-1] == '\n')
	line[strlen(line)-1] = '\0';
sprintf(filename,"%s.txt",line);
fout = fopen(filename, "w");

 while(mc!=EOF){
    mc=fgetc(fin);
    if (mc!=';' )     
        { 
	printf("%c", mc);
	printf("\n");
         }
         else if(mc=='\n' ) 
 {
         printf("please enter your response as a letter:");
  
scanf("%c", &chg);
switch(chg) {
		case 'a' :
		case 'A':
			 strcpy(rt, "A) 1\n");
		break;
		case 'b' : 
		case 'B':
			 strcpy(rt,"B) 2\n");
		break;
		case 'c' : 
		case 'C':
			 strcpy(rt,"C) 3\n");
		break;
		case 'd' :
		case 'D':
			 strcpy(rt,"D) 4\n");
		break;
		default: return 0;
		}
  printf("\n");
  fputs(rt, fout); 
}
       return 0; }

else if(mc=='\t') {
printf("\n");

}

else if(mc!='\t') 
{ //prints questions to screen
     printf("%c", mc);

 }

else if(mc==EOF) break;
   //closes file and ends program
getchar();
fclose (fin);
fclose (fout);

return 0;}

for some reason my program now only prints once character before prompting for a user response, though it was working before. im not too sure why...

You have a dozen printf() 's and some puts() . Can you be a little more specific?

Also, see this about your scanf() .

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define INFILE "questions.txt"

 int main(int argc, char *argv[]) {
FILE *fin,*fout;
fin = fopen(INFILE, "r");

if ( fin == NULL )
       {
       printf( "Error openning %s\n", INFILE );
       return 0;
       }
  char mc;
  char filename[260];
  char line[255];
  char rt[80];
  char mt;
  char chg;
		
printf("ECE 15 Assignment 4\n");
printf("Richard Fang\n");
printf("What is your name?\n");

fgets(line, 255, stdin);
    if( line[strlen(line)-1] == '\n')
	line[strlen(line)-1] = '\0';
sprintf(filename,"%s.txt",line);
fout = fopen(filename, "w");

 while(mc!=EOF){
    mc=fgetc(fin);
    if (mc!=';' )     
        { 
	printf("%c", mc);
	printf("\n");
         }
         else if(mc==';')
{
          printf("\n");
}
         else if(mc=='\n' ) 
 {
         printf("please enter your response as a letter:");

  
         chg=getchar();
         switch(chg) {
		case 'a' :
		case 'A':
			 strcpy(rt, "A) 1\n");
		break;
		case 'b' : 
		case 'B':
			 strcpy(rt,"B) 2\n");
		break;
		case 'c' : 
		case 'C':
			 strcpy(rt,"C) 3\n");
		break;
		case 'd' :
		case 'D':
			 strcpy(rt,"D) 4\n");
		break;
		default: return 0;
		}
               printf("\n");
               fputs(rt, fout); 
}
       return 0; 
}





else if(mc==EOF) break;
   //closes file and ends program
getchar();
fclose (fin);
fclose (fout);

return 0;}

for some reason that doesnt work anymore. It only prints the first character of the question string then asks the "please enter your answer as a letter" prompt

printf("%c", mc);

>>It only prints the first character of the question string
>>printf("%c", mc);printf("%c", mc);

Well duuuh. That's exactuly what you told it to print. The "%c" says to print only a single character. if you want an entire null-terminated string then use "%s" and pass a pointer to the string you want printed.

A better formatting of your code would yield the reason why:
Take a look at the braces now.

while(mc != EOF)
    {
        mc=fgetc(fin);
        if (mc!=';' )     
        { 
            printf("%c", mc);
            printf("\n");
        }
        else if(mc==';')
        {
            printf("\n");
        }
        else if(mc=='\n' ) 
        {
            printf("please enter your response as a letter:");
            chg=getchar();
            
            switch(chg)
            {
                case 'a' :
                 case 'A':
                     strcpy(rt, "A) 1\n");
                    break;
                case 'b' : 
                case 'B':
                     strcpy(rt,"B) 2\n");
                    break;
                case 'c' : 
                case 'C':
                     strcpy(rt,"C) 3\n");
                    break;
                case 'd' :
                case 'D':
                     strcpy(rt,"D) 4\n");
                    break;
                default: return 0;
            }
            printf("\n");
            fputs(rt, fout); 
        }
        return 0; /* this return allow the while statement to loop only once and stop the program */
    }

I am not going to get too deep in the logic of your program, but I see that you wrote:

char mc;
mc=fgetc(fin);

mc needs to be an int in the event of a possible failure of fgetc, which would return a negative integer.

fgets(line, 255, stdin);

    if( line[strlen(line)-1] == '\n')
        line[strlen(line)-1] = '\0';
    sprintf(filename,"%s.txt",line);
    fout = fopen(filename, "w");

I can only guess what you intended to do here, but if I am correct this is a much proper way of doing checking always for the returned value of those functions used:

/* was fgets successful? */
if ( fgets( line, sizeof line / sizeof ( char ), stdin ) )
{
    /* obtain the string length */
    int length = strlen( line ) - 1;
    
    /* is the Enter key register in the string? */
    if ( line[ length ] == '\n' )
    {
        line[ length ] = '\0' /* eliminate the new line */
    }
    else
    {
        /* clearing the standard buffer of undesired characters */
        while ( ( lenght = getchar() ) != '\n' && length != EOF )
        {
            ; /* do nothing here */
        }
    }
    /* was sprintf succesful? */
    if ( sprintf( filename, "%s.txt", line ) > 4 )
    {
        fout = fopen( filename, "w" );
        if ( fout == NULL )
        {
            /* write some kind of error message here and deal with error */
        } /* end of if fout */
    } /* end of if sprintf */
} /* end of if fgets */
This article has been dead for over six months. Start a new discussion instead.