| | |
multiple choice test
![]() |
•
•
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 7: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 7:44 am.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
•
•
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 4:55 pm.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan stating how a liberal's mind works.
•
•
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?
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.
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
--
If your code lacks code tags, you will be IGNORED
--
If your code lacks code tags, you will be IGNORED
•
•
•
•
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
Views: 5333 | Replies: 12
| Thread Tools | Search this Thread |
Tag cloud for C
api arguments array arrays binary binarysearch c++ char character code codes coke command conversion convert copy copyimagefile cpu database decimal directory dude dynamic ebook ebooks error exec factorial fgets file fork function functions getlasterror givemetehcodez grade graphics homework i/o input insert int integer lazy libcurl line linked linkedlist linux list lists locate logical_drives loop loops malloc matrix memory messagebox motherboard mysql no-effort opensource output path pause pointer pointers problem process program programming questions read recursion recursive recv reverse scanf single socketprograming socketprogramming spoonfeeding sql static string strings strtok structures student suggestions system systemcall turbo turbo-c turboc unix user variable windows






