Using Pointers With Strings

This is a discussion on Using Pointers With Strings within the C Programming forums, part of the General Programming Boards category; Right, I am getting into a little bit of trouble here!!! ...I know about pointers and how they work, and ...

  1. #1
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    121

    Question Using Pointers With Strings

    Right, I am getting into a little bit of trouble here!!! ...I know about pointers and how they work, and have used many in the past for different applications etc.

    Basically, I am trying to find a way to save users input through different menus so I can create a request that gets sent to the server which reads the request and responds!

    I dont want an answer, I really would like an example, I have looked everywhere; but am going nowhere!!!!

    So, for example, if i am selling a car to the user, I want to gather information about what they want; color engine size etc....

    -MENU 1-
    Please enter the color you want
    1.red
    2.blue
    3.black

    -MENU 2-
    Please choose an engine size
    1. 1.0
    2. 1.2
    3. 1.4

    -MENU 3-
    Please choose style of car
    1. 4x4
    2. Saloon
    3. Sports

    OK, so the user goes through the menu choosing options, but what i want to have is a pointer to a string (or memory containing chars) that represents a request such as:

    (using the car protocol that i just made up off the top of my head!)

    "POST MYCAR CP/1.0\r\nCar-Options:#color;red#engine;1.0#style;sports\r\n\r\n "

    I really would like to know how to save user input into memory or a string buffer so it creates the message as it goes through the different stages!

    I started off with scanf("%s", buffer) and had a different char array for each one, but it was set to 256 chars long, so there was a lot of rubbish data inbetween each of the elements in the request!!!! ...If anyone understands what I am trying to get at, please please, your help is much appriciated!!!!!

    THANKS TO EVERYONE WHO READS THIS!!!!!!!!!!!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Well, if you use sprintf then it should be easy:
    Code:
    char * colormenu[] = {"red","blue","black"};
    char * stylemenu[] = {"4x4","Saloon","Sports"};
    char command[100]; // Arbitrary length, make as long as you need it
    
    int colorchoice, stylechoice, loop;
    
    printf("Please enter the color you want\n");
    for( loop = 0; loop < sizeof colormenu / sizeof *colormenu; ++loop )
        printf("%d.%s\n",loop+1,colormenu[loop]);
    scanf("%d",&colorchoice);
    
    printf("Please enter the style you want\n");
    for( loop = 0; loop < sizeof stylemenu / sizeof *stylemenu; ++loop )
        printf("%d.%s\n",loop+1,stylemenu[loop]);
    scanf("%d",&stylechoice);
    
    sprintf(command,"POST MYCAR CP/1.0\r\nCar-Options:#color;%s#style;%s\r\n\r\n ",
            colormenu[colorchoice-1],stylemenu[stylechoice-1]);
    I skipped the engine related code to cut down on the size for this example. After the code executes, the command character array should contain the formated command string.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    121

    Wink Brilliant!!!

    Thanks a lot my friend!!! ...You have made it very clear and simple now!!! cant thank you enough!!!!

    Matt.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:34 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  3. pointers to strings
    By LiLgirL in forum C Programming
    Replies: 3
    Last Post: 04-23-2004, 02:38 PM
  4. More on pointers and strings
    By mart_man00 in forum C Programming
    Replies: 4
    Last Post: 02-10-2003, 05:10 PM
  5. help with pointers and strings
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 03-05-2002, 05:54 AM

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