-
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.
:confused: :confused: :confused: :confused: :confused: :confused: :confused: :confused: :confused:
-
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;
}
-
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;
}
-
Never mix the headers with .h with those without:
#include <fstream>
#include <string>
#include <iostream> // you need this for cin and cout
...
-
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?
-
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 :D
-
I never got into the habit of .h and .cpp, as I never made something that doesn't have anything to with templates :p