User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 456,573 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,573 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 2456 | Replies: 12
Reply
Join Date: Oct 2007
Posts: 5
Reputation: crunchycrisp is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
crunchycrisp crunchycrisp is offline Offline
Newbie Poster

multiple choice test

  #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 8:28 am. Reason: add line numbers
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: multiple choice test

  #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.
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);
Last edited by Ancient Dragon : Oct 27th, 2007 at 8:44 am.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Oct 2007
Posts: 5
Reputation: crunchycrisp is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
crunchycrisp crunchycrisp is offline Offline
Newbie Poster

Re: multiple choice test

  #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
 if (strlen(mc)==1)

but that didnt work either

any idea on how to solve that?
Reply With Quote  
Join Date: May 2006
Posts: 2,781
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 229
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: multiple choice test

  #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.
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
Reply With Quote  
Join Date: Dec 2006
Posts: 1,569
Reputation: Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold 
Rep Power: 12
Solved Threads: 114
Aia's Avatar
Aia Aia is offline Offline
Posting Virtuoso

Re: multiple choice test

  #5  
Oct 27th, 2007
 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:
  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 5:55 pm.
At the very moment that I find myself in the side of the mayority, I will know that I need to re-think my ideas. ~ In my book.
Reply With Quote  
Join Date: Oct 2007
Posts: 5
Reputation: crunchycrisp is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
crunchycrisp crunchycrisp is offline Offline
Newbie Poster

Re: multiple choice test

  #6  
Oct 29th, 2007
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?
Reply With Quote  
Join Date: Dec 2005
Posts: 3,834
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 436
Colleague
Salem's Avatar
Salem Salem is offline Offline
banned

Re: multiple choice test

  #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.
Reply With Quote  
Join Date: May 2006
Posts: 2,781
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 229
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: multiple choice test

  #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?
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
Reply With Quote  
Join Date: Oct 2007
Posts: 5
Reputation: crunchycrisp is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
crunchycrisp crunchycrisp is offline Offline
Newbie Poster

Re: multiple choice test

  #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...

#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;}
Reply With Quote  
Join Date: May 2006
Posts: 2,781
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 229
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: multiple choice test

  #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().
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 6:08 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC