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?