+ 1
Can i give string values to my array ?
11 Answers
+ 5
Mohammed is correct, any number greater than 0 is considered true, and OR need one statement to be true to return true(or 1)so think it through if you have to do it that way. You can complicate your code a little bit with random number generator.
Add #include <cstdlib> and #include <ctime> , and inside main,
int main()
{
srand(time(0));
int min = 0;//array index 0
int max = 4;// array index 4(your last array)
int i = min + (rand() % (max-min+1));//generates number between 0-4
cout <<arr[i]; //your cout change to this
}
The rest of your code remains the same, whatever works now. I think this should work, play around with it
+ 3
this is because or operator always return a value either 1 or 0 so the result of [0 || 1 || 2 ] will be 1 as the or operator return true or 1 even if only 1 operand is true. Remember all integers greater than 0 are regarded as true so 0 || 1 is true now (true || 2 ) will again be true. So you will get the output of 2nd element of the array that is arr[1]
+ 2
yes by using pointers. But use std::vector instead
+ 1
If you are asking can an array hold strings, the answer is yes. The process is the same, string str[10]; is an array that can store 10 strings
+ 1
tyty đ, now i want to take some random values of the string array ,and uses to put them in the screen , with : cout << array [ 1 || 2 .. || 5] ; and the outpout is nothing , literally . you guys know what is wrong ?
+ 1
you have not assigned anything to your string variables ex1, ex2, ex3, ex4, ex5 so they are always initiated empty that is why you are not getting any display. Try assigning any value to these variables such as ex1 = "test1" etc and every thing will be fine.
0
post your code here or in code playground
0
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string ex1,ex2,ex3,ex4,ex5;
string arr[5] = {ex1,ex2,ex3,ex4,ex5};
cout << arr[0 || 1|| 2 ] ;
system ("pause");
return 0;
}
/* even if i put just: cout<< arr [2]; the outpout is nothing */
0
thanks! now works, but in the or operator the outpout is always the arr[1] . i havent idea what is happen
0
thanks for the solution đ , now i know how obtain random data!
0
yes you can...