| | |
Trouble Converting Variables
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
I'm using a code segment out of a book that I'm learning from. For some reason it isn't working and I haven't been able to figure out how it works.
When I use the srand function it says I can't convert my (short) Number variable to a (void) srand (time(NULL)) there is an error.
Errors:
- Error C2440: '=' : cannot convert from 'void' to 'short'
Expressions of type void cannot be converted to other types
-Warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
When I use the srand function it says I can't convert my (short) Number variable to a (void) srand (time(NULL)) there is an error.
Errors:
- Error C2440: '=' : cannot convert from 'void' to 'short'
Expressions of type void cannot be converted to other types
-Warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
main ()
{
short Number, Guess = 0;
Number = srand (time(NULL));
Number++;
while (Guess != Number)
{
cout <<"Enter a number between 1 and 100: ";
cin>> Guess;
if (Guess < Number)
{
cout <<"You are guessing to low." <<endl;
}
if (Guess > Number)
{
cout <<"You are guessing to high." <<endl;
}
}
cout <<"You got it bud, the winning number is: " <<Number <<endl;
return 0;
} :evil::cry::evil::cry::evil::cry::evil::cry::evil:
srand() only establishes the seed for the rand() function.
Try
Try
C++ Syntax (Toggle Plain Text)
srand (time (0)); Number = rand ();
![]() |
Similar Threads
- Custom pascal triangle ? (C)
- A good idea when to use pointers vs "reference variables"? (C++)
- converting to uppercase (C++)
- Question RE: Converting Fractional Decimals to Binary (Python)
- Converting Any Base to Any Base (C)
- converting variables. (C#)
- Trouble converting internal DVD drive to external (USB Devices and other Peripherals)
- Help with Class, stuck. (C++)
Other Threads in the C++ Forum
- Previous Thread: If statement, how to write this one?
- Next Thread: string comparisions
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





