•
•
•
•
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
![]() |
•
•
Join Date: Oct 2007
Posts: 5
Reputation:
Rep Power: 0
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
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation:
Rep Power: 40
Solved Threads: 972
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.
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.
•
•
Join Date: Oct 2007
Posts: 5
Reputation:
Rep Power: 0
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
if (strlen(mc)==1)
but that didnt work either
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.
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
-- Pearl Williams
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:
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?
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
•
•
•
•
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.
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.
•
•
Join Date: Oct 2007
Posts: 5
Reputation:
Rep Power: 0
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?
#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?
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
-- Pearl Williams
•
•
Join Date: Oct 2007
Posts: 5
Reputation:
Rep Power: 0
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...
#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...
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
-- Pearl Williams
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
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 and Software Design)
- Please Help me in finding multiple choice question with answer on data structure. (Computer Science and Software Design)
- 2-D array (C++)
- Array Assignment (Computer Science and Software Design)
- Change in Certification Policy NT vs 2000 (Windows NT / 2000 / XP / 2003)
Other Threads in the C Forum
- Previous Thread: simple linked lists
- Next Thread: printing arrays



Linear Mode