GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Nov-2004, 02:23
brookeville brookeville is offline
Junior Member
 
Join Date: Oct 2004
Posts: 56
brookeville is on a distinguished road

Simple question on arrays--please help!


Hello everyone, I have a simple question that I must ask regarding a program I've been working on.

My program is as follows:
CPP / C++ / C Code:
#include <iostream>
using namespace std;

char correct_answers[20] = {'B','D','A','A','C','A','B','A','C','D',
'B','C','D','A','D','C','C','B','D','A'};



int main()
{
	cout << "Enter CAPITAL A, B, C, or D for the 20 questions: " << endl << endl;
	
	int count = 0;
	int i;
	char answer[20];
	
	
	for (i = 0; i < 20; i++)
	{
		
		cout << "Enter answer for question # "<< i + 1 << ": ";
		cin >> answer[i];

		if (answer[i] != 'A' && answer[i] != 'B' && answer[i] != 'C' && answer[i] != 'D')
		{
			cout << "You have entered an invalid letter." << endl;
			cout << "Re-enter your answer for question # " << i + 1 << ": ";
			cin >> answer[i];
		}

		

		if ((answer[i]) == (correct_answers[i]))
		{
			cout << "Correct!" << endl;
			count++;
		}

	}
	
	
	if (count >= 15)
	{
		cout << "\n\nYou passed the exam. Congratulations!" << endl;
		cout << "You correctly answered " << count << " out of 20 questions." << endl;
		cout << "You incorrectly answered "<< (20 - count) << " out of the 20 questions.\n";
		cout << "Here is a list of the numbers of the questions you missed: " << endl; //here I have a problem
	}
	else
	{
		cout << "\n\nYou failed the exam." << endl;
		cout << "You correctly answered " << count << " out of 20 questions." << endl;
		cout << "You incorrectly answered "<< (20 - count) << " out of the 20 questions.\n";
		cout << "Here is a list of the numbers of the questions you missed: " << endl; //and here I have a problem
	}

	return 0;
}


What I don't know how to do is output a list showing the question numbers of the incorrectly answered questions. After I output: "Here is a list of the numbers of the questions you missed," how do I output the numbers? For instance, if I was incorrect for #2 and #8, the computer should say,

"You missed questions:"
2
8

I've been working very hard on this program so far. Any help you can offer would be loved. Please help me out with this dilemma...
  #2  
Old 16-Nov-2004, 05:49
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,108
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Quote:
Originally Posted by brookville
CPP / C++ / C Code:
  if (count >= 15)
  {
    cout << "\n\nYou passed the exam. Congratulations!" << endl;
    cout << "You correctly answered " << count << " out of 20 questions." << endl;
    cout << "You incorrectly answered "<< (20 - count) << " out of the 20 questions.\n";
    cout << "Here is a list of the numbers of the questions you missed: " << endl; //here I have a problem
  }
  else
  {
    cout << "\n\nYou failed the exam." << endl;
    cout << "You correctly answered " << count << " out of 20 questions." << endl;
    cout << "You incorrectly answered "<< (20 - count) << " out of the 20 questions.\n";
    cout << "Here is a list of the numbers of the questions you missed: " << endl; //and here I have a problem
  }

Hey Brookville, it looks like you have been making good progress lately. If I understand your question you want to be able to let the user know which questions were incorrect. You have already used the method of checking the user answer with the key of correct answers and have stored the user answers in the array answer[]. If you loop thru again and output the loop variable you should have what you want.

Or, you could have a new array say answer_key[] that stores a true/false condition as each question is asked you could then easily loop through it and output the users results.

Example:
CPP / C++ / C Code:
for(int i = 0; i <20; i++){
    if (answer_key[i])
        cout << "You answered question " << i +1 << "correctly." << endl;
    else
        cout << "You answered question " << i +1 << "incorrectly." << endl;
}

The if() could just as easily look like:

if (answer[i] == correct_answers[i]) and then your positive. This is code you are using already. I think you had answered your own question by time you finished this post . Sometimes 'fresh eyes' see the simple things. Or maybe I just misunderstood your question, wouldn't be the first time.

Brookville, in the for loop I have the value of i between 0 and 19 so if you did the same adding '1' to i would make the output between 1 and 20. Programmers tend to count like computers (starting at 0) but I can't recall having a test start out with:

Question 0:
What is your name? "You have 30 seconds Bob" (good old Cheech and Chong)
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
Last edited by cable_guy_67 : 16-Nov-2004 at 05:58. Reason: clarify example code
  #3  
Old 16-Nov-2004, 17:47
brookeville brookeville is offline
Junior Member
 
Join Date: Oct 2004
Posts: 56
brookeville is on a distinguished road
Hey Mark, thanks very much for helping me out! Yes, I do feel I'm making a good amount of progress with programming lately... which I'm happy for. :-) I'm pretty comfortable with using functions now, and I am currently learning arrays.

With your advice of using a new for loop and using the loop if (answer[i] != correct_answers[i]), I have solved the problem. The directions ask to output the question numbers of only the incorrect answers, so I changed it to "not equal to" and it solved the problem.

Also, I noticed another error while debugging it... In my original program, if the user entered anything other than A B C or D, the if loop would only iterate once... and the "enter a valid letter" statement only appeared once. But I changed the if loop to a while loop to iterate as many times as needed.

Finally, when the user enters a correct answer, I took out the statement that outputs "Correct!" to the screen. Actually, that was my original way of seeing if the program worked. It helped a lot with debugging it, because I could see if the value in count was accurate by counting the number of "corrects."

Here is my finished program:

CPP / C++ / C Code:
#include <iostream>
using namespace std;

char correct_answers[20] = {'B','D','A','A','C','A','B','A','C','D',
'B','C','D','A','D','C','C','B','D','A'};

int main()
{
	cout << "Enter CAPITAL A, B, C, or D for the 20 questions: " << endl << endl;
	
	int count = 0;
	int i;
	char answer[20];
	
	for (i = 0; i < 20; i++)
	{
		
		cout << "Enter answer for question # "<< i + 1 << ": ";
		cin >> answer[i];

		while (answer[i] != 'A' && answer[i] != 'B' && answer[i] != 'C' && answer[i] != 'D')
		{
			cout << "You have entered an invalid letter." << endl;
			cout << "Re-enter your answer for question # " << i + 1 << ": ";
			cin >> answer[i];
		}

		

		if ((answer[i]) == (correct_answers[i]))
		{
			count++;
		}

	}
	
	
	if (count >= 15)
	{
		cout << "\n\nYou passed the exam. Congratulations!" << endl;
		cout << "\nYou correctly answered " << count << " out of the 20 questions." << endl;
		cout << "\nYou incorrectly answered "<< (20 - count) << " out of the 20 questions.\n";
		cout << "\nHere are the numbers of the questions you missed: \n\n";
		
		for(int i = 0; i <20; i++)
		{
			if (answer[i] != correct_answers[i]) 
			cout << "You answered question #" << i + 1 << " incorrectly." << endl;
		}
	
	}

	else
	{
		cout << "\n\nYou failed the exam." << endl;
		cout << "\nYou correctly answered " << count << " out of the 20 questions." << endl;
		cout << "\nYou incorrectly answered "<< (20 - count) << " out of the 20 questions.\n";
		cout << "\nHere are the numbers of the questions you missed: \n\n";
	

		for(int i = 0; i <20; i++)
		{
			if (answer[i] != correct_answers[i]) 
			cout << "You answered question #" << i + 1 << " incorrectly." << endl;
		}
	}
	
	cout << endl; //skip one line
	return 0;
}

Allow me to express my humble thanks for your help with this problem!
  #4  
Old 16-Nov-2004, 18:18
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,108
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Quote:
Originally Posted by brookeville
Allow me to express my humble thanks for your help with this problem!

Thanks brookeville. Glad to help. Your code works well for me. The only thing I don't like is when you hit return without entering anything and it gives you a blank line in the console. It would be cool if hitting return without entering anything would give you the incorrect prompt. I couldn't crush it though. Nice work.

edit: You could move your char array inside the main loop to make them local variables instead of globals. My understanding is this is usually the preferred way to go when possible.
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
Last edited by cable_guy_67 : 16-Nov-2004 at 18:24. Reason: global to local
  #5  
Old 16-Nov-2004, 21:18
brookeville brookeville is offline
Junior Member
 
Join Date: Oct 2004
Posts: 56
brookeville is on a distinguished road
Quote:
Originally Posted by cable_guy_67
Thanks brookeville. Glad to help. Your code works well for me. The only thing I don't like is when you hit return without entering anything and it gives you a blank line in the console. It would be cool if hitting return without entering anything would give you the incorrect prompt. I couldn't crush it though. Nice work.

edit: You could move your char array inside the main loop to make them local variables instead of globals. My understanding is this is usually the preferred way to go when possible.

Cool! It's simple to move the char array inside main to make it local rather than global, so consider it done! However, I do not know how to output the error message if only the return key is pressed. The logic is to say:

while(answer[i] != 'A' && answer[i] != 'B' && answer[i] != 'C' && answer[i] != 'D' && answer[i] != return)

But I don't know to make "return" into C++ code!
  #6  
Old 16-Nov-2004, 22:23
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Try

CPP / C++ / C Code:
while(answer[i] != 'A' && answer[i] != 'B' && answer[i] != 'C' && answer[i] != 'D' && answer[i] != '\n')
  #7  
Old 17-Nov-2004, 00:27
brookeville brookeville is offline
Junior Member
 
Join Date: Oct 2004
Posts: 56
brookeville is on a distinguished road
Quote:
Originally Posted by nkhambal
Try

CPP / C++ / C Code:
while(answer[i] != 'A' && answer[i] != 'B' && answer[i] != 'C' && answer[i] != 'D' && answer[i] != '\n')

Hey nkhambal,

As much as it seems that using the newline sequence would work, it still didn't output the error message when the enter key was pressed. Any other suggestions I should try? Thanks.
  #8  
Old 17-Nov-2004, 01:07
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Sorry I didn't think it through..

How about this.?

CPP / C++ / C Code:
while((answer[i] != 'A' && answer[i] != 'B' && answer[i] != 'C' && answer[i] != 'D' ) || answer[i] == '\n')
  #9  
Old 17-Nov-2004, 01:52
brookeville brookeville is offline
Junior Member
 
Join Date: Oct 2004
Posts: 56
brookeville is on a distinguished road
Quote:
Originally Posted by nkhambal
Sorry I didn't think it through..

How about this.?

CPP / C++ / C Code:
while((answer[i] != 'A' && answer[i] != 'B' && answer[i] != 'C' && answer[i] != 'D' ) || answer[i] == '\n')

Still no effect... It still allows for enter to be pressed and skips lines... oh well! It's OK if we just leave my program as it is, but I would definitely like to eliminate this small problem before submitting it. Anything else to try?
  #10  
Old 17-Nov-2004, 01:58
Dr. Evil Dr. Evil is offline
Member
 
Join Date: Oct 2004
Location: Netherlands
Posts: 120
Dr. Evil will become famous soon enough
That's because the '\n' character isn't sent when the Return key is pressed, the key code 0x0D is sent.
 
 

Recent GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help on simple arrays brookeville CPP / C++ Forum 4 10-Nov-2004 17:34
float arrays question justachessgame C Programming Language 6 08-Nov-2004 20:37
c simple question if13121 C Programming Language 2 28-Oct-2004 07:15
another c simple question if13121 C Programming Language 1 20-Oct-2004 13:27
very simple c question if13121 C Programming Language 1 18-Oct-2004 00:12

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:46.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.