8 Replies - 492 Views - Last Post: 11 December 2012 - 07:35 AM Rate Topic: -----

#1 schroeder  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 10-December 12

My first C++ Program - Comments Requested

Posted 10 December 2012 - 01:33 PM

Hey there,

I decided to start teaching myself C++ this weekend.
For my first project, I wanted to make a multiple choice quiz to help my girlfriend with a state-regulated exam she has coming up (to get her Hair Cutting License).

The quiz is 25 questions long - to make it easier on your eyes, I've only included three of the questions.
The first question is commented throughout, to show you my intentions for each line of code.

As I am a total n00b to C++, I would greatly appreciate anyone who would take a minute and look over my code and give me some feedback!



#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{

//#######################################################################################
// Introduction to the program - Instructions on how to take the quiz.	

	cout<<"This is a multiple choice quiz in Hair Cut Theory"<<endl<<endl<<endl;
	cout<<"INSTRUCTIONS:"<<endl<<endl;
	cout<<"I will ask you a series of questions, to which you may reply by"<<endl;
	cout<<"pressing 'a', 'b', 'c', or 'd' followed by the 'enter' key."<<endl<<endl<<endl;
	system("pause");
	system("cls");

//#######################################################################################
// Introduces the variable that keeps track of the score.

	int addscore = 0;

//#######################################################################################
// Question 1.

	char answer1 = 'b';     //Introduces the correct answer to the problem.
	char input1;            //The user input variable for the question.
	cout<<"Question 1."<<endl<<endl;
	cout<<"Achieving balance within a design can be accomplished"<<endl;   //The Question.
	cout<<"by understanding the head shape and:"<<endl<<endl;
	cout<<"a. Head Points"<<endl;
	cout<<"b. Reference Points"<<endl;                   //The possible answers.
	cout<<"c. Four Corners"<<endl;
	cout<<"d. Technique Points"<<endl<<endl;
	cin.get(input1);        //Prompt the user for the answer to the question
	cout<<endl;
		if (input1 == answer1)     //If the user input matches the correct answer, then...
		{
			cout<<"You are correct!"<<endl<<endl;    //Display praise to the user...
			addscore++;       //and add +1 to the score count.
		}
		else {           //If the user input is incorrect, then display the correct answer and move on.
			cout<<"I'm sorry, but the correct answer was "<<answer1<<"."<<endl<<endl;
		}
		cin.get();
		system("pause");
		system("cls");

//#######################################################################################
// Question 2.

	char answer2 = 'a';
	char input2;
	cout<<"Question 2."<<endl<<endl;
	cout<<"The location of the four corners signals a change in the:"<<endl<<endl;
	cout<<"a. Head Shape"<<endl;
	cout<<"b. Bone Shape"<<endl;
	cout<<"c. Hair Texture"<<endl;
	cout<<"d. Hair Growth"<<endl<<endl;
	cin.get(input2);
	cout<<endl;
		if (input2 == answer2)
		{
			cout<<"You are correct!"<<endl<<endl;
			addscore++;
		}
		else {
			cout<<"I'm sorry, but the correct answer was "<<answer2<<"."<<endl<<endl;
		}
		cin.get();
		system("pause");
		system("cls");

//#######################################################################################
// Question 3.

	char answer3 = 'c';
	char input3;
	cout<<"Question 3."<<endl<<endl;
	cout<<"The two front corners represent the widest part of the:"<<endl<<endl;
	cout<<"a. Apex Area"<<endl;
	cout<<"b. Parietal Area"<<endl;
	cout<<"c. Bang Area"<<endl;
	cout<<"d. Nape Area"<<endl<<endl;
	cin.get(input3);
	cout<<endl;
		if (input3 == answer3)
		{
			cout<<"You are correct!"<<endl<<endl;
			addscore++;
		}
		else {
			cout<<"I'm sorry, but the correct answer was "<<answer3<<"."<<endl<<endl;
		}
		cin.get();
		system("pause");
		system("cls");

//#######################################################################################

// This is where the other 22 questions go - removed from the code to save space - you get the idea...

//#######################################################################################
// Test Results - The score is added up and presented to the user.

		cout<<"TEST RESULTS"<<endl<<endl<<endl;
		cout<<"You answered "<<addscore<<" of 25 questions correctly!"<<endl<<endl;

		system("pause");
}



Thank you for taking the time to view my post, and my beginner code.
Hopefully it didn't make your eyes bleed ;)

Any comments that you would like to share would be greatly appreciated!

ps - it looks like some of my code got structured a bit weird... I'm new to the forums, so any advice on uploading source code would be great as well. Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: My first C++ Program - Comments Requested

#2 DevonZ  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 73
  • Joined: 04-August 12

Re: My first C++ Program - Comments Requested

Posted 10 December 2012 - 03:11 PM

Not bad for a first program, and a good idea too. You can move right along with your learning by getting into using an array and loops. After, read up on file I/O and load your questions from the files into arrays and loop through the arrays accordingly. Later, you could then move the code into structs and objects.

At that point you're code would be pretty short and sweet - and you could load 1000 questions if you wanted. However, it's bad practice to load large text files into an array as it wastes memory. You could keep the file stream open and go through them one question at a time.

Basically, you need to think about how you can segment your code and prevent repetition. Functions, methods, loops, structs, classes, ect.

This post has been edited by DevonZ: 10 December 2012 - 03:13 PM

Was This Post Helpful? 0
  • +
  • -

#3 schroeder  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 10-December 12

Re: My first C++ Program - Comments Requested

Posted 10 December 2012 - 03:36 PM

Hey,

For my first C++ project, I've decided to make a multiple choice quiz for my girlfriend - she is studying for her state-regulated exam to become a licensed hair stylist.

Currently there are 25 questions that get asked (with multiple choice answers (a, b, c or d)).
If you get the answer right: you are told so, a point gets added to the score, and you move on to the next question.
If you get the answer wrong: you are told the correct answer, and you move on to the next question.
At the end, a final score is given to the user.
Pretty basic stuff..

I'd like to step this up a notch....

A)How could I set the program up to ask the questions in a random order?

B)Right now the scoring system simply tallies the total number of correct answers.
If I wanted to take that number and convert it to a percentage that is associated with a grade (a, b, c, d, f), how would I do that?

C)I asked this question in another post, and got some great answers, but I wasn't really following some of the more detailed responses - a bit over my head at this point, but I want to do it anyways.... How could I set up a "table of contents" at the beginning of the program - one that would allow the user to select a section of questions to jump to?
One of the responses mentioned using IF and ELSE IF statements, but how exactly would I implement them in this code?

Here is my code for the program so far.
Please note, I'm only showing 3 of the 25 questions here, as the code is pretty repetitive - you get the idea.

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{

//#######################################################################################
// Introduction to the program - Instructions on how to take the quiz.	

	cout<<"This is a multiple choice quiz in Hair Cut Theory"<<endl<<endl<<endl;
	cout<<"INSTRUCTIONS:"<<endl<<endl;
	cout<<"I will ask you a series of questions, to which you may reply by"<<endl;
	cout<<"pressing 'a', 'b', 'c', or 'd' followed by the 'enter' key."<<endl<<endl<<endl;
	system("pause");
	system("cls");

//#######################################################################################
// Introduces the variable that keeps track of the score.

	int addscore = 0;

//#######################################################################################
// Question 1.

	char answer1 = 'b';
	char input1;
	cout<<"Question 1."<<endl<<endl;
	cout<<"Achieving balance within a design can be accomplished"<<endl;
	cout<<"by understanding the head shape and:"<<endl<<endl;
	cout<<"a. Head Points"<<endl;
	cout<<"b. Reference Points"<<endl;
	cout<<"c. Four Corners"<<endl;
	cout<<"d. Technique Points"<<endl<<endl;
	cin.get(input1);
	cout<<endl;
		if (input1 == answer1)
		{
			cout<<"You are correct!"<<endl<<endl;
			addscore++;
		}
		else {
			cout<<"I'm sorry, but the correct answer was "<<answer1<<"."<<endl<<endl;
		}
		cin.get();
		system("pause");
		system("cls");

//#######################################################################################
// Question 2.

	char answer2 = 'a';
	char input2;
	cout<<"Question 2."<<endl<<endl;
	cout<<"The location of the four corners signals a change in the:"<<endl<<endl;
	cout<<"a. Head Shape"<<endl;
	cout<<"b. Bone Shape"<<endl;
	cout<<"c. Hair Texture"<<endl;
	cout<<"d. Hair Growth"<<endl<<endl;
	cin.get(input2);
	cout<<endl;
		if (input2 == answer2)
		{
			cout<<"You are correct!"<<endl<<endl;
			addscore++;
		}
		else {
			cout<<"I'm sorry, but the correct answer was "<<answer2<<"."<<endl<<endl;
		}
		cin.get();
		system("pause");
		system("cls");

//#######################################################################################
// Question 3.

	char answer3 = 'c';
	char input3;
	cout<<"Question 3."<<endl<<endl;
	cout<<"The two front corners represent the widest part of the:"<<endl<<endl;
	cout<<"a. Apex Area"<<endl;
	cout<<"b. Parietal Area"<<endl;
	cout<<"c. Bang Area"<<endl;
	cout<<"d. Nape Area"<<endl<<endl;
	cin.get(input3);
	cout<<endl;
		if (input3 == answer3)
		{
			cout<<"You are correct!"<<endl<<endl;
			addscore++;
		}
		else {
			cout<<"I'm sorry, but the correct answer was "<<answer3<<"."<<endl<<endl;
		}
		cin.get();
		system("pause");
		system("cls");

//#######################################################################################

// This is where the other 22 questions go - removed from the code to save space - you get the idea...

//#######################################################################################
// Test Results - The score is added up and presented to the user.

		cout<<"TEST RESULTS"<<endl<<endl<<endl;
		cout<<"You answered "<<addscore<<" of 25 questions correctly!"<<endl<<endl;

		system("pause");
}





Thanks in advance for the help guys!!!!
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: My first C++ Program - Comments Requested

Posted 10 December 2012 - 06:32 PM

You need to look into functions. Your code has a very repetative structure.

ask question
get answer
compare answer
output message

If you could generalize the process, you could save yourself a huge amount of code.
Was This Post Helpful? 0
  • +
  • -

#5 schroeder  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 10-December 12

Re: My first C++ Program - Comments Requested

Posted 10 December 2012 - 07:36 PM

View Postjjl, on 10 December 2012 - 06:32 PM, said:

You need to look into functions. Your code has a very repetative structure.

ask question
get answer
compare answer
output message

If you could generalize the process, you could save yourself a huge amount of code.


Thank you for your reply!

May I ask, how would you go about simplifying the code by the use of functions?
Each question has its own User Input Answer and Correct Answer, so I'm not sure how I could request/compare/etc for the various questions with functions.

Total noob here - any guidance would be appreciated!
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: My first C++ Program - Comments Requested

Posted 10 December 2012 - 08:48 PM

Quote

Each question has its own User Input Answer and Correct Answer, so I'm not sure how I could request/compare/etc for the various questions with functions.


If you could create a function that took in parameters for the question, choices, and the correct answer. You could write it one time, and call that function for every question that you like.

Take a look at Jack's tutorial on functions: http://www.dreaminco...t-i-the-basics/


Once you read the tutorial, create a function that has the following structure
bool askQuestion(const char *question, const char *choices, char answer) 
{

   //your code
}



The function should print the question and choices string. All you need to do is take in input (using cin) for the users choice and compare it to the correct answer passed in as a parameter.

Here are examples of how the function be called, after it has been implemented by you :)/>/>

bool askQuestion(const char *question, const char *choices, char answer) 
{

   //your code
}

int main() {
   askQuestion("What is 2+2?", //the question
               "a) 2 e) 3 c) 4 d) 5", //the choices
               'c'); //the correct answer

   askQuestion("What color is grass?", //the question
            "a) green e) blue c) red d) black", //the choices
            'a'); //the correct answer

   return 0;

}


This post has been edited by jjl: 10 December 2012 - 08:54 PM

Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7764
  • View blog
  • Posts: 13,127
  • Joined: 19-March 11

Re: My first C++ Program - Comments Requested

Posted 10 December 2012 - 09:11 PM

View PostDevonZ, on 10 December 2012 - 05:11 PM, said:

Not bad for a first program, and a good idea too. You can move right along with your learning by getting into using an array and loops. After, read up on file I/O and load your questions from the files into arrays and loop through the arrays accordingly. Later, you could then move the code into structs and objects.

At that point you're code would be pretty short and sweet - and you could load 1000 questions if you wanted. However, it's bad practice to load large text files into an array as it wastes memory. You could keep the file stream open and go through them one question at a time.



Well, let's not worry too much about that just now. After all, you're much more likely to run out of human patience before you run out of machine space fort his particular application. Reading into an array should be fine for something like this, at least as a next step.



Quote

Basically, you need to think about how you can segment your code and prevent repetition. Functions, methods, loops, structs, classes, ect.



This, however, I agree with completely. schroederI'd refer you back to the rough framework I posted earlier for this. You might want to try a few toy aplications with loops and arrays first, before you start monkeying with your masterwork here.

For example, if you have an array of strings, you should be able to print them out in reverse order, or only the even numbered ones, or the odd numbered ones, or so forth, by controlling the loop variable. Try playing with that, using a short array of strings as your data. {"apple", "banana", "cherry", "date", "elderberry", "fig", "guava", "huckleberry"} should be enough to work with for this.

Once you can do that, you might want to try reading from a file - for example, try to read lines from a file and print them to the console output. Having done that, try to insert the lines into an array. Think about how you'd read in multi-line strings. You can assume that there is a blank line between strings.

You see where this goes, right? Once you've done those things, you'll have done pretty much everything you need to read your questions from a file and put them into an array. This makes life easier. For example, you can compose different quizzes by putting different question sets into different test files. (you'll have to get the test answers into memory as well... how will you do that? there are a few good answers, see if you can come up with two)

From what you've shown us so far, you seem pretty smart and pretty independent of mind, so I'm going to assume that you're capable of doing the research to try these things out without me explaining them, but do eel free to ask questions if you find something doesn't make sense.
Was This Post Helpful? 0
  • +
  • -

#8 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: My first C++ Program - Comments Requested

Posted 10 December 2012 - 09:50 PM

Well, if you observe, you will notice a pattern in the form of question, options, answer.

You can use that pattern to create a function. In that way,you will be able to eliminate a large amount of lines for your code.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 4075
  • View blog
  • Posts: 12,578
  • Joined: 25-December 09

Re: My first C++ Program - Comments Requested

Posted 11 December 2012 - 07:35 AM

Please don't open multiple topics for the same problem. Topics merged.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1