Thread: Help with a C Programing Quiz project

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    4

    Help with a C Programing Quiz project

    Hey all...

    Im having some issues writing a program to create a quiz in C.

    The basic layout is this....

    I need 10 questions-Multiple choice
    I then need to tally all the right and wrong answers and give a letter grade at the end.

    Can anyone help me out with this?

    I know its probably a snap for some of you but Im not too familier with this and am having a real hard time.

    If you could keep it as basic as possible that would be great as we havnt really learned too much advanced stuff in class !

    thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is certainly a snap. However, it is your homework, so you need to do show you are working on it, not ask US to do it for you. We will certainly HELP you, but not write it for you.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    If you're interested in more questions (both easy and hard), have a look at my C programming language quiz.

    No, I won't answer technical problems by PM. ;-)

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4
    Below is the code Ive come up with so far. Sorry if its not as pretty as others, Im new to this board and still working on it!
    Any way,what Im having trouble figuring out is what I need to do next which is take these 10 questions and when you are done answering them I need to tally up the number right, the number wrong and display a grade.

    Any tips would be greatly appreciated!!!
    Thanks

    Code:
    /* Programming in C FINAL PROJECT */
    
    
    #include <stdio.h>
    #include <iostream>
    #include <stdlib.h>
    
    	char Q1;
    	char Q2;
    	char Q3;
    	char Q4;
    	char Q5;
    	char Q6;	
    	char Q7;
    	char Q8;
    	char Q9;
    	char Q10;
    
    	int c,w;
    
    	main()
    
    {	
    	printf ("1-What is 1 + 1?\nA=1\nB=2\nC=3\nD=4\n");
    	scanf ("%c", &Q1);
    		if ((Q1 == 'B')||(Q1 == 'b'))
    		{c=c+1;
    		printf ("Correct Answer\n");
    			}
    		else 
    			{
    		w=w+1;
    		printf ("Wrong Answer\n");
    				
    			}
    system ("pause");
    system ("cls");
    
    		printf ("2-What is 2 + 2?\nA=4\nB=2\nC=6\nD=8\n");
    		scanf ("%s", &Q2);
    		if ((Q2 == 'A')||(Q2 == 'a'))
    	
    		c=c+1;
    		printf ("Correct Answer\n");
    			}
    		else 
    			{
    		w=w+1;
    		printf ("Wrong Answer\n");
    				
    			}
    system ("pause");
    system ("cls");
    
    		printf ("3-What is 4 + 4?\nA=2\nB=6\nC=8\nD=9\n");
    		scanf ("%s", &Q3);
    			if ((Q3 == 'C')||(Q3 == 'c'))
    			{c=c+1;
    		printf ("Correct Answer\n");
    			}
    		else 
    			{
    		w=w+1;
    		printf ("Wrong Answer\n");
    				
    			}
    system ("pause");
    system ("cls");
    
    		printf ("4-What is 4 + 7?\nA=2\nB=6\nC=2\nD=7\n");
    		scanf ("%s", &Q4);
    		if ((Q4 == 'D')||(Q4 == 'd'))
    			{c=c+1;
    		printf ("Correct Answer\n");
    			}
    		else 
    			{
    		w=w+1;
    		printf ("Wrong Answer\n");
    				
    			}
    system ("pause");
    system ("cls");
    
    		printf ("5-What is 1 + 3?\nA=2\nB=4\nC=3\nD=7\n");
    		scanf ("%s", &Q5);
    		if ((Q5 == 'B')||(Q5 == 'b'))
    			{c=c+1;
    		printf ("Correct Answer\n");
    			}
    		else 
    			{
    		w=w+1;
    		printf ("Wrong Answer\n");
    				
    			}
    system ("pause");
    system ("cls");
    
    		printf ("6-What is 1 + 6?\nA=7\nB=6\nC=5\nD=10\n");
    		scanf ("%s", &Q6);
    		if ((Q6 == 'A')||(Q6 == 'a'))
    			{	c=c+1;
    		printf ("Correct Answer\n");
    			}
    		else 
    			{
    		w=w+1;
    		printf ("Wrong Answer\n");
    				
    			}
    system ("pause");
    system ("cls");
    
    		printf ("7-What is 3 + 9?\nA=8\nB=18\nC=17\nD=12\n");
    		scanf ("%s", &Q7);
    		if ((Q7 == 'D')||(Q7 == 'd'))
    			{c=c+1;
    		printf ("Correct Answer\n");
    			}
    		else 
    			{
    		w=w+1;
    		printf ("Wrong Answer\n");
    				
    			}
    system ("pause");     /* Start FIXING FROM HERE */
    system ("cls");
    
    		printf ("8-What is 3 + 9?\nA=8\nB=18\nC=17\nD=12\n");
    		scanf ("%s", &Q7);
    		if ((Q7 == 'D')||(Q7 == 'd'))
    			{	c=c+1;
    		printf ("Correct Answer\n");
    			}
    		else 
    			{
    		w=w+1;
    		printf ("Wrong Answer\n");
    				
    			}
    system ("pause");
    system ("cls");
    
    		printf ("7-What is 3 + 9?\nA=8\nB=18\nC=17\nD=12\n");
    		scanf ("%s", &Q7);
    		if ((Q7 == 'D')||(Q7 == 'd'))
    			{c=c+1;
    		printf ("Correct Answer\n");
    			}
    		else 
    			{
    		w=w+1;
    		printf ("Wrong Answer\n");
    				
    			}
    system ("pause");
    system ("cls");
    
    		printf ("7-What is 3 + 9?\nA=8\nB=18\nC=17\nD=12\n");
    		scanf ("%s", &Q7);
    			if ((Q7 == 'D')||(Q7 == 'd'))
    			{	c=c+1;
    			printf ("Correct Answer\n");
    			}
    			else 
    			{
    				w=w+1;
    			printf ("Wrong Answer\n");
    				
    			}
    
    
    return 0;
    
    }

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    39
    What don't you use getchar() for character input instead?

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4
    Quote Originally Posted by ninety3gd View Post
    What don't you use getchar() for character input instead?
    Because Im not all that familier with C and this way works!

  7. #7
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    1)main() returns an int.
    2)You need a "%c" in scanf() not "%s"
    3)
    Code:
    #include <iostream>
    you don't need that c++ header as you are not using it here.
    Edit:
    You might want to check this thread:
    printf statement executed twice
    Last edited by stevesmithx; 05-12-2009 at 11:08 AM.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    @dilemma
    Any way,what Im having trouble figuring out is what I need to do next which is take these 10 questions and when you are done answering them I need to tally up the number right, the number wrong and display a grade.
    What is the problem then? Add a if statement in the end and print...

    Also, I would suggest you to make look-ups for question and answers and then run a loop instead of writing 10 same thing 10 times (the way you've done...) that way it will look much more sophisticated and that will add flexibility of adding more questions also whenever needed...

  9. #9
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Well, a loop might not be as easy except you know about arrays. I don't think so. But please, fix your indentation. And a neat trick

    Code:
    this = this + 1;
    is equivalent to

    Code:
    this++;

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    Well, without knowing about arrays it would be difficult...but looking at code's title that shouldn't be the case..what say...:

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    4
    I know very litte about arrays hence the method i used the write this. So far it does what I need it to do but still having problems figuring out how to tally the right and wrong and display a grade based on that!

    Any help would be appreciated!

    Thanks

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I would second the recommendations to use arrays and a loop, but other than that...

    how to tally the right and wrong and display a grade based on that!
    Well when you're done, c should contain the number right, and w should contain the number wrong. The expression

    Code:
    100*c/(c+w)
    will yield their percentage correct. A series of simple if statements can then help you determine the letter grade. Also, I think it would be better to just track how many they get correct, and then compare it with the total number of questions. Keeping track of the correct answers and the wrong answers and then adding them together to find the ratio of correct:total just seems a little messy to me, but that's really just a style issue.

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by sean View Post
    I would second the recommendations to use arrays and a loop, but other than that...
    And third!! Your own grade will not be good otherwise, because I *guarantee* the purpose of the assignment was to get you to use arrays and loops. It will also be much simpler in the end.

    Look:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char answers[5];  /* some arrays */
    char questions[5][8]={ "Where?", "When?", "What?", "Why?", "How?" };
    
    void answer_question(int num) {
    	printf("%s (enter a letter): ",questions[num]);
    	scanf("%c",&answers[num]);
    	getchar(); /* grab newline */
    } 
    
    int main() {
    	int i;
    	for (i=0; i<5; i++) answer_question(i); /* a loop */
    	printf("Your answers: %s\n", answers);	
    	return 0;
    }
    The part about grabbing the newline is so that the effect of pushing enter does not end up as part of the data; if you remove that line you will see what I mean.
    Last edited by MK27; 05-15-2009 at 03:45 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM