array char q's

This is a discussion on array char q's within the C++ Programming forums, part of the General Programming Boards category; Hey guys I just started picking up c++. I have previously programed in python, and was a little confused as ...

  1. #1
    NotSoAvgProgrammer
    Join Date
    Jul 2007
    Location
    Virginia, U.S.
    Posts
    57

    array char q's

    Hey guys I just started picking up c++. I have previously programed in python, and was a little confused as to the array syntax in c++. I am trying to make a cash register program. I want something like "hot dog" to correlate to 1.95. I do not want to make a new var for every item the user creates, so I was thinking I would use an array. How can I make an array to hold a word in whole. For instance "word[1] = "Hot dog";" and then how can I figure out that, "Hot dog" is "word[1]" so I can call "price[1]"? Am I even on the right track of thinking or what? Is there some method that I can search for, or look into?


    Thanks,


    Joe

  2. #2
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    You could use a std::map, which works much like an associated array.
    http://www.sgi.com/tech/stl/Map.html

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,037
    To make a word correlate to something, use a map.
    For example:
    Code:
    #include <map>
    #include <iostream>
    ...
    map<string, double> myMap; //a map that maps strings to numbers.
    myMap.insert("hot dog",1.95);
    cout << myMap.find("hot dog");
    ...
    Look for tutorials or books for more detailed explanation.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,037
    Quote Originally Posted by avgprogamerjoe View Post
    How can I make an array to hold a word in whole.
    To answer this part of the question:
    Make an array of strings.
    Code:
    #include <string>
    ...
    string strArray[10];
    strArray[0]="Hot dog";//0 is the first element, remember.
    if(strArray[i]=="Hot dog")
    ...
    Or better yet a vector of strings:
    Code:
    #include <string>
    #include <vector>
    ...
    vector<string> strArray(10);
    strArray[0]="Hot dog";//0 is the first element, remember.
    if(strArray[i]=="Hot dog")
    ...
    Same effect, except a vector allows the array to grow if needed.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Don't forget the std:: prefix for strings, vectors, maps, etc if you don't have a using directive.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  3. code help required
    By Yobbo in forum C Programming
    Replies: 9
    Last Post: 09-02-2005, 11:15 PM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM

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