Function looping error

Did i miss something why i can't loop a function? Can someone explain what did i do wrong? Am i missing header file why my function is not working?


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

using namespace std;

void FuncTest(int x)
{
	cout << "random number " << x << endl;
}


int main()
{
	srand(time(NULL));

	for (int i = 0; i < 25; i++)
	{
		cout << FuncTest(1+(rand() % 25)) << endl;
	}

}



I'm getting this error
1
2
Warning	1	warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data	c:\users\lastname\documents\visual studio 2013\projects\guessin_game\guessin_game\source.cpp	14	1	Guessin_Game


Error 2 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion) c:\users\lastname\documents\visual studio 2013\projects\guessin_game\guessin_game\source.cpp 18 1 Guessin_Game

1
2
	3	IntelliSense: no operator "<<" matches these operands
            operand types are: std::ostream << void	c:\Users\Lastname\Documents\Visual Studio 2013\Projects\Guessin_Game\Guessin_Game\Source.cpp	18	8	Guessin_Game


I also tried changing void FuncTest to int FuncTest and i get this error
1
2
Error	2	error C4716: 'FuncTest' : must return a value	c:\users\lastname\documents\visual studio 2013\projects\guessin_game\guessin_game\source.cpp	9	1	Guessin_Game

Your function is already call cout!
You dont have to cout in loop again.
Also you have to #include <cstdlib> for using srand.
Replace line 18:
 
     cout << FuncTest(1+(rand() % 25)) << endl;

with:
 
    FuncTest(1+(rand() % 25));


Function FuncTest() was declared with a type of void, that is it does not return a value. Hence at line 18, the cout << is being asked to output something which isn't there.

As for the header file and the first warning, for completeness you should have #include <cstdlib> for the random functions, and you might put this at line 14 to avoid the warning C4244, srand( (unsigned int) time(NULL));
Topic archived. No new replies allowed.