Thread: converting char to string

  1. #1
    Registered User deleeuw's Avatar
    Join Date
    Aug 2001
    Posts
    31

    converting char to string

    Hey all,
    I'm stumped! How do I convert
    char X to std::string sob (see below)?


    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    #include <string.h>
    #include <fstream.h>
    
    main(){
    FILE *OB2;
    char x[5];
    cin>>x[5];
    obCheck:
    std::fstream OB;
    OB.open("./oob", std::ios::in);
    std::string sob(x);
    while (!OB.eof()){
    cout<<sob<<endl;
    std::getline(OB, sob);
    }
    OB.close();
    cout<<sob<<endl;
    if (x=sob){
    cout<<"Unit name already used. Please enter another name"<<endl;
    cin>>x;
    goto obCheck;
    }
    
    else{
    }
    }
    THX
    Deum solum fidentia est

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    first of all, take the .h off from the headers.

    Code:
    cin >> x[5];
    should be
    Code:
    std::cin >> x;
    Are you trying to compare x and sob?

  3. #3
    Registered User deleeuw's Avatar
    Join Date
    Aug 2001
    Posts
    31
    yes,
    I'm trying to compare x to sob.

    Thx`
    Deum solum fidentia est

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    13
    Code:
    if (x=sob){
    ... what are you trying to do? are you trying to compare them? or are you assigning sob to x?

    from your use of the if statement i'd say your comparing them... in this case it should be if(x==sob) and i would declare x as a string or a pointer to a char so..

    string x = "whatever";
    or
    char *y = "whatever";

    cout<<x; and cout<<y; will both print "whatever" i dunno if that helps, caues i'm confused about what your doing exactly.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Since x is a c-string, and sob is a std::string, you could compare them as such:

    Code:
    if(strcmp(x, sob.c_str()) == 0) {
      // do stuff
    }

  6. #6
    Registered User deleeuw's Avatar
    Join Date
    Aug 2001
    Posts
    31
    OK--Stupid Question:
    what is a "c_string"?
    Deum solum fidentia est

  7. #7
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    A C string is but a simple null-terminated string, a array of characters terminated with the character null (the one with integer-value 0). In C++, most people use the string class of the standard library. (but I don't...)

  8. #8
    Registered User deleeuw's Avatar
    Join Date
    Aug 2001
    Posts
    31
    Hey all,
    Thanks for everyone's help.
    I finally got the thing working:
    it reads and appends the list fine.

    Thanks again!
    Deum solum fidentia est

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. string to char converting problem
    By bergziege in forum C++ Programming
    Replies: 2
    Last Post: 08-07-2007, 03:37 PM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. converting string to integer, for further use
    By shoobsie in forum C Programming
    Replies: 2
    Last Post: 07-01-2005, 03:12 AM
  5. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM