3 Replies - 399 Views - Last Post: 08 February 2009 - 11:02 PM Rate Topic: -----

#1 Gorebus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-February 09

Quick question - error in my code

Post icon  Posted 08 February 2009 - 10:34 PM

This code is a snippet from a larger program, i'm having problems assigning a string from one string array to another. My goal is to be able to pick random colors and put them into the favorite array a couple times. I'm very new to this, thanks for you help.

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>

using namespace std;

int rand_0toN1(int n);

//global declarations
char favorite[8][31]
const char *colors[8] = {"blue", "green", "orange", "red", "black", "purple", 
"grey", "yellow"};

int main()
{
favorite[0] = colors[rand_0toN1(8)];	//the compiler tells me "incompatable types in assignment of ' " regarding this line...
cout<<favorite[0]<<endl;
}


This post has been edited by Gorebus: 08 February 2009 - 10:46 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Quick question - error in my code

#2 thepeoplescoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 97
  • Joined: 02-February 09

Re: Quick question - error in my code

Posted 08 February 2009 - 10:40 PM

View PostGorebus, on 8 Feb, 2009 - 09:34 PM, said:

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>

using namespace std;

int rand_0toN1(int n);

//global declarations
char favorite[8][31]
const char *colors[8] = {"blue", "green", "orange", "red", "black", "purple", 
"grey", "yellow"};

int main()
{
favorite[0] = colors[rand_0toN1(8)];	//the compiler tells me "incompatable types in assignment of ' " regarding this line...
cout<<favorite[0]<<endl;
}



Okay, being that you're new to this, to explain WHY that's happening would probably confuse you, so I'll tell you how to fix it. Instead, do the following.

strcpy(favorite[0], colors[rand_0toN1(8)]);


This post has been edited by thepeoplescoder: 08 February 2009 - 10:40 PM

Was This Post Helpful? 1
  • +
  • -

#3 Gorebus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-February 09

Re: Quick question - error in my code

Posted 08 February 2009 - 10:49 PM

It works great now, thanks for your quick and simple help!
Was This Post Helpful? 0
  • +
  • -

#4 thepeoplescoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 97
  • Joined: 02-February 09

Re: Quick question - error in my code

Posted 08 February 2009 - 11:02 PM

View PostGorebus, on 8 Feb, 2009 - 09:49 PM, said:

It works great now, thanks for your quick and simple help!

Ohh, no problem.

The easiest way to explain what was going on was that you were trying to assign a char pointer to a char array.

What was happening with the whole colors[xxxx] thing, was that the compiler was trying to convert that to the numerical address of what it was holding. The compiler was viewing favorite[0] as the numerical address of the first element of that array in memory (i.e. the address of favorite[0][0]) which can NEVER be changed.

The compiler thought you were trying to change the address of the favorite array, which by the standards of the language, is impossible.

Just remember, ALL variables have an address. An address is simply a number that states where a variable "lives" in memory. It's kind of like the same thing as the address of your house, which states where you live in the world, except with computers, it's simply a number.

To get the address of any variable, type &the_name_of_your_variable.

Pointers are variables that store an address, preferably, the address of another variable that you have created. To get the address of a variable that your pointer points to, simply type the_name_of_your_pointer. Note that the ampersand is not there, because we want the variable that the pointer points TO.

Of course there's more, but I don't want you to suffer from information overload just yet. Take small steps, and you'll do fine.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1