-2

This beginner's program I am trying to make is basically a program where a truly random number is generated and the "player" has to guess the number. The computer will respond "too high" or "too low" until the player guesses the right number.

I got this program idea from some website, and thought I should roll with it. This is my second program- my first being a simple I/O program.

Here is my code:

// BracketingSearch.cpp

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
#include <istream>
#include <fstream>
using namespace std;

int main() 
{

int x = srand(time(0));
cout << x << endl;
for (int x = 1; x <= 10; x++) {
    cout << 1 + (rand() % 100) << endl;
}
string stall;
getline(cin, stall);
}

Side notes: I don't think I need that many headers; I am still getting use to the c++ libraries. Please critique my code as much as possible. I am eager to learn programming! The string stall is just to make my console application pause for input, so I can see the results.

Thank you for everyone who can help! -Mike

Matthew
  • 21
  • 4
  • 1
    You seem to have 2 different questions: "Why is srand not being stored in my variable" and "Please critique my code as much as possible". Are you looking for a [code review](http://codereview.stackexchange.com)? Please clarify. – Rakete1111 Aug 15 '16 at 03:59
  • 1
    The return type of `srand` is `void`. I am surprised your compiler didn't complain about `int x = srand(time(0));`. – R Sahu Aug 15 '16 at 04:02
  • 1
    The function `srand()` doesn't have a return value. See here: http://www.cplusplus.com/reference/cstdlib/srand/ Edit: Looks like @RSahu was faster by a couple of seconds. – Tara Aug 15 '16 at 04:02
  • 2
    Rather than `srand` and `rand` take a look into using [std::uniform_int_distribution](http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution). – user4581301 Aug 15 '16 at 05:55
  • Did this pass the compiler? `srand` does return void, and the compiler should not permit this value to be assigned to x. – Rudi Aug 15 '16 at 07:01

2 Answers2

4

srand(time(0)) seeds the random number generated by rand(). Its return type is void. void type cant be stored in an integer type variable.

See here for more information on srand().

Ravi Shankar Bharti
  • 8,922
  • 5
  • 28
  • 52
  • How do you recommend I store a data type of void for this program? I followed that link, and it has great advice. Thank you! – Matthew Aug 15 '16 at 15:10
  • why do you need to store `srand(time(0))` in a variable? simple `srand(time(0))` would also do the job. – Ravi Shankar Bharti Aug 15 '16 at 15:59
  • I tried using `rand()` throughout my program and it seems that the function is changing it's value every time I place it. – Matthew Aug 15 '16 at 16:26
  • When using `srand(time(0))`, it won't let me use `cout <<` to print anything or use the `!=` and `>` operators. It appears that it's because `srand()` is a void type while `rand()` is an int data type. Any suggestions? – Matthew Aug 15 '16 at 16:28
0

I rewrote the program to use the minimum of headers required. Since I think this is homework, I left the original error in place, but placed the error output from gcc here.

#include <iostream>
#include <cstdlib>
#include <ctime>

// this is a personal thing, but I avoid "using namespace std;"
// in any file, since it makes it less clear where some symbols
// came from

int main() 
{
    int x = std::srand(std::time(0)); // line 11
    std::cout << x << std::endl;
    for (int x = 1; x <= 10; x++) {
        std::cout << 1 + (std::rand() % 100) << std::endl;
    }

    // Windows folklore -- you should better switch your ide to not close
    // the terminal after the program was run
    std::cin.get();
}

The output of g++ -Wall foo.cpp:

foo.cpp: In function 'int main()':
foo.cpp:11:40: error: void value not ignored as it ought to be
         int x = std::srand(std::time(0));
                                        ^
Rudi
  • 19,366
  • 3
  • 55
  • 77
  • Using Visual C++, here are my errors: `a value of type "void" cannot be used to initialize an entity of type "int"` and `initializing: cannot convert from 'void' to 'int'` – Matthew Aug 15 '16 at 15:44