Can't add string literals.

This is a discussion on Can't add string literals. within the C++ Programming forums, part of the General Programming Boards category; I posted this problem in another post but seeing how that was for a different problem and I'd already been ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    28

    Can't add string literals.

    I posted this problem in another post but seeing how that was for a different problem and I'd already been given a solution I thought I'd make a new post.
    For some reason, I cannot add string literals using the + operator. I've tried tons of different functions such as copy(), strcat(), etc. BUT every friggin time, it either gives me error C2110 or it compiles and gives me a unreadable figure. I've tried conversions, I've tried everything that I know including 3 different compilers (Borland, VC, GNU GCC).

    Would someone please find my dunce mistake so I can get on with my coding self?
    If you need the compilable code please ask.

    Code:
    #include <iostream>
    #include <string>
    #include <windows.h>
    
    using namespace std;
    
    char getGrid()
    {
        char grid[6][6];
        for ( int i = 0; i < 6; i++)
        {
            for ( int j = 0; j < 6; j++)
            {
                char* szResult = new char[255];
                memset(szResult, 0x00, 255);
                char szKey[] = "grid[" + i + "][" + j + "]"; // WILL NOT DO WHAT I NEED. 
                                                              //This isn't the only option I've tried. 
                                                             //Probably something easy I'm missing...
    
                cout << szKey << " " << i << " " << j << endl; //int i and int j have normal values.
                GetPrivateProfileString("Grid", szKey, "", szResult, 255, ".\\default.ini");
                grid[i][j] = (int)szResult;
            }
    
        }
        return **grid;
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You can't do this in C or C++.
    Code:
    char szKey[] = "grid[" + i + "][" + j + "]"; // WILL NOT DO WHAT I NEED.
    But of course, you've figured that out by now!!

    Define szKey as a C++ String and then try the *concatenation operator* (aka, the plus sign).
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    However, you can't use + on integers with strings - only strings and char arrays.

    http://www.cppreference.com/wiki/str...ring_operators

    One possible solution is to use stringstream, which is the C++ corresponding to sprintf.

    --
    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.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    28
    matsp, thanks. This seems to work very well.
    Last edited by computerquip; 11-13-2008 at 12:22 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help! string literals
    By cakestler in forum C Programming
    Replies: 16
    Last Post: 02-05-2009, 10:41 AM
  2. help needed! string literals
    By cakestler in forum C++ Programming
    Replies: 2
    Last Post: 02-03-2009, 12:26 PM
  3. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  5. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM

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