user1582575
user1582575

Reputation: 89

C++ Not displaying Random number correctly

I want to generate a random number for the temperature. The code I used is below:

int Temp()
{
    // genreate random temperture

    // initialize random seed: 
    srand ( time (NULL) );

    // generate number between 1 and 100:
    int t = rand() % 100 + 1;

    std::cout << t << std::endl;
    return t;
}

When the program is run, instead of displaying a number between 1 and 100, it display the following:

010C1109

Could someone explain where or why it is going wrong?

Edit: If anyone wondering I used the following:

#include <iostream>
#include <string>
#include <fstream>
#include <istream>
#include <ctime>
#include <stdio.h>      
#include <stdlib.h>    
#include <time.h>     
#include <map>
#include <cstdlib>
#include <cstring>
#pragma once

Upvotes: 3

Views: 247

Answers (1)

Gabriel
Gabriel

Reputation: 3584

How to choose the way your numbers are displayed:

std::cout << std::hex << t << std::endl; //displays in hexadecimal
std::cout << std::dec << t << std::endl; //displays in decimal

In my example I see 58 in hexadecimal and 88 in decimal (5*16+8). Here are official links for making the post complete.

C++ forum:

http://www.cplusplus.com/forum/windows/51591/

Details explained:

http://www.cplusplus.com/reference/ios/dec/

Upvotes: 2

Related Questions