String/Array Problem 
Author Message
 String/Array Problem

I get a type conversion error trying to assign the value of a string, that
is read from file, to a char array  (see the following code).  I took this
approach because the input lines from the file contain spaces.  Every book
I've looked at (and believe me when I say "I've looked at a lot!), seems to
overlook the need to include samples like this.
file://----start
#include <string>
#include <fstream>
#include <iostream>

using namespace std;

void main(){

 string s;
 char s2[10][100];
 int count=0;

 ifstream DataFile;
 DataFile.open("d:\\test\\test.txt");

 while (getline(DataFile,s)){
  s2[count]=s;
  count++;
  cout << s2[count] << endl;
 }

Quote:
}

//----end


Sat, 29 Jun 2002 03:00:00 GMT  
 String/Array Problem


Fri, 19 Jun 1992 00:00:00 GMT  
 String/Array Problem
Barry--Thank you very much.  Your solution worked.  Regarding your initial
question and putting it bluntly, I was confused.  I thought that I had to
declare the count and length of the array I was trying to store.  In other
words, I was trying to store an array of 10 records of 100 characters long
(not very efficient, but it seemed right).  Thanks again.

Fred***
----


Quote:
> I get a type conversion error trying to assign the value of a string, that
> is read from file, to a char array  (see the following code).  I took this
> approach because the input lines from the file contain spaces.  Every book
> I've looked at (and believe me when I say "I've looked at a lot!), seems
to
> overlook the need to include samples like this.
> file://----start
> #include <string>
> #include <fstream>
> #include <iostream>

> using namespace std;

> void main(){

>  string s;
>  char s2[10][100];
>  int count=0;

>  ifstream DataFile;
>  DataFile.open("d:\\test\\test.txt");

>  while (getline(DataFile,s)){
>   s2[count]=s;
>   count++;
>   cout << s2[count] << endl;
>  }

> }
> file://----end

Why is s2 a char array? Try changing it to:

string s2[100];

Also, you might get more interesting answers by reposting your question to
comp.lang.c++.moderated (with no cross-posting).

--

Barry L. Wallis, Senior Systems Engineer
Science Applications International Corporation
4161 Campus Point Court
San Diego, CA 92121
  _____
"In all labor there is profit, but mere talk leads to poverty."
- King Solomon



Sat, 29 Jun 2002 03:00:00 GMT  
 String/Array Problem


Fri, 19 Jun 1992 00:00:00 GMT  
 String/Array Problem


Quote:
> I get a type conversion error trying to assign the value of a string, that
> is read from file, to a char array  (see the following code).  I took this
> approach because the input lines from the file contain spaces.  Every book
> I've looked at (and believe me when I say "I've looked at a lot!), seems
to
> overlook the need to include samples like this.
> file://----start
> #include <string>
> #include <fstream>
> #include <iostream>

> using namespace std;

> void main(){

>  string s;
>  char s2[10][100];
>  int count=0;

>  ifstream DataFile;
>  DataFile.open("d:\\test\\test.txt");

>  while (getline(DataFile,s)){
>   s2[count]=s;
>   count++;
>   cout << s2[count] << endl;
>  }

> }
> file://----end

Why is s2 a char array? Try changing it to:

string s2[100];

Also, you might get more interesting answers by reposting your question to
comp.lang.c++.moderated (with no cross-posting).

--

Barry L. Wallis, Senior Systems Engineer
Science Applications International Corporation
4161 Campus Point Court
San Diego, CA 92121
  _____
"In all labor there is profit, but mere talk leads to poverty."
- King Solomon



Sun, 30 Jun 2002 03:00:00 GMT  
 String/Array Problem


Fri, 19 Jun 1992 00:00:00 GMT  
 String/Array Problem
Glad I could help. If you really did want to store an array of 10 records of
100 characters, your declaration was fine. However, you can't do a simple
assignment:

Quote:
> >   s2[count]=s;

You would need to use strcpy() or some other function. Since you are using
C++ strings are much easier, safer and possibly more efficient.

Glad I could help.

--

Barry L. Wallis, Senior Systems Engineer
Science Applications International Corporation
4161 Campus Point Court
San Diego, CA 92121
  _____
"In all labor there is profit, but mere talk leads to poverty."
- King Solomon


Quote:
> Barry--Thank you very much.  Your solution worked.  Regarding your initial
> question and putting it bluntly, I was confused.  I thought that I had to
> declare the count and length of the array I was trying to store.  In other
> words, I was trying to store an array of 10 records of 100 characters long
> (not very efficient, but it seemed right).  Thanks again.

> Fred***
> ----




> > I get a type conversion error trying to assign the value of a string,
that
> > is read from file, to a char array  (see the following code).  I took
this
> > approach because the input lines from the file contain spaces.  Every
book
> > I've looked at (and believe me when I say "I've looked at a lot!), seems
> to
> > overlook the need to include samples like this.
> > file://----start
> > #include <string>
> > #include <fstream>
> > #include <iostream>

> > using namespace std;

> > void main(){

> >  string s;
> >  char s2[10][100];
> >  int count=0;

> >  ifstream DataFile;
> >  DataFile.open("d:\\test\\test.txt");

> >  while (getline(DataFile,s)){
> >   s2[count]=s;
> >   count++;
> >   cout << s2[count] << endl;
> >  }

> > }
> > file://----end

> Why is s2 a char array? Try changing it to:

> string s2[100];

> Also, you might get more interesting answers by reposting your question to
> comp.lang.c++.moderated (with no cross-posting).

> --

> Barry L. Wallis, Senior Systems Engineer
> Science Applications International Corporation
> 4161 Campus Point Court
> San Diego, CA 92121
>   _____
> "In all labor there is profit, but mere talk leads to poverty."
> - King Solomon



Mon, 01 Jul 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. String Array problem....

2. String/Array Problem

3. Arrays and arrays of strings problem

4. Copy Array string to another Pointer array string in ANSI-C

5. Problem with Array of Strings

6. problems on array of strings char **

7. Problems using Arrays of Strings

8. Problem: Assigning the contents of an array to a string

9. Problem: Assigning the contents of an array to a string

10. Problem of Return Array String to Main Program in ANSI-C

11. Problems passing arrays of Strings to function.

12. problems with:char strings/arrays

 

 
Powered by phpBB® Forum Software