Thread: set string array equal variable

  1. #1
    Registered User
    Join Date
    May 2004

    Question set string array equal variable

    I'm new to arrays, can anyone help explain my fault... Other than my lack of knowledge

    I declare an array of strings:
    char stations [20] [11];
    then I try to set an element of the array determined by the value of a variable ->count to equal another variable -> partnerId.
    //determine recType and populate array if necessary
    		if (recType == 'p' || recType == 'P')
    			count = stationId; //set count equal to stationId
    			stations [count]== partnerId; //set element equal to partnerId
    			cout <<stations[count]<<endl; //display the element just set
    Every things seems to work fine, but my output on screen is nothing but garble. Can someone tell me what I am doing wrong?

    thanks as always for the help-

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Ontario, Canada
    A) You're using a comparison operator for assignment.
    B) You need to use strcpy() to copy the string from partnerID to stations. You can't use the assignment operator to copy strings.
    strcpy( stations[count], partnerId );
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Manassas, VA USA

    Lightbulb Array Insight


    I happen to love arrays.. they are my favorite type of data structure.

    Well.. it looks like you have successfully declared a 'multi-dimensional' character array.. 20 elements by 11 elements.

    A typical problem with arrays are "off by one" errors when cycling through a FOR loop... which leads to accessing one element outside the bounds of the array... which can lead to garbage.

    Please supply more code.. include any associated FOR loops.
    Last edited by The Brain; 06-29-2004 at 04:48 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    Registered User
    Join Date
    May 2004
    xsquared, that worked perfectly, I see your point, that makes sense with the error message I got using a single = earlier. Thanks soooooo much.

    Brain, thanks for you enthusiasm and you have already saved me a question later...

    I appreciate everyones help this site rocks!


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 06:04 AM
  4. Set string variable
    By Beaner in forum C Programming
    Replies: 3
    Last Post: 01-28-2006, 06:15 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM