Structures

This is a discussion on Structures within the C++ Programming forums, part of the General Programming Boards category; Ok, heres my problem. i have a structure here it is: struct route { char cityCode1[3]; char cityCode2[3]; char cityCode3[3]; ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    12

    Structures

    Ok, heres my problem.
    i have a structure here it is:

    struct route
    {
    char cityCode1[3];
    char cityCode2[3];
    char cityCode3[3];
    char cityCode4[3];
    char cityCode5[3];
    char cityCode6[3];
    char cityCode7[3];
    };

    route truckRoute;
    int index = 1;

    now, i'm assigning values( 3 letter chars) to the variables.. i want to be able to increment the variable names # (ex. cityCode1 to cityCode2) when I do a cin>>, would this be accomplished by the following set of code? or Could someone help me to determine the correct procedure

    cin >>cityCode
    cityCode = truckRoute.citycode<<index
    index++

    Andrew Wilmut
    Last edited by awilmut; 10-27-2001 at 05:57 PM.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Make an structure of arrays of arrays of chars...

    Code:
    #include <iostream>
    const int NUM_CODES=7;
    const int CODE_LENGTH=3;
    
    struct route 
    { 
         char cityCode[NUM_CODES][CODE_LENGTH]; 
    }; 
    
    int main() {
         route truckRoute;
         for (int i =0; i < NUM_CODES; i++) {
                std::cout << "Enter city code " << i + 1<< " for truck route: ";
                std::cin >> truckRoute.cityCode[i];
         }
         return 0;
    }

  3. #3
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    Damm you beat me

    but I think you need to declare CODE_LENGTH as 4 to take into account the '\0'

    mind you i don't do much c++.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    12
    Ok, but is what I was suggesting possible??
    i'm not that far ahead in my c++ courses to do what u suggest...

    Andrew Wilmut

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    12
    i just need a way to be able to input the 3 letter codes in sequence

  6. #6
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    No, what you said is not possible.

    cin >>cityCode
    cityCode = truckRoute.citycode<<index
    index++

    cityCode, without being bound to the name of a route, has no meaning. Also, I have absolutely no clue what this means...

    cityCode = truckRoute.citycode<<index
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  3. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21