Thread: am I declaring my strings wrong?

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    32

    am I declaring my strings wrong?

    I normally have no problems using strings in this manner, but this is the first time to use the .c_str() function to use a variable and two strings to create a file path for calling a file, and its giving me operational errors.

    char fileName [12];
    char path [12] = "C:\\";

    cout << "Please Enter the date to search for: \n"
    << "(NOTE: MUST BE IN MONTH DAY YEAR FORMAT";
    cin >> fileName;
    path += fileName;
    ifstream fin;
    fin.open (path.c_str(), ios::in | ios::nocreate);

    error C2297: '+=' : illegal, right operand has type 'char [12]'
    c:\hehp\viewer\viewer.cpp(98) :

    error C2228: left of '.c_str' must have class/struct/union type

    I am not overly familiar with string classes but I am reading that now, trying to understand how .c_str() function is being used with the strings I've created and why it is not happy with my code...
    I'd like to put Murphy and his laws in headlock sometimes!

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >path += fileName;

    you cannot use the += operator on arrays like that. it will not conatenate the strings for you.

    [edit] yes, use strings like prelude suggested
    Last edited by Perspective; 03-05-2003 at 05:27 PM.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    32
    Salem,

    these are the header files I am using, and have been from the start:
    # include <fstream.h>
    # include <stdlib.h>
    # include <string.h>

    I am assuming # include <string> was the your referrence to
    # include <string.h>?

    string full_path = "C:\\";

    returns mutiple errors about undeclared identifiers, and I am not understanding what your referring to about the using namespace std;

    I am guessing that string is declared from a header file and I am saying the string class "string" has the string "full_path" and then telling what "full_path" is equal to. So am I not using the write header files or am I just way off on what I gathering from everyones response? Its probably my lacking back ground in the use of string classes, but I using the code examples are returning more errors than before....
    I'd like to put Murphy and his laws in headlock sometimes!

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    I've no idea what the old name for <string> is.
    Its <string>

    The old <string.h> has functions for manipulating char* c style strings. This header is available in c++ as <cstring> where the same functions that are in <string.h> are but the caveat is that they are all included inside of namespace std.

    The header <string> is for STL string template class. You will not find functions in this header for manipulating char* strings but instead find a few template classes and functions for manipulating c++ string objects.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    32
    So what your saying is the type of compiler I am using does not use the type of header files your suggesting, thats why I am getting the error response from the compiler, using sample code suggestions?

    namespace std; is part of the <string> header file for what compiler or what OS are do you normally use?

    I am using MSVisual 6.0, I also use Linux 6.0 but have Linux 8.0 at home, have not installed yet. I was not aware at such an early stage of my learning I was already outdated!

    Stoned_Coder is correct, I reviewed the <string.h> header file and could not find anything about namespace std; However, I could not find the <cstring> header file at all.

    Is there a compiler anyone recommends? That is similar to ms visual, vi files can be frustrating on command line!

    Thanks for the help everyone, I've learned alot about my bad coding practices!!LOL!
    I'd like to put Murphy and his laws in headlock sometimes!

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    MS VC++ 6.0 will work fine - it supports old-school and new-school C++ (you can even mix em up if you want).

    <cstring> is located on my installation at:

    C:\Program Files\Microsoft Visual Studio\VC98\Include\cstring

    And if you open it up, you will that it is mostly empty except for
    Code:
    #include <string.h>
    The string class is part of the Standard Template Library (STL), which itself is now part of the C++ standard.
    Stick with VC++, keep writting code and experimenting - best way to learn.

    gg

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    32
    ok, I am understanding now, I found that header file, thanks for the heads up CodePlug, Salem, I will try your code and thanks for the visual , I think I understand now, You guys have been a huge help thanks alot!
    I'd like to put Murphy and his laws in headlock sometimes!

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    32
    GOT IT, ITS WORKING, MY CODE IS WORKING!!! VERY EXCITING, AND I LEARNED MUCHO LESSONS IN THE PROCESS! THANKS SALEM - CODEPLUG - STONED CODER!
    I'd like to put Murphy and his laws in headlock sometimes!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Worksafe Array of Strings
    By Hawkin in forum C Programming
    Replies: 4
    Last Post: 03-28-2008, 11:00 PM
  2. C++ Strings under the STL?
    By laserlight in forum C++ Programming
    Replies: 9
    Last Post: 07-19-2007, 02:55 AM
  3. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  4. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM
  5. hangman need help with strings and pointers
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-27-2002, 09:13 AM