# Comparing Arrays question

Show 80 post(s) from this thread on one page
Page 2 of 3 First 123 Last
• 04-13-2009
taugust7
Code:

```okay i feel like such a newb why the -1 in the for loops? for (count = 0; count<=size-1; count++)         {                 cin>> your_answers[count];                     } and also he told us to use 21 because we have to include the null terminator or some bull........```
• 04-13-2009
Flaug
Here is some updated code:

Code:

```#include <iostream> using namespace std; int main() {              int count, correct = 0;                  const int size = 20;         char your_answers[size];         char answers[size] = {'b','d','a','a','c','a','b','a','c','d','b','c','d','a','d','c','c','b','d','a'};         cout<< " Please enter the students answers. \n" ;         for (count = 0; count<=size-1; count++)         {                 cin >> your_answers[count];                 while(true)                 {                         if (your_answers[count] == 'a' || your_answers[count] == 'b' || your_answers[count] == 'c' || your_answers[count] == 'd')                                 break; //Break out of while loop.                         else                         {                                 cout << "(" << your_answers[count] << ") Your answer is not an appropriate one. Please try again." << endl;                                 cin >> your_answers[count];                         }                 }         }         for (count=0; count<=size-1;count++)         {                 if (answers[count] == your_answers[count])                                   correct += 1; //If they got this one right, then add one to 'correct'.         }         if(correct >= 15) //They got 15 right                 cout << "\nYou passed the test. Congratulations." << endl;         else                 cout << "\nYou're a failure." << endl;         cout << "You got " << correct << " out of 20 correct." << endl;         cout << endl; //Make a new line.         for (count = 0; count<=size-1; count++)         {                 cout << "Correct Answer: " << answers[count] << " | Your answer: " << your_answers[count];                 if (answers[count] == your_answers[count]) //It's correct;                         cout << " [CORRECT]." << endl;                 else                         cout << " [WRONG]." << endl;         }         system("PAUSE"); return 0; }```
• 04-13-2009
Flaug
Quote:

Originally Posted by taugust7
Code:

```okay i feel like such a newb why the -1 in the for loops? for (count = 0; count<=size-1; count++)         {                 cin>> your_answers[count];                     } and also he told us to use 21 because we have to include the null terminator or some bull........```

It's -1 because arrays start at 0. So from 0 - 19 is 20. So 20 - 1 is 19.
• 04-13-2009
taugust7
Code:

```#include <iostream> #include <string> using namespace std; int main() {                int count, correct = 0, incorrect = 0;                  const int size = 20;         char your_answers[size];         char answers[size] = {'b','d','a','a','c','a','b','a','c','d','b','c','d','a','d','c','c','b','d','a'};         cout<< " Please enter the students answers. \n" ;         for (count = 0; count<=size-1; count++)         {                 cin>> your_answers[count];                     }         for (count=0; count<=size-1;count++)         {                 if (answers[count] == your_answers[count])                                   correct += 1;         }         for (count=0; count<=size-1;count++)         {         if (answers[count] != your_answers[count])                         incorrect += 1;     }         if(correct >= 15)                 cout << "You passed the test. Congratulations. " << correct << " out of 20 correct. " << endl;         else                 cout << "You're a failure." << endl; cout<< "You got "<< incorrect<< " answers wrong \n";         system ("pause"); return 0; } okay so this is what i got so far, i need to find a way to show which answers i got wrong, i.e. if number[1] is wrong display number[1] of array and i still cant figure out that damned validation```
• 04-13-2009
taugust7
this is good, but i cant use (true)
im limited in the crap i can use so if there is another simpler way of validation i would appreciate if you could help me
• 04-13-2009
Sebastiani
Flaug, you should give pointers, not complete homework assignments.
• 04-13-2009
taugust7
its not a homework assignment,
my teacher expects us to learn loops in a week and didnt cover do's and fors,
im goin over a few assignments and tryin to get them but i need alot of help
he told us to learn do's for's while's, so im learning whats in those chapters
i also appreciate all the help man thank you so much
• 04-13-2009
Sebastiani
Hogwash. It's homework.
• 04-13-2009
taugust7
Code:

```well either way sebastianinininnininininininininininininiininininiininiiinininin do you know what i should look into if i want to validate my answers without using (true) all i can think of is if (your_answers != 'a' ||' b' ||'c'||'d') {  cout<< "incorrect answer, please enter a,b,c, or d. \n" cin>> your_answers[count] }```
• 04-13-2009
Sebastiani
>> do you know what i should look into if i want to validate my answers without using (true)

Well, considering that the code you posted is both logically broken and won't even compile, I'd suggest a decent book.
• 04-13-2009
taugust7
Code:

```#include <iostream> #include <string> using namespace std; int main() {                int count, correct = 0, incorrect = 0;                  const int size = 20;         char your_answers[size];         char answers[size] = {'b','d','a','a','c','a','b','a','c','d','b','c','d','a','d','c','c','b','d','a'};         cout<< " Please enter the students answers. \n" ;         for (count = 0; count<=size-1; count++)         {                 cin>> your_answers[count];                    } for (count=0; count<=size-1;count++)         {                 if (answers[count] == your_answers[count])                                   correct += 1;         } for (count=0; count<size;count++)     {               if (answers[count] != your_answers[count])             incorrect += 1;     }     if(correct >= 15)                 cout << "You passed the test. Congratulations." << endl;         else                 cout << "You're a failure." << endl;         cout << "You got " << correct << " out of 20 correct." << endl;         cout<< " You got " << incorrect<< " out of 20 wrong. "<<endl;         cout << endl;         for (count = 0; count<=size-1; count++)         {                 cout << "Correct Answer: " << answers[count] << " | Your answer: " << your_answers[count];                 if (answers[count] == your_answers[count])                         cout << " [CORRECT]." << endl;                 else                         cout << " [WRONG]." << endl;         }                 system ("pause"); return 0; } and my output is  Please enter the students answers. bdaacabacdbcdadccbdd You passed the test. Congratulations. You got 19 out of 20 correct.  You got 1 out of 20 wrong. Correct Answer: b | Your answer: b [CORRECT]. Correct Answer: d | Your answer: d [CORRECT]. Correct Answer: a | Your answer: a [CORRECT]. Correct Answer: a | Your answer: a [CORRECT]. Correct Answer: c | Your answer: c [CORRECT]. Correct Answer: a | Your answer: a [CORRECT]. Correct Answer: b | Your answer: b [CORRECT]. Correct Answer: a | Your answer: a [CORRECT]. Correct Answer: c | Your answer: c [CORRECT]. Correct Answer: d | Your answer: d [CORRECT]. Correct Answer: b | Your answer: b [CORRECT]. Correct Answer: c | Your answer: c [CORRECT]. Correct Answer: d | Your answer: d [CORRECT]. Correct Answer: a | Your answer: a [CORRECT]. Correct Answer: d | Your answer: d [CORRECT]. Correct Answer: c | Your answer: c [CORRECT]. Correct Answer: c | Your answer: c [CORRECT]. Correct Answer: b | Your answer: b [CORRECT]. Correct Answer: d | Your answer: d [CORRECT]. Correct Answer: a | Your answer: d [WRONG]. Press any key to continue . . . now i still need good output validation an i dont know how to do it```
• 04-13-2009
taugust7
Code:

``` while (your_answers != 'a', 'b', 'c', 'd')         {               cout<< "Your answer of "<< your_answers[count]<<" is invalid, please select A,B,C or D. \n";               cin>> your_answers[count];               } i need something along these lines but i cant figure it out```
• 04-13-2009
Sebastiani
I don't see a shred of code that shows you *trying* to validate the input. Perhaps a combination of tolower() and isalpha() might be useful?

Also, this sort of thing won't work:

>> if (X != 'a' ||'b')

Since it's identical to:

if (X != 'a' ||'b' != 0)

You'll need to compare each value with X (but using && instead of ||, obviously).
• 04-13-2009
taugust7
thank you sir ill see what i come up with
• 04-13-2009
taugust7
also i dont know what the crap isalpha and tolower are, this is an intro level class and im tryin to teach myself arrays and loops ya know
Show 80 post(s) from this thread on one page
Page 2 of 3 First 123 Last