| | |
multiple choice test
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2007
Posts: 5
Reputation:
Solved Threads: 0
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:
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?
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:
c Syntax (Toggle Plain Text)
#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?
Last edited by Ancient Dragon; Oct 27th, 2007 at 8:28 am. Reason: add line numbers
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.
>>and prompt for an answer which then has to be converted to a number
>>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.
C Syntax (Toggle Plain Text)
while( (mc=getc(fin)) != EOF) { }
>>and prompt for an answer which then has to be converted to a number
C Syntax (Toggle Plain Text)
int answer = 0; printf("Enter a number\n"); scanf("%d", &answer);
Last edited by Ancient Dragon; Oct 27th, 2007 at 8:44 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Oct 2007
Posts: 5
Reputation:
Solved Threads: 0
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
but that didnt work either
any idea on how to solve that?
i also tried using
C Syntax (Toggle Plain Text)
if (strlen(mc)==1)
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
C Syntax (Toggle Plain Text)
FILE *fin,*fout; fin = fopen(INFILE, "r");
example:
C Syntax (Toggle Plain Text)
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?
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.
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
•
•
•
•
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.
Last edited by Aia; Oct 27th, 2007 at 5:55 pm.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
•
•
Join Date: Oct 2007
Posts: 5
Reputation:
Solved Threads: 0
after fiddling around with the code, ive progressed to :
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?
C Syntax (Toggle Plain Text)
#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?
•
•
•
•
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?
rt=="A) 1";
rt=="B) 2";
rt=="C) 3";
rt=="D) 4";
Is this how your book recommends loading a string into a variable?
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Oct 2007
Posts: 5
Reputation:
Solved Threads: 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...
C Syntax (Toggle Plain Text)
#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...
printf()'s and some puts(). Can you be a little more specific?Also, see this about your
scanf(). The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- how to develop online test (ASP.NET)
- Multiple choice quiz (Visual Basic 4 / 5 / 6)
- New user needs a little help on homework here :< (Pascal and Delphi)
- PHP Logic for Online Test (PHP)
- Skipping Questions in my ASP survey (ASP)
- Dr Scheme Tutorial (Computer Science)
- Please Help me in finding multiple choice question with answer on data structure. (Computer Science)
- 2-D array (C++)
- Array Assignment (Computer Science)
- Change in Certification Policy NT vs 2000 (Windows NT / 2000 / XP)
Other Threads in the C Forum
- Previous Thread: simple linked lists
- Next Thread: printing arrays
| Thread Tools | Search this Thread |
Tag cloud for C
#include adobe ansi array arrays asterisks binarysearch calculate centimeter changingto char convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic fflush file fork forloop framework getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware highest histogram inches include incrementoperators input iso kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard multi mysql number opendocumentformat opensource owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv repetition research reversing scanf scripting segmentationfault sequential shape socket socketprograming spoonfeeding standard string strings structures systemcall testing threads turboc unix user variable voidmain() wab windows.h windowsapi






