Trying to pass input string to dynamic array

The visual studio c++ program compiles and build without errors. When i try to run it from the black screen, it says: Prgram.exe has encountered a problem and needs to close. We are sorry for the inconvenience. I believe I wrote this code before and it worked without problems. I don't understand why it will not work now. I could be wrong, it's been awhile. PLEASE KINDLY HELP?




#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
string fileName,line;
ifstream infile;
cout<<"Enter the file name and complete path: ";
cin>>fileName;
int count=0;
string *A;
infile.open(fileName.c_str());
while(!infile.eof())
{
getline(infile,line);
if(line[count]=='A'||line[count]=='a')
{
A=new string[count];
A[count]=strlen(line.c_str());//THIS LINE IS CAUSING AN ERROR
cout<<A[count]<<endl;
count++;
}
}
return 0;
}
What are you doing??? strlen() gets the length of a C string, returns an int, and you're passing that to a string?
http://cplusplus.com/reference/clibrary/cstring/strlen/

And what if your file doesn't exist?

EDIT: And you're resetting A for every iteration, just so you know. See the line above the one you indicated.

-Albatross
Last edited on
I did make a mistake. It's been a long time since I programmed. I am trying pass a string such as a name into A[]. Maybe I used the wrong syntax. I copied it from an old program I wrote. Can you please clarify how to pass the input string to string array instead of: A[count]=strlen(line.c_str())
To pass a string into a string array, you just do it the easy way. :)
A[count]=line;

http://cplusplus.com/reference/string/string/operator=/

-Albatross
It worked. Thanks Albatross, your a lifesaver.
Topic archived. No new replies allowed.