![]() |
|
#1
|
|||
|
|||
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:
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
|
||||
|
||||
|
Quote:
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:
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 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" __________________
"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
|
|||
|
|||
|
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:
Allow me to express my humble thanks for your help with this problem! |
|
#4
|
||||
|
||||
|
Quote:
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
|
|||
|
|||
|
Quote:
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
|
|||
|
|||
|
Try
CPP / C++ / C Code:
|
|
#7
|
|||
|
|||
|
Quote:
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
|
|||
|
|||
|
Sorry I didn't think it through..
How about this.? CPP / C++ / C Code:
|
|
#9
|
|||
|
|||
|
Quote:
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
|
|||
|
|||
|
That's because the '\n' character isn't sent when the Return key is pressed, the key code 0x0D is sent.
|
Recent GIDBlog
Last Week of IA Training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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