-1

The program is supposed to have the user enter 5 names and output any duplicate names. How do I make my program output 'duplicate name' for values inside an array? I can't seem to understand how to write it so it will check for a repeated element when the user inputs the names. Thanks.

#include <iostream>

using namespace std;

int main() {

    string names[5];        //array with 5 names
    string input;

   for (string input : names){
        cout << "Please enter a name: ";   
        cin >> input;
    }

    for (int check = 1; check-1; check++){

        if (input == names[check]){
            cout << input << " is a duplicate name!" << endl;
        }
    }
}
QM Adams
  • 73
  • 7
  • `for (string input : names){` => `for (string& input : names){` Also the looping parameter and your `input` variable should have different names. – πάντα ῥεῖ Oct 04 '18 at 21:55
  • And `check-1` should be `check < 5`. – David G Oct 04 '18 at 21:56
  • `for (int check = 1; check-1; check++)` is an unusual for loop. Typically the second statement, the one between the `;`s is a condition. This will exit if `check-1` is zero which will instantly happen because `check` starts at 1. – user4581301 Oct 04 '18 at 22:02
  • What do you mean by looping parameter and input should have different names? @ πάντα ῥεῖ – QM Adams Oct 04 '18 at 22:03
  • Well maybe my whole second loop is wrong, but how to do go about writing it to make it output the duplicates? – QM Adams Oct 04 '18 at 22:04

1 Answers1

2

A few issues to rectify include:

  • for (string input : names) creates a temporary variable and fails to modify the original array; use for (string &input : names) to create a reference to each array element in the loop block.
  • Your for loops aren't nested, so there's no grounds for comparison between some element of the array (or an input string) against every other element. Either nest your second loop inside your input loop to perform the checks as you add names, or separate the check into its own block. Note that there are more efficient ways of solving this, such as keeping an unordered_set of names you've seen; nested loops grow in exponential time complexity.
  • Your second for loop's termination condition isn't testing anything helpful as it stands (it evaluates to 0 which is false, terminating the loop immediately). Iterating until the counter meets or exceeds the length of the array, 5, is more appropriate.
#include <iostream>

int main() {
    std::string names[5];

    for (std::string &input : names) {
        std::cout << "Please enter a name: ";   
        std::cin >> input;
    }

    for (int i = 0; i < 5; i++) {
        for (int j = i + 1; j < 5; j++) {
            if (names[i] == names[j]){
                std::cout << names[i] << " is a duplicate name!" << std::endl;
            }
        }
    }
}

Output:

Please enter a name:  a
Please enter a name:  b
Please enter a name:  c
Please enter a name:  d
Please enter a name:  a
a is a duplicate name!

Try it

ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • 1
    No problem--if you're not comfortable with `&` references yet, just use a traditional `for` loop until you are. – ggorlen Oct 04 '18 at 22:18
  • 1
    Why are my questions getting bad ratings? I can't find topics that relate to my instance that were discussed before; any ideas before I am not allowed to ask questions anymore? – QM Adams Oct 04 '18 at 22:31
  • 1
    @QMAdams Stack Overflow can be rough when you're trying to learn the language fundamentals. You might learn more and faster with some structure like a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or class. – Blastfurnace Oct 04 '18 at 22:46
  • 1
    @QMAdams SO has a pretty steep learning curve (like programming in general). Spend time looking at other questions and [meta](https://meta.stackoverflow.com/) and keep working on coding basics and you'll start getting upvotes. Check out [how to ask](https://stackoverflow.com/help/how-to-ask). It can be awful to get stuck for 4+ hours (or more) on a small and innocuous program, but that's the best way to learn. – ggorlen Oct 04 '18 at 22:47