Results 1 to 7 of 7

Thread: Help Me!!!!

  1. #1

    Thread Starter
    Addicted Member Virtual24's Avatar
    Join Date
    May 2001
    Posts
    228

    Angry Help Me!!!!

    I dont understand some problems I'm having concerning the CString, string, and char * variable types!!

    For some reason my computer or complier doesnt like these variables... I am using VC++ 6.0.

    I am trying to find the best way to have a variable that can hold a string but can be written to with a variable of type char, like this: str[i] += ch; or str[i] = str[i] + ch;

    When I try to use a CString in this way it says Error '=': lefthand operand must be l-value. ?????

    I cant use char * because when i do that my program gives no errors and runs but then crashes when ever it gets to a line with that variable in it.

    and with my string variables i am getting this problem:
    using string with a cout or cin gives me an error: operator '<<' doesnt take a lefthand variable of class
    string...

    Does anyone know how to fix any of this????????? Plz Help. Thanx.
    To protect time is to protect everything...

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() {
        string x = "Hello";
        string y = "World";
    
        string combo = string("I say: ") + x + " " + y;
    
        cout << combo << "!" << endl;
    
        return 0;
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Addicted Member Virtual24's Avatar
    Join Date
    May 2001
    Posts
    228
    That worked just fine, can you tell me why my code produces errors???

    Code:
    #include <fstream.h>
    #include <string>
    
    using namespace std;
    
    int main() {
        string flnm, str; char ch; int i = 0;
    
    	cout << "Enter the file name..." << endl;
    	cin >> flnm;
    	cout << endl << endl;
    
    	ifstream XDEF(flnm);
    
    	while( XDEF.get(ch) ) {
    		str += ch;
    	}
    
    	cout << str << endl;
    
        return 0;
    }
    Last edited by Virtual24; Feb 24th, 2002 at 07:44 PM.
    To protect time is to protect everything...

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Never mix the headers with .h with those without:
    #include <fstream>
    #include <string>
    #include <iostream> // you need this for cin and cout
    ...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Addicted Member Virtual24's Avatar
    Join Date
    May 2001
    Posts
    228
    2 things, 1, if i include the fstream.h file in my program, i dont need to include iostream b/c it does it automatically, and 2, I thought <fstream.h> and <fstream> was the same thing, isnt it?
    To protect time is to protect everything...

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    1. Theoretically, yes. Practically, possibly not so it's safest to include what you need. Stick to the non-dot-h headers, it's better that way.

    2. The .h is the old version, the other is the newer templated version that fits in with the rest of the standard library. Use it for now, and you'll find out why it's useful when you get onto templates and generic programming
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I never got into the habit of .h and .cpp, as I never made something that doesn't have anything to with templates
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width