Thread: String, Pair and Map Problem

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    4

    String, Pair and Map Problem

    I'm new to C++ programming although I'm pretty familiar with programming in general. I have issue relating to strings I've stored. Essentially I have a map defined as follows.

    Code:
    map<string, string> resources;
    I've added a number of elements to it without any problem. Where I run into problems is after retrieving a value and trying to use the string like so.

    Code:
    string s1(resources["width"]);
    string s2("x");
    s1 += s2;
    cout << s1 << endl;
    This is a simplified version of my code but it illustrates the problem. If s1 = "1280" and I concatenate s2 onto it I will get the following output "x280" instead of "1280x".

    What am I doing wrong? Thanks.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Is it possible that you have read the resources from a file, and that the end of each line has a carriage return ('\r' or character 13/0x0D) at the end? That would print 1280\rx, which APPEARS on the screen as x280, since \r means "move the cursor to the very left of the line".

    You could print the lenght of the string (on a separate line for ease of reading).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    Thats is exactly. I assumed the carriage return got thrown away. Thank you very much.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by dream_noir View Post
    Thats is exactly. I assumed the carriage return got thrown away. Thank you very much.
    Well, it does if the file is read as text in a Windows/DOS or OS/2 system (and other systems which use the same type of line-endings). Presumably, you either do not read the file that way, or you are using for example Linux and reading a file created in another OS that has different line-endings. It's a mess, but it's been that way for a long time, so we can't easily change it, because all sorts of existing programs would break.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Problems defining classes
    By esmeco in forum C++ Programming
    Replies: 47
    Last Post: 10-24-2007, 01:13 PM
  3. search algorithm
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 09-17-2007, 02:59 PM
  4. Writing my own stl container
    By curos in forum C++ Programming
    Replies: 10
    Last Post: 12-18-2005, 04:33 AM
  5. Map Problem
    By XSquared in forum C++ Programming
    Replies: 3
    Last Post: 04-18-2003, 07:39 AM