C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Two questions...first, outputting strings from a datafile, a

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Sun Feb 26, 2006 1:06 am    Post subject: Two questions...first, outputting strings from a datafile, a Reply with quote



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.
Back to top
Frank Schmidt
Guest





PostPosted: Sun Feb 26, 2006 3:06 am    Post subject: Re: Two questions...first, outputting strings from a datafil Reply with quote



<snow.carriers (AT) gmail (DOT) com> schrieb im Newsbeitrag
news:1140914305.067314.105590 (AT) u72g2000cwu (DOT) googlegroups.com...
Quote:
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/express/visualC/default.aspx


Quote:
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 Sad
the error basicly means that your fout got no member for the operator >>
that takes a char pointer.

tried "string"? see below...


Quote:
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";
Back to top
Richard Herring
Guest





PostPosted: Thu Mar 02, 2006 4:06 pm    Post subject: Re: Two questions...first, outputting strings from a datafil Reply with quote



In message <dtr5d8$mdl$1 (AT) online (DOT) de>, Frank Schmidt <fs (AT) example (DOT) com>
writes
Quote:

snow.carriers (AT) gmail (DOT) com> schrieb im Newsbeitrag
news:1140914305.067314.105590 (AT) u72g2000cwu (DOT) googlegroups.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/express/visualC/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.

Quote:
:S but i never have an idea whats standard and
what not Sad

Evidently.

Quote:
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.
Quote:

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
Back to top
Richard Herring
Guest





PostPosted: Thu Mar 02, 2006 4:06 pm    Post subject: Re: Two questions...first, outputting strings from a datafil Reply with quote

In message <1140914305.067314.105590 (AT) u72g2000cwu (DOT) googlegroups.com>,
snow.carriers (AT) gmail (DOT) com writes
Quote:
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?

Quote:
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"?

Quote:
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.

Quote:
(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.

Quote:
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.

Quote:
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.

Quote:
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 ''.

Quote:
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.

Quote:
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.

Quote:
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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.