Trying to figure out the IF statement. Really new, so this will be simple I'm sure. I'm essentially just trying to roll a 6 and 20 sided die. I want the statement to run "If 20,then display the randomly selected number for 20". What am i missing (I'm sure a lot, please be nice)?
// auto dice roller
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
string mystr;
srand((unsigned)time(0));
int a;
cout << "Please select a die number: \n";
cin >> a ;
if (a = 20)
{
cout << twenty[rand() % 20] << "\n";
}
= is for assignment. == is for comparison. So it should be if(a == 20) A few other errors: twenty[rand() % 20] probably isn't doing what you want. I am assuming you want it to print out 1 if it is 1 and 2 if 2..ect right now say you roll a 20 then 20 % 20 == 0 so it would output 1. If it is 10 it will output 9, 19 it will output 20..ect.. Indexes are really offsets so they start at 0 and not 1. The easiest way would be to simply output the integer anyways like : cout << rand() % 20 + 1 << endl;
rand() returns a number between 0 and RAND_MAX (usually around 32k)
To force it in 20 different values we are using modulo opeartor. It gives you a number from 0 to 19 which after using it as array subscript returns string containing number from 1 to 20.
Easier way is to drop array completely and output: std::cout << (rand() % 20) + 1 << '\n';
Both were helpful, and that definitely fixed it. So, I have a feeling i'm not following (working at same time), but what is the difference in assignment and comparison?
assigment: you are assigning value to the variable:
1 2 3 4 5
int a;
a = 10;
cout << a << '\n'; //outputs 10
a = 30;
cout << a << '\n'; //outputs 30
comparsion: compares variable and value and returns true if they are equal:
1 2 3 4 5 6 7 8 9 10
int a = 10;
a == 5; //Comparsion, does not actually do anything here
cout << a << '\n'; //outputs 10
if (a == 5) //compares and returns false, next line is not executed
a = 100;
if (a == 10) //returns true executes next line
a = 50;
cout << a << '\n'; //outputs 50
//True implicitly converts to 1 and false to 0
cout << (a == 5) << (a == 50) << '\n'; //outputs 01
Any non zero value converts to true.
That is why if( 10 ) will be executed
Assigment also returns assigned value, so if (a = 50) is equal toif ( 50 ) and evaluates to true
== is a relational operator that compares its operands. The result is either true (operands are equal) or false (not equal). The operands do not change.
Equality comparison is commutative: a==b and b==a mean the same thing
Assignment operator = takes the value of the right side and stores that value into the variable on the left. The left operand changes. This is not commutative:
a == 7, b == 42
if you do
a = b
then a == 42
revert a to 7 by
a = 7
(b is still 42)
if you now do
b = a
then b == 7
Style tip:
Always write the constant value to the left side in comparisons: if ( 20 == d )
If you by accident miss the second =-character 20 = a, then the compiler will notice it, because it is illegal to try to assign to literal constant