Thread: String help

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    2

    String help

    Hi, I'm fairly new at programming with C++. I'm trying to develop a program which generates GPS strings then sends them to another computer through the serial port. The problem I have is taking a number of variables (including ints, floats, strings) and put them together in a single long string like the following:

    $GPRMC,012744,V,3840.2230,S,17801.9449,E,0.000,0.0 ,070602,20.9,E*43

    Do I have to use stringstreams or something?
    Any help would be greatly appreciated thanks.

  2. #2
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    sprintf() mebbe?
    hello, internet!

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571

    Re: String help

    Originally posted by MadKiwi
    Hi, I'm fairly new at programming with C++. I'm trying to develop a program which generates GPS strings then sends them to another computer through the serial port. The problem I have is taking a number of variables (including ints, floats, strings) and put them together in a single long string like the following:

    $GPRMC,012744,V,3840.2230,S,17801.9449,E,0.000,0.0 ,070602,20.9,E*43

    Do I have to use stringstreams or something?
    Any help would be greatly appreciated thanks.
    You can use sprintf to format a string to take integers and the like. Try this.

    Code:
    float number = 60.0f;
    char buffer[256];
    
    /* This will format your string */
    sprintf( buffer, "The number is, %f", number );
    
    cout << buffer << endl;
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You can use a stringstream or sprintf:
    Code:
    #include <iostream>
    #include <sstream>
    #include <cstdio>
    
    // Comment this line out to see stringstream.
    #define WITH_SPRINTF
    
    int main()
    {
      int i = 10;
      float f = 123.4f;
      char c = 'G';
    #ifdef WITH_SPRINTF
      char buffer[BUFSIZ];
      sprintf ( buffer, "%d,%.1f,%c", i, f, c );
      std::cout<< buffer <<std::endl;
    #else
      std::ostringstream oss;
      oss<< i <<","<< f <<","<< c;
      std::cout<< oss.str() <<std::endl;
    #endif
      return 0;
    }
    -Prelude
    Last edited by Prelude; 08-22-2002 at 07:42 PM.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    2
    OMG those replies were quick. Thanks for the help. I can now get the stringstream to work.

  6. #6
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Or the STL string class could work. Just use the + operator to conectate(sp?) the strings.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    golfinguy4 ,

    It's "concantenate". Oh, I just knew this was a pressing question with you!

    Prelude,

    (#include <stdio> for 'sprintf'. Borland. You'll know.) (That silly "winky" thing again...) (Darn it...)

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >(#include <stdio> for 'sprintf'. Borland. You'll know.)
    I know, memory lapse. But it should be #include <cstdio>.

    -Prelude
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Prelude,

    I'll defer...but only because you're right.

    #include <stdio> does work on Borland5, (tested, of course), but the standard is as you describe.

    Right again. What are the odds?

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM