By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,017 Members | 1,029 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,017 IT Pros & Developers. It's quick & easy.

Two questions...first, outputting strings from a datafile, and string addition...

P: n/a
Let me first state that I'm using Borland Turbo C++, it's relatively
old so the new string methods won't work. Anyways, first I'm trying to
collect a line of a string (with numbers, letters, dashes) into each
variable. For just numbers, it's relatively easy:

ifstream fout("s1.in");
for (a=0; a<17; a++) {
fout >> data[a];
cout << data[a] << endl;
}

Where a and data[17] are just regular integers. However, when I change
variable c to char[11] (since it has letters, and each line only needs
to hold 11, no more), and when I change data[a] to a char, I get these
two errors:

L value required in function xxx
Cannot convert 'int' to 'char *' in function xxx

I was wondering what would be the best fix for this. In addition, how
would string addition occur? In Java, it's relatively simple:

String out;
out = "";
out = out + "1"; ...etc

But in C++ I'm having a hard time. For string, I'm using char out[12].
When I assign out[12] = "", it won't work, I get an error. Same with
string addition, when I use out[12] = out[12] "1", it still doesn't
work. I've searched google and can't seem to find much information on
it.

If anyone can help me out, I'd appreciate it. Thanks.

Feb 26 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

<sn***********@gmail.com> schrieb im Newsbeitrag
news:11**********************@u72g2000cwu.googlegr oups.com...
Let me first state that I'm using Borland Turbo C++, it's relatively
old so the new string methods won't work.
maybe take a look on

www.bloodshed.net/dev/

or

http://msdn.microsoft.com/vstudio/ex...C/default.aspx

Anyways, first I'm trying to
collect a line of a string (with numbers, letters, dashes) into each
variable. For just numbers, it's relatively easy:

ifstream fout("s1.in");
for (a=0; a<17; a++) {
fout >> data[a];
cout << data[a] << endl;
}

Where a and data[17] are just regular integers. However, when I change
variable c to char[11] (since it has letters, and each line only needs
to hold 11, no more), and when I change data[a] to a char, I get these
two errors:
L value required in function xxx
Cannot convert 'int' to 'char *' in function xxx
duno... ms c++ can do that :S but i never have an idea whats standard and
what not :(
the error basicly means that your fout got no member for the operator >>
that takes a char pointer.

tried "string"? see below...

I was wondering what would be the best fix for this. In addition, how
would string addition occur? In Java, it's relatively simple:

String out;
out = "";
out = out + "1"; ...etc

But in C++ I'm having a hard time. For string, I'm using char out[12].
When I assign out[12] = "", it won't work, I get an error. Same with
string addition, when I use out[12] = out[12] "1", it still doesn't
work. I've searched google and can't seem to find much information on
it.


well, the char array strings are c. it would look like.

#include <string.h>
char out[12];
strcpy(out, ""); // or out[0] = 0;
strcat(out, "1");

:( it is not possible to use them with simple operators = + :(

in c++ you normaly got

#include <string>
std::string s;
s = "";
s = s + "1";

Feb 26 '06 #2

P: n/a
In message <11**********************@u72g2000cwu.googlegroups .com>,
sn***********@gmail.com writes
Let me first state that I'm using Borland Turbo C++, it's relatively
old so the new string methods won't work.
In which case you're not talking about standard C++ but some other
language which is off-topic here.

Is there some reason why you have to use that compiler, given that
there's no shortage of free C++ compilers (including a Borland one)
which provide more or less full and standard-compliant implementations
of the standard libraries?
Anyways, first I'm trying to
collect a line of a string (with numbers, letters, dashes) into each
variable. For just numbers, it's relatively easy:

ifstream fout("s1.in");
Why are you naming an _input_ stream "fout"?
for (a=0; a<17; a++) {
fout >> data[a];
cout << data[a] << endl;
}

Where a and data[17] are just regular integers. However, when I change
variable c to char[11]
??? I see no variable called 'c' in the code sample above.
(since it has letters, and each line only needs
to hold 11, no more),
A dangerous assumption. What happens when something changes, as surely
it will? Even a few lines down your post, you're (ab)using a 12-element
array.
and when I change data[a] to a char, I get these
two errors:
Post the _complete actual code_ that didn't compile, and indicate the
line numbers where the errors occur. Your description of what you are
doing is so unclear that it's anybody's guess what the compiler is
really seeing.
L value required in function xxx
Cannot convert 'int' to 'char *' in function xxx

I was wondering what would be the best fix for this.
Upgrade to a proper C++ compiler, use std::string, post minimal complete
actual code and actual error messages and indicate which lines the
errors refer to.
In addition, how
would string addition occur? In Java, it's relatively simple:

String out;
out = "";
out = out + "1"; ...etc

But in C++ I'm having a hard time.
That's because you're not using C++.

If you really can't use std::string, you're effectively programming in
C, and you need to look at the str... functions defined in <string.h>.
You also need to learn about the difference between string literals
enclosed in "" and character constants enclosed in ''.
For string, I'm using char out[12].
And there's your problem. Array-of-char is not a string type, it's
something much more primitive.
When I assign out[12] = "", it won't work, I get an error.
out[12] refers to a non-existent char one beyond the end of your array
of char, so you get undefined behaviour.

Even if you kept the subscript in range, out[0] is a char, but "" is a
string literal, which resolves into a pointer to const char, so you're
trying to assign incompatible types.
Same with
string addition, when I use out[12] = out[12] "1", it still doesn't
work. I've searched google and can't seem to find much information on
it.

If anyone can help me out, I'd appreciate it. Thanks.


--
Richard Herring
Mar 2 '06 #3

P: n/a
In message <dt**********@online.de>, Frank Schmidt <fs@example.com>
writes

<sn***********@gmail.com> schrieb im Newsbeitrag
news:11**********************@u72g2000cwu.googleg roups.com...
Let me first state that I'm using Borland Turbo C++, it's relatively
old so the new string methods won't work.
maybe take a look on

www.bloodshed.net/dev/

or

http://msdn.microsoft.com/vstudio/ex...C/default.aspx

Anyways, first I'm trying to
collect a line of a string (with numbers, letters, dashes) into each
variable. For just numbers, it's relatively easy:

ifstream fout("s1.in");
for (a=0; a<17; a++) {
fout >> data[a];
cout << data[a] << endl;
}

Where a and data[17] are just regular integers. However, when I change
variable c to char[11] (since it has letters, and each line only needs
to hold 11, no more), and when I change data[a] to a char, I get these
two errors:
L value required in function xxx
Cannot convert 'int' to 'char *' in function xxx


duno... ms c++ can do that


I very much doubt that any C++ compiler, even "ms c++", whatever that
is, supports the conversion of integers into pointers without explicit
casting.
:S but i never have an idea whats standard and
what not :(
Evidently.
the error basicly means that your fout got no member for the operator >>
that takes a char pointer.
Which doesn't match the posted code, so you're just guessing.
tried "string"? see below...

I was wondering what would be the best fix for this. In addition, how
would string addition occur? In Java, it's relatively simple:

String out;
out = "";
out = out + "1"; ...etc

But in C++ I'm having a hard time. For string, I'm using char out[12].
When I assign out[12] = "", it won't work, I get an error. Same with
string addition, when I use out[12] = out[12] "1", it still doesn't
work. I've searched google and can't seem to find much information on
it.


well, the char array strings are c. it would look like.

#include <string.h>
char out[12];
strcpy(out, ""); // or out[0] = 0;
strcat(out, "1");

:( it is not possible to use them with simple operators = + :(

in c++ you normaly got

#include <string>
std::string s;
s = "";
s = s + "1";


What's wrong with s += "1" ?

--
Richard Herring
Mar 2 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.