Thread: Program prints correct result, but prints it over and over

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    225

    Program prints correct result, but prints it over and over

    I have a program where the user inputs a line of integers, and then all unique ones are outputted. It works fine-almost. It prints the numbers correctly, but prints them more than once and I'm not sure why.
    Code:
    #include <iostream>
    using namespace std;
    int main ( ) {
        int array[100];
        int count = 0;
        cout << "Enter integer values into the array, or -1 to stop: ";
      while(true) {
                  cin >> array[count];
                  if(array[count] == -1) {
                                  break;
                                  }
                                  else {
                                       count++;
                                       }
                                       }
                                       cout << endl;
                                       cout << "Distinct values in the array:";
                                       cout << endl;
                                       cout << "(";
                                       if(count > 0) {
                                                cout << array[0];
                                         //       cout << ")";
                                                for(int i = 1; i < count; i++) {
                                                        bool seen = false;
                                                        for(int var = 0; var < array[i]; var++) {
                                                        if(var == i) {
                                                               seen = true;
                                                               }
                                                               }
                                                               if(seen != true) {
                                                                       cout << "," << array[i];
                                                                       }
                                                                       }
                                                                       }
                                                                       cout << ")";        
                                                }
    Please help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,059
    4 months, and your code is still dog-food.
    Indent style - Wikipedia, the free encyclopedia

    You're running out of people who give a damn to even look at your posts.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Okay, now:
    Code:
    #include <iostream>
    using namespace std;
    int main ( ) {
        int array[100];
        int count = 0;
        cout << "Enter integer values into the array, or -1 to stop: ";
        while(true) {
        cin >> array[count];
        if(array[count] == -1) {
        break;
        }
        else {
        count++;
        }
        }
        cout << endl;
        cout << "Distinct values in the array:";
        cout << endl;
        cout << "(";
        if(count > 0) {
        cout << array[0];
        //       cout << ")";
        for(int i = 1; i < count; i++) {
        bool seen = false;
        for(int var = 0; var < array[i]; var++) {
        if(var == i) {
        seen = true;
        }
        }
        if(seen != true) {
        cout << "," << array[i];
        }
        }
        }
        cout << ")";        
        }

  4. #4
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Any help? I really need help with this.
    edit: nevermind

  5. #5
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Nevermind, turns out I didn't have it.
    What do I need to change to fix this?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,539
    Start by properly formatting your code!:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ( )
    {
       int array[100];
       int count = 0;
       cout << "Enter integer values into the array, or -1 to stop: ";
       while(true)
       {
          cin >> array[count];
          if(array[count] == -1)
          {
             break;
          }
          else
          {
             count++;
          }
       }
       cout << endl;
       cout << "Distinct values in the array:";
       cout << endl;
       cout << "(";
       if(count > 0)
       {
          cout << array[0];
          //       cout << ")";
          for(int i = 1; i < count; i++)
          {
             bool seen = false;
             for(int var = 0; var < array[i]; var++)
             {
                if(var == i)
                {
                   seen = true;
                }
             }
             if(seen != true)
             {
                cout << "," << array[i];
             }
          }
       }
       cout << ")";
    }

    Next show an example of what you inputted into the program along with the output your program produced. Lastly show exactly what output you want your program to produce for the given input.

    If you can't properly indent your code I really suggest you talk to your career adviser and look into transferring into another profession.


    Jim

  7. #7
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Okay, here is what it should print:
    Code:
    Enter integer values into the array, or -1 to stop:
    5
    5
    5
    5
    3
    3
    4
    4
    4
    -1
    
    Distinct values in the array:
    (5,3,4)
    and what it does:
    Code:
    Enter integer values into the array, or -1 to stop: 5
    5
    5
    5
    3
    3
    4
    4
    4
    -1
    
    
    Distinct values in the array:
    (5,3,3,4,4,4)

  8. #8
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Hmm... Here's what I would do :

    * use boolean array to keep track of which numbers were read in
    * when reading in a number not previously inserted (bool[i] == false), use linked list for sorted insertions
    * iterate list and print out numbers

  9. #9
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Is there a simpler way? This is just a small problem, after all. There must be a couple specific things that are wrong.

  10. #10
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    This is the offending piece of code:

    Code:
                for(int var = 0; var < array[i]; var++) // For 0 through value at array[i]:
                {
                    if(var == i) // If inner iterator equals outer iterator,
                    {
                        seen = true; // seen is true.
                    }
                }
                if(seen != true) // If inner iterator was not equal to outer iterator,
                {
                    cout << "," << array[i]; // print value at outer iterator index.
                }
    I think you meant the inner loop to go through 0 through the outer iterator, and not the value at the index at the outer iterator.

    Also, I think you meant to test the value at the index of the outer iterator against the value at the index of the inner iterator here:

    Code:
                    if(var == i) // If inner iterator equals outer iterator,
                    {
                        seen = true; // seen is true.
                    }
    
                    // array[var] == array[i] makes more sense
    Edit:

    To really see what's wrong, try a test case by entering [1,2,3,4,5,6,7,ect] as the input, and go through with a debugger watching the values.

    You are trying to test values at array indexes, but are actually just testing the iterators themselves.
    Last edited by Alpo; 02-10-2015 at 11:36 AM.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  11. #11
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Okay, I'm starting to see it: one of my for loop conditions is wrong, and that if var == i is wrong. How would I fix them? Tried running the debugger, didn't help much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-28-2015, 10:53 AM
  2. Replies: 2
    Last Post: 09-06-2011, 01:08 AM
  3. Confusing pointers, Prints wierd result
    By sanddune008 in forum C Programming
    Replies: 4
    Last Post: 06-29-2010, 03:35 AM
  4. Why the program prints line twice?
    By red463 in forum C Programming
    Replies: 15
    Last Post: 04-30-2010, 05:20 AM
  5. program that prints itself
    By modec in forum C Programming
    Replies: 1
    Last Post: 10-10-2003, 02:14 PM