How to store user inputs in an array and check if the two array has the same element

887 views Asked by At

I'm trying to make a quiz checker where the user will input their answer and it will automatically check in the array. Any tips?

Here is my code:

int main()
{
     string aswer[] = { "D", "C", "D", "D", "A"); // 
     char input[5];
     int counter ;
     int points = 0;
     cout << "Welcome and Good luck!\n";

            for (counter = 0; counter < 5; counter++) {
                cout << counter << " Choose from letters A-D: \n";
                cin >> input;
            }
           for (int i = 0; i < 5; ++i) {
                cout << "\n you enter " << input[i] << endl;
            }
            foreach (char item in answer){
                foreach (char item1 in inputs){
                    If (item == item1){
                    points = points +1  
                }
            }

           }
    return 0;
      }
1

There are 1 answers

0
David C. Rankin On

Tips -- be consistent choosing storage. If you are using a std::string for the answer (not the erroneous array of std::string shown), then use a std::string for input as well. But if you must use a POA (plain old array), that's fine for educational purposes -- you just lack all auto-memory management and bounds protections.

You initialize a std::basic_string as:

    std::string answer { "DCDDA" };    /* initialize string (not array of strings) */

(note: the use of std:: namespace identifier. See Why is “using namespace std;” considered bad practice?)

Don't use Magic Numbers in your code. The 5 you stick in your array declaration and in your loop limit is a Magic Number. Instead,

#define NCHAR 5     /* if you need a constant, #define one (or more) */
...
    char input[NCHAR];                 /* array of 5 char (why not string?) */
    ...
    for (int i = 0; i < NCHAR; i++) {   /* loop taking input */

When you loop taking input, validate every input. The user may well generate a manual EOF to cancel input with Ctrl+d (or Ctrl+z on windows). Only increment the count of characters stored after you have validated the input, e.g.

    int counter = 0, points = 0;       /* counter and points, initialized zero */
    
    std::cout << "Welcome and Good luck!\n\n";

    for (int i = 0; i < NCHAR; i++) {   /* loop taking input */
        std::cout << "  " << i << ".  Choose from letters A-D: ";
        if (std::cin >> input[i])       /* validate input */
            counter++;                  /* only increment on successful input */
    }

For your output, understand the difference between using '\n' and std::endl, C++: “std::endl” vs “\n”.

When you have items stored in a container (such as std::string), you can use a Range-based for loop (since C++11) to loop over each item in the container. To loop over your stored input, you must limit the loop to the number of elements successfully read as input. Use the same counter as your comparison limit, e.g.

    for (auto& item : answer)           /* range based for-loop over chars in string */
        for (int i = 0; i < counter; i++)   /* loop over counter chars in array */
            if (item == input[i])       /* if the item matches the input char */
                points += 1;            /* add point */

Putting it altogether, you would have:

#include <iostream>
#include <string>

#define NCHAR 5     /* if you need a constant, #define one (or more) */

int main()
{
    std::string answer { "DCDDA" };    /* initialize string (not array of strings) */
    char input[NCHAR];                 /* array of 5 char (why not string?) */
    int counter = 0, points = 0;       /* counter and points, initialized zero */
    
    std::cout << "Welcome and Good luck!\n\n";

    for (int i = 0; i < NCHAR; i++) {   /* loop taking input */
        std::cout << "  " << i << ".  Choose from letters A-D: ";
        if (std::cin >> input[i])       /* validate input */
            counter++;                  /* only increment on successful input */
    }
    
    std::cout << "\nYou entered:\n";    /* output stored values */
    for (int i = 0; i < counter; i++)   /* looping only 0 to counter */
        std::cout << "  input[" << i << "] : " << input[i] << '\n';
    
    for (auto& item : answer)           /* range based for-loop over chars in string */
        for (int i = 0; i < counter; i++)   /* loop over counter chars in array */
            if (item == input[i])       /* if the item matches the input char */
                points += 1;            /* add point */
    
    std::cout << "\nTotal points: " << points << '\n';  /* output total points */
}

Example Use/Output

$ ./bin/points
Welcome and Good luck!

  0.  Choose from letters A-D: A
  1.  Choose from letters A-D: B
  2.  Choose from letters A-D: C
  3.  Choose from letters A-D: D
  4.  Choose from letters A-D: E

You entered:
  input[0] : A
  input[1] : B
  input[2] : C
  input[3] : D
  input[4] : E

Total points: 5

Checking maximum points if all characters match:

$ ./bin/points
Welcome and Good luck!

  0.  Choose from letters A-D: D
  1.  Choose from letters A-D: C
  2.  Choose from letters A-D: D
  3.  Choose from letters A-D: D
  4.  Choose from letters A-D: A

You entered:
  input[0] : D
  input[1] : C
  input[2] : D
  input[3] : D
  input[4] : A

Total points: 11

Look things over and let me know if you have questions.