Like Tree2Likes
  • 1 Post By c99tutorial
  • 1 Post By laserlight

Integrating string and int

This is a discussion on Integrating string and int within the C Programming forums, part of the General Programming Boards category; Can in c programming language, string and int can integrated each other, Code: char *Colour; int Black=10,White=5; printf("Insert Colour=");scanf("%1s",&Colour); printf("Value ...

  1. #1
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112

    Integrating string and int

    Can in c programming language, string and int can integrated each other,
    Code:
    char *Colour;
    int Black=10,White=5;
    printf("Insert Colour=");scanf("%1s",&Colour);
    printf("Value =%1s",&Colour)
    in the Value thing, i wanna print a number of Black variables,
    anyone knows?
    Last edited by loserone+_+; 01-01-2013 at 04:26 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    What do you mean?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    Quote Originally Posted by laserlight View Post
    What do you mean?
    So when i input the String in that proggrame,
    It not print a character, it will print number of the integer
    The character i input, it is based on the name based of the declaring variables of int
    sorry, if my explanation confusing, its hard to explain

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    Sorry, you need to explain better. My guess is this: the user is supposed to input the name of a colour, then you want to print the value associated with that colour. Is that correct?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    yes thats what i mean,
    do you have any idea to make that programme?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    Yes, there are a few ways of doing this. One way is to create two arrays: an array of strings and an array of integer values. Thus when the user enters a string, you search the array of strings for the string entered, then print the integer value in the other array at the same index.

    Another way is to create a struct consisting of a string and an integer value. Then you create an array of these struct objects. Thus when the user enters a string, you search the array of struct objects for the string entered, then print the corresponding integer value.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    ok, i understand for the struct thing,
    but i didnt get what u mean in that array things, i know how to use array, but i dunno to integrating int and strings,

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,409
    Laserlight means that you can put your colour names in one array and a matching integer value in the other - at the same index location so your values might be:

    black = 20 (stored at index 0 in both arrays)
    green = 30 (stored at index 1 in both arrays)

    so you then check waht the user has input, find the colour in your colour labels array and whatever the index number is when you get a match then retrieve the integer value from the other array at that same index
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  9. #9
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    Quote Originally Posted by rogster001 View Post
    so you then check waht the user has input, find the colour in your colour labels array and whatever the index number is when you get a match then retrieve the integer value from the other array at that same index
    by mean, one string input, one value, right?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    Quote Originally Posted by loserone+_+
    by mean, one string input, one value, right?
    Well, that is what you agreed that you meant: "print the value associated with that colour".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    1,053
    It sounds like you want a hashtable. A hashtable allows you to store a set of keys (typically each key is a string), and associate with each key a value. The value is often numeric, but doesn't have to be. There is no standard implementation of convention for it in C, but if you search for hashtable implementations in C, you could find it.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    I agree with c99tutorial, but I suggest that you hold it off for now, because unfortunately there is no hash table support (or indeed any other kind of mapping container support beyond arrays) in the C standard library. Get this to work with simple arrays (maybe and the struct option) first, then investigate further how to make it more efficient with a more sophisticated data structure.
    AndiPersti likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    ohh ok, thanks for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Integrating function using trapezoidal rule
    By 1thoughtMaze1 in forum C Programming
    Replies: 2
    Last Post: 02-24-2012, 05:35 PM
  2. Integrating lua into C++ application
    By Swarvy in forum Game Programming
    Replies: 2
    Last Post: 07-19-2009, 01:23 PM
  3. Integrating Boost with STLPort
    By Mario F. in forum Tech Board
    Replies: 1
    Last Post: 11-11-2006, 05:49 AM
  4. Integrating
    By Rhidian in forum C Programming
    Replies: 1
    Last Post: 04-07-2005, 03:14 PM
  5. Integrating malloc
    By Stack Overflow in forum C Programming
    Replies: 3
    Last Post: 08-02-2004, 01:20 PM

Tags for this Thread


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