Unable to get rid of comma when trying to create array

So I am trying to get rid of the last comma, basically thinking once you get the last time through the loop, don't add the comma, I am aslo trying to put in random order and have no repeats, but first trying to get the last comma out of there, what I have below basically puts to many records in the list. I may be doing this ALL wrong. Trying to create comma separated value, that I can then use each value one at a time in case to give user random questions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int main()
{

 int array[5];

 srand((unsigned)time(0))
    for(int i=0; i<5; i++)
        {
        array[i] = (rand()%5)+1;
    if (i < 5);
        {
          cout << array[i] << "," << endl;
        }
    if (i == 5);
        {
          cout << array[i] << endl;
        }
       }
}
closed account (E0p9LyTq)
The error is on srand((unsigned)time(0)). No ; terminator!

Remove the ; terminator on both if statements. Your braced statements get executed no matter what the results of the if statements are.

Your if statement logic is a bit skewed, you are exceeding the boundaries of the array. array[5] is the 6th! element.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{

   int array[5];

   srand((unsigned)time(0));
   for(int i=0; i<5; i++)
   {
      array[i] = (rand()%5)+1;
      if (i < 4)
      {
         cout << array[i] << "," << endl;
      }
      if (i == 4)
      {
         cout << array[i] << endl;
      }
   }
}


I would use if/else

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int main()
{
    //declare array
    int array[5];
    
    //loop through values
    for (int j = 0; j < 5; j++)
    {
        //fill array
        array[i] = rand() % 5 + 1;

        //display array
        if (j != 4)
            cout << array[j] << ", ";
        else
            cout << array[j] << endl;
        //end if
    } //end for
} //end of main function 
Last edited on
Right, i forgot arrays starts at "0", and that is why it is a 4 and not a 5. very cool, so now I looking at this and do I even need commas? If the array is built 12345 instead of 1, 2, 3, 4, 5 [when I call the array value X = array[2], it will know, right? So I have that working now, so now I need to figure out how to get rid of duplicate numbers, I updated my code to make sure its working when I call a number and it is, but is there a way to get rid of the duplicates in the array. I am trying to use Random_Shuffle for the array,but I don't get an error and its not random.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>

using namespace std;


int main()
{

    //int arrays[5];
    int options[5]= {1,2,3,4,5};
    int array1;
    int array2;


    random_shuffle(&options[0], &options[4]);
    for (unsigned i = 0; i < 5; i++)
    {
        cout << options[i] << endl;
    }

  // srand((unsigned)time(0));
   //for(int i=0; i<5; i++)
   //{
    //  arrays[i] = (rand()%5)+1;
      //cout << arrays[i];
   //}

  //array1 = arrays[3];

  array2 = options[0];
  //cout <<"\n" << array1;
  cout << "\n" << array2;
   return 0;
}
Ahh, so I had to changed the options end to 5 in random_shuffle to get it to work.
Last edited on
This is really cool, I finally got it working, so it takes an array of numbers, randomizes the numbers and then I I call on based on location in array and it displays.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>

using namespace std;


int main()
{

    //int arrays[5];
    int options[5]= {1,2,3,4,5};
    int array2;

    srand((unsigned)time(0));
    random_shuffle(&options[0], &options[5]);
    for (unsigned i = 0; i < 5; i++)
    {
        cout << options[i] << endl;
    }

  array2 = options[0];
  cout << "\n" << array2;
   return 0;
}
One more thing: Several things:

Avoid having magic numbers like 5 in your code, make them a const variable instead, and use that variable name throughout the code.

Also try to avoid c style casts, prefer the c++ static_cast. It's unfortunate (IMO) that some of the the reference on this site has some c style casts (if that is where you saw an example). cppreference has definitive reference material, although it might be (necessarily) harder to read.

It's good that you have the cast though, std::time returns a std::time_t type, and on old implementations that might be implemented as a signed integer, and std::srand expects an unsigned value anyway. With that, it is always good to read the documentation for each function that you use, and make sure you use the types that are specified there, along with any thing else in order to use the function correctly.

Here is your code with a couple of changes, I haven't tested it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>

using namespace std;


int main()
{
    const unsigned int ArraySize = 5;

   
    int options[ArraySize]= {1,2,3,4,5};
    int array2;  // is there a better name for this variable?

    std::srand( static_cast<unsigned int> std::time(0) );
    std::random_shuffle(&options[0], &options[ArraySize]);
    for (unsigned i = 0; i < ArraySize; i++)
    {
        std::cout << options[i] << endl;
    }

  array2 = options[0];
  std::cout << "\n" << array2;
   return 0;
}
Thanks the TheIdeasMan, i appreciate the knowledge you are sharing, this site has been amazing so far and I also am reading the C++ Primer book (Fifth Edition), The internet is a mix of everything :0). Is there a reason not to use namespace std;? The array2 was just of an example, the code I am using for my trivia game its called "guess", so the ArraySize it a great, one less place to change if I make updates to the ArraySize.

Thanks again for your feedback, its sure fun to learn this stuff, because I feel like its such a large amount of information we are only limited by our creativity.
Is there a reason not to use namespace std;?


Have a go with Google, there is plenty written why this is not preferred. While you are at it, have a read about namespaces in general :+)

Another good one is const - use it as much as you can, have a read up about that too :+)

..... its sure fun to learn this stuff, .....


Excellent, if it is fun - then that bodes well for your learning :+D


Some other resources, although I gather that your book is one of the good ones:

http://www.codergears.com/QACenter/index.php?qa=resources


http://en.cppreference.com/w/


http://stackoverflow.com/
Last edited on
Topic archived. No new replies allowed.