assign string to another string (array)
Sep 3, 2015 at 8:51am UTC
Hello,
I'm trying to assign a random name (from an array) to a string
and that works great, but fails when I assign that string to another string. What am I doing wrong?
The reason for this is I want to loop the random array 4 times and have a different namn each time.
Any ideas there?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
int main() {
srand ( time(NULL) ); //initialize the random seed
string randomName[4] = {"Cake" , "Toast" , "Butter" , "Jelly" };
int RandIndex = rand() % 4; //generates a random number between 0 and 3
cout << randomName[RandIndex];
string dag1Namn1;
dag1Namn1=randomName;
cout << dag1Namn1;
return 0;
}
Last edited on Sep 5, 2015 at 2:23pm UTC
Sep 3, 2015 at 8:54am UTC
randomName is an array, and you are trying to assing it to a string. Instead you should do
dag1Namn1=randomName[RandIndex];
and everything should be ok.
Sep 3, 2015 at 9:02am UTC
Indeed! Picky thing this.
Thank you!
Topic archived. No new replies allowed.