Keep getting erron messege, not sure how to fix it!

This is a discussion on Keep getting erron messege, not sure how to fix it! within the C++ Programming forums, part of the General Programming Boards category; I am new to C++ and I am writing a program that will create 100 random numbers and split them ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    Unhappy Keep getting erron messege, not sure how to fix it!

    I am new to C++ and I am writing a program that will create 100 random numbers and split them by odd and even. I think I have the code correct but I keep getting this error:

    error C2440: '=' : cannot convert from 'void' to 'int'

    Here is the program:

    Code:
    #include <iostream>
    using namespace std;
    void main()
    {
    int i, num, even, odd;  
    
    for(i = 0; i < 100; i++)
    {
    num = srand(i);
    }
    
    if ( num % 2 == 0 )
    even = num;
    else;
    odd = num;
    }
    // main

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,456
    srand is funstion which does not returns value - it should be called once to randomize your sequence of random values

    rand is function that returns a random value. it does not accept parameters...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    srand() is to seed the random number generator so it does not always generate the same sequence of numbers. Use it once before you call rand() like this:
    Code:
    #include <ctime>
    srand(time(NULL));
    rand() is the function that actually returns a random number.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    thanks!

    thanks! I will try that and see if it work. When I switch srand(i) to rand() it didn't give me the error! Thanks again!!

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    38
    You don't simply want to replace srand with rand: you want to use both.

    As MK27 stated, you call srand once to seed the random number generator, then use rand in your
    loop to get your random numbers. Calling rand without first calling srand, AFAIK, implicitly calls srand(1).

    Code:
     
    #include <iostream> 
    #include <vector> 
    #include <ctime> 
    #include <cstdlib>  
    
    int main(int argc, char* argv[]) {  
    	unsigned int i = 0; // Iterator variable  
    	int num = 0; // Random number  
    	std::vector<int> evens; // Vector for even numbers  
    	std::vector<int> odds; // Vector for odd numbers   
    
    	srand(time(NULL)); // Seed random number generator   
    
    	// Fill vectors with random numbers  
    	for(i = 0; i < 100; i++) {   
    		num = rand();   
    		if ((num % 2) == 0)    
    			evens.push_back(num);   
    		else    
    			odds.push_back(num);  
    	}   
    
    	// Display even numbers  
    	std::cout << "Displaying even numbers:" << std::endl;  
    	for (i = 0; i < evens.size(); i++) {   
    		if ((i % 5) == 0)    
    			std::cout << std::endl;   
    		std::cout << evens[i] << " ";  
    	}   
    
    	std::cout << std::endl << std::endl;    
    	
    	// Display odd numbers  
    	std::cout << "Displaying odd numbers:" << std::endl;  
    	for (i = 0; i < odds.size(); i++) {   
    		if ((i % 5) == 0)    
    			std::cout << std::endl;   
    		std::cout << odds[i] << " ";  
    	}   
    
    	return 0; 
    }
    Last edited by nicoqwertyu; 03-20-2010 at 06:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 12-06-2009, 05:09 AM
  2. fix lines visual c++ 2008
    By the_contractor in forum C Programming
    Replies: 4
    Last Post: 11-05-2009, 01:32 AM
  3. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  4. C++ code need help to fix
    By McReal in forum C++ Programming
    Replies: 9
    Last Post: 05-12-2007, 02:48 PM
  5. Help me fix my mess!!!!
    By Starr in forum C++ Programming
    Replies: 35
    Last Post: 02-01-2006, 02:40 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21