Error Message

Hi Guys. I'm doing an assignment for uni, and I'm getting a message I don't understand. I've researched other examples, and I don't see a difference, perhaps someone can shed some light. The assignment involves a random number generator and bubble sort.

- The error message is,"Warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data"

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//Random Number Generator
//With Bubble Sort and Selection Sort

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

void bubblesort(); //Bubble Sort Function
int random;
int randqty [1000]; //Array Storing Random Numbers

int main()
{	
	srand(time(0)); //Error message points to this line. Ive tried using 'NULL' too.
	int range;
	int option;	

	cout << "How many random numbers would you like sorted?";
	cin >> random;	
	cout << "Within which range would you like these random numbers?";
	cin >> range;	
	for (int i = 1; i <random +1; i++)
	{
		randqty [i]= 1+(rand() % range);
		cout << randqty[i] <<endl;
	}
	
	cout << "How would you like the random numbers sorted?\n";
	cout << "Option 1: Bubble Sort\n";
	cout << "Option 2: Selection Sort\n";
	cin >> option;

		switch (option)
		{
			case 1:
				{
					system ("cls");
					cout << "Bubble Sort\n";
					bubblesort();
					break;
				}			
			case 2:
				{
					system ("cls");
					cout << "Selection Sort\n";					
					break;
				}
			default:
				{
					system ("cls");
					cout << "Please enter another choice\n";
					break;
				}
		}

	return 0;
}

void bubblesort(int randqty[], int random)
{	

	int temp;
	bool swap;
	

	do
	{
		swap = false;
		for (int x=0; x < random-1; x++)
		{
			if (randqty[x] > randqty[x+1])
			{
				temp = randqty[x];
				randqty[x]=randqty[x+1];
				randqty[x+1]=temp;
				swap=true;
			}
		}
	}while (swap);

}


Thank-you in advance.
That isn't an error, it's just a warning. You should be able to ignore it.

Function time() returns a value of type time_t
Function srand() takes a parameter of type unsigned int
The compiler automatically converts from one to the other, and is simply letting you know that there may be some loss of precision.

If considered necessary, you could suppress the message by using an explicit cast, using either srand((unsigned int) time(0));
or srand(static_cast<unsigned int>(time(0)));
Last edited on
Thank - you Chervil, spot on.

If I could ask about one more error message...
"error LNK2019: unresolved external symbol "void __cdecl bubblesort(void)" (?bubblesort@@YAXXZ) referenced in function _main"

I have no idea about this? A Google search says I may be missing a library, I just have no idea which one?
You say bubblesort does not take any argument
You use bubblesort without passing any parameters
You define a bubblesort that must take an array and its size as parameters.
┬┐see the problem?

You should learn about scope


By the way, `random' is not a good name for the size of an array.
Last edited on
In your code above, you have defined function bubblesort at line 60 like this:
void bubblesort(int randqty[], int random)
That is, it requires two input parameters, the first is an array of integers, the second is an integer.

However, the declaration on line 9
void bubblesort();
is a function which takes no parameters at all.

As far as the compiler is concerned, these are two different functions.
On line 40, the function is called, with no parameters. The compiler thinks this is ok, as it matches the declaration on line 9. But the linker tries to match the function call to the code or the function, but cannot find it.

Hence it is the linker which outputs the error message.

In this case, it looks like the correct version is the one on line 60.
You need to change both the prototype declaration (line 9) and the call on line 40, so they each supply the required parameters, so as to agree with line 60.

Here's a recent thread where the role of the prototype is discussed in a bit more detail: http://www.cplusplus.com/forum/beginner/88749/#msg476413
Thank-you once again, all input is greatly appreciated.
Topic archived. No new replies allowed.