multiple choice test

Reply

Join Date: Oct 2007
Posts: 5
Reputation: crunchycrisp is an unknown quantity at this point 
Solved Threads: 0
crunchycrisp crunchycrisp is offline Offline
Newbie Poster

multiple choice test

 
1
  #1
Oct 27th, 2007
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:
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #define INFILE "questions.txt"
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12. /*switch(chg) {
  13. case 'a' : return rt=1; //attempt at the list function
  14. break;
  15. case 'b' : return rt=2;
  16. break;
  17. case 'c' : return rt=3;
  18. break;
  19. case 'd' : return rt=4;
  20. break;
  21. case 'A' : return rt=1;
  22. break;
  23. case 'B' : return rt=2;
  24. break;
  25. case 'C' : return rt=3;
  26. break;
  27. case 'D' : return rt==;
  28. break;
  29. default: return 0;
  30. }*/
  31. int main(int argc, char *argv[]) {
  32. FILE *fin,*fout;
  33. fin = fopen(INFILE, "r");
  34. char mc;
  35. char rt[80];
  36. char filename[80];
  37. char line[255];
  38. char chg;
  39.  
  40.  
  41. printf("ECE 15 Assignment 4\n");
  42. printf("Richard Fang\n");
  43.  
  44.  
  45.  
  46. printf("What is your name?\n");
  47. fgets(line, 255, stdin);
  48.  
  49. if( line[strlen(line)-1] == '\n')
  50. line[strlen(line)-1] = '\0';
  51. sprintf(filename,"%s.txt",line);
  52. fout = fopen(filename, "w");
  53.  
  54.  
  55. // reads the questions file
  56. do {
  57. mc=getc(fin);
  58.  
  59.  
  60. if (mc=='\n') //at end of line asks for response
  61. { printf("\n");}
  62. else if(mc==1) {
  63. printf("please enter your response as a letter:");
  64. fgets(rt, 50, stdin);
  65. if( line[strlen(line)-1] == '\n')
  66. line[strlen(line)-1] = '\0';
  67. printf("\n");
  68. fputs(rt, fout); //writes to the answers file
  69.  
  70. }
  71. else if(mc=='\t') {
  72. printf("\n");
  73. }
  74. else if(mc!='\t') { //prints questions to screen
  75. printf("%c", mc);
  76.  
  77.  
  78. }
  79. else if(mc==EOF) break;
  80.  
  81. }
  82. while ( mc!=EOF ); //closes file and ends program
  83. getchar();
  84. fclose (fin);
  85.  
  86. return 0;
  87. }


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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,631
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1615
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: multiple choice test

 
0
  #2
Oct 27th, 2007
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.
  1. while( (mc=getc(fin)) != EOF)
  2. {
  3.  
  4. }

>>and prompt for an answer which then has to be converted to a number
  1. int answer = 0;
  2. printf("Enter a number\n");
  3. 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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 5
Reputation: crunchycrisp is an unknown quantity at this point 
Solved Threads: 0
crunchycrisp crunchycrisp is offline Offline
Newbie Poster

Re: multiple choice test

 
0
  #3
Oct 27th, 2007
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
  1. if (strlen(mc)==1)
but that didnt work either

any idea on how to solve that?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 4,332
Reputation: WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future 
Solved Threads: 415
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Industrious Poster

Re: multiple choice test

 
0
  #4
Oct 27th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,099
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 185
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: multiple choice test

 
0
  #5
Oct 27th, 2007
  1. FILE *fin,*fout;
  2. 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:
  1. FILE *fin;
  2. fin = fopen( INFILE, "r" );
  3. if ( fin == NULL )
  4. {
  5. printf( "Error openning %s\n", INFILE );
  6. exit( 1 );
  7. }

Originally Posted by crunchycrisp View Post
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 5
Reputation: crunchycrisp is an unknown quantity at this point 
Solved Threads: 0
crunchycrisp crunchycrisp is offline Offline
Newbie Poster

Re: multiple choice test

 
0
  #6
Oct 29th, 2007
after fiddling around with the code, ive progressed to :

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #define INFILE "questions.txt"
  5.  
  6.  
  7.  
  8. int choose( char *list)
  9. {
  10. char chg;
  11. char mc;
  12. char rt[80];
  13.  
  14. if (mc==';' && ' ') //at end of line asks for response
  15. { printf("\n");
  16.  
  17. }
  18.  
  19. else if(mc=='\n' && ';') {
  20. printf("please enter your response as a letter:");
  21. scanf("%c", &chg);
  22.  
  23. switch(chg) {
  24. case 'a' :
  25. case 'A':
  26. rt=="A) 1";
  27. break;
  28. case 'b' :
  29. case 'B':
  30. rt=="B) 2";
  31. break;
  32. case 'c' :
  33. case 'C':
  34. rt=="C) 3";
  35. break;
  36. case 'd' :
  37. case 'D':
  38. rt=="D) 4";
  39. break;
  40. default: return 0;
  41. }
  42.  
  43. printf("\n");
  44.  
  45. //writes to the answers file
  46.  
  47. }
  48. else if(mc=='\t') {
  49. printf("\n");
  50. }
  51. else if(mc!='\t') { //prints questions to screen
  52. printf("%c", mc);
  53.  
  54.  
  55. }
  56. //else if(mc==EOF) break;
  57.  
  58. return 0; }
  59.  
  60.  
  61.  
  62. int main(int argc, char *argv[]) {
  63. FILE *fin,*fout;
  64. fin = fopen(INFILE, "r");
  65. if ( fin == NULL )
  66. {
  67. printf( "Error openning %s\n", INFILE );
  68. return 0;
  69.  
  70. }
  71. char mc;
  72. char filename[260];
  73. char line[255];
  74.  
  75. char rt[80];
  76. char mt;
  77.  
  78.  
  79.  
  80. printf("ECE 15 Assignment 4\n");
  81. printf("Richard Fang\n");
  82.  
  83.  
  84.  
  85. printf("What is your name?\n");
  86. fgets(line, 255, stdin);
  87.  
  88. if( line[strlen(line)-1] == '\n')
  89. line[strlen(line)-1] = '\0';
  90. sprintf(filename,"%s.txt",line);
  91. fout = fopen(filename, "w");
  92.  
  93.  
  94. // reads the questions file
  95. while(mc != EOF){
  96.  
  97. mc=fgetc(fin);
  98.  
  99. choose(mc);
  100. fputs(rt, fout);
  101. //closes file and ends program
  102. getchar();
  103. fclose (fin);
  104. fclose (fout);
  105.  
  106. 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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 6,605
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 854
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: multiple choice test

 
0
  #7
Oct 29th, 2007
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.
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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 4,332
Reputation: WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future 
Solved Threads: 415
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Industrious Poster

Re: multiple choice test

 
0
  #8
Oct 29th, 2007
Originally Posted by crunchycrisp View Post
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?
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 5
Reputation: crunchycrisp is an unknown quantity at this point 
Solved Threads: 0
crunchycrisp crunchycrisp is offline Offline
Newbie Poster

Re: multiple choice test

 
0
  #9
Oct 29th, 2007
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...

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #define INFILE "questions.txt"
  5.  
  6. int main(int argc, char *argv[]) {
  7. FILE *fin,*fout;
  8. fin = fopen(INFILE, "r");
  9.  
  10. if ( fin == NULL )
  11. {
  12. printf( "Error openning %s\n", INFILE );
  13. return 0;
  14. }
  15. char mc;
  16. char filename[260];
  17. char line[255];
  18. char rt[80];
  19. char mt;
  20. char chg;
  21.  
  22. printf("ECE 15 Assignment 4\n");
  23. printf("Richard Fang\n");
  24. printf("What is your name?\n");
  25.  
  26. fgets(line, 255, stdin);
  27. if( line[strlen(line)-1] == '\n')
  28. line[strlen(line)-1] = '\0';
  29. sprintf(filename,"%s.txt",line);
  30. fout = fopen(filename, "w");
  31.  
  32. while(mc!=EOF){
  33. mc=fgetc(fin);
  34. if (mc!=';' )
  35. {
  36. printf("%c", mc);
  37. printf("\n");
  38. }
  39. else if(mc=='\n' )
  40. {
  41. printf("please enter your response as a letter:");
  42.  
  43. scanf("%c", &chg);
  44. switch(chg) {
  45. case 'a' :
  46. case 'A':
  47. strcpy(rt, "A) 1\n");
  48. break;
  49. case 'b' :
  50. case 'B':
  51. strcpy(rt,"B) 2\n");
  52. break;
  53. case 'c' :
  54. case 'C':
  55. strcpy(rt,"C) 3\n");
  56. break;
  57. case 'd' :
  58. case 'D':
  59. strcpy(rt,"D) 4\n");
  60. break;
  61. default: return 0;
  62. }
  63. printf("\n");
  64. fputs(rt, fout);
  65. }
  66. return 0; }
  67.  
  68. else if(mc=='\t') {
  69. printf("\n");
  70.  
  71. }
  72.  
  73. else if(mc!='\t')
  74. { //prints questions to screen
  75. printf("%c", mc);
  76.  
  77. }
  78.  
  79. else if(mc==EOF) break;
  80. //closes file and ends program
  81. getchar();
  82. fclose (fin);
  83. fclose (fout);
  84.  
  85. return 0;}
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 4,332
Reputation: WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future 
Solved Threads: 415
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Industrious Poster

Re: multiple choice test

 
0
  #10
Oct 29th, 2007
Originally Posted by crunchycrisp View Post
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().
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 5333 | Replies: 12
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC