0

This is the code in the main() portion of the program:

 int numFiles;
 cout << "How many signal files are there?";
 cin >> numFiles;

 char singalFiles[numFiles][100];

 string backgroundFile;

 for (int i=0;i<numFiles;i++){
    string singalFile;
    cout << "Please input the name of singal file" << i << ".";
    cin >> singalFile;

    singalFile >> char singalFiles[i][100];

    string backgroundFile;
    cout << "Please input the name of background file" << i << ".";
    cin >> singalFile;

    backgroundFile >> char backgroundFiles [i][100];
 }

This is the code I am writing as part of a research project. I was wondering if somebody could help me with this. I am very new to c++ and do not know how to get the strings to write into a char array.

I am having trouble reading the strings into the char array so they can be stored there. That is, I am trying to read each of the strings called backgroundFile and signalFile into the char arrays backgroundFiles and singalFiles.

Jérôme
  • 8,016
  • 4
  • 29
  • 35
Channing
  • 166
  • 1
  • 9
  • "`char signalFiles[numFiles][100];`": Are you allowed to use a `std::vector` instead, or is this one of those classes where you're forced to use a crippled subset of C++? The construct you're using there is illegal for standard C++; it's a variable length array. If you need to create an array when the size isn't known at compiletime, you will have to allocate memory for it with `new`, or use a container class. Anyway, I think you're also looking for `strncpy`. – DavidO Feb 20 '15 at 20:15

1 Answers1

0

The definition char singalFiles[numFiles][100]; may be an issue as standard C++ requires the size of an array be constant. Some compilers accept this as an extension, but you shouldn't rely on it.

But as easy alternative, you can use vectors and strings:

vector<string> singalFiles(numFiles);  

Then you can easily read the data:

   //cin >> singalFile;   ==> combine with the next line
   // singalFile >> char singalFiles[i][100];
   cin >> singalFiles[i];

You don't even have to reserve for the size in advance. You could as well do:

vector<string> singalFiles;  // the size of a vector is dynamic anyway !   
... 
cin >> singalFile;  // as you did before
signalFiles.push_back(signalFile);  // add a new element to the end of the vector. 
Christophe
  • 68,716
  • 7
  • 72
  • 138
  • Thank you. This is really helpful. So i need to read on vectors in C++? – Channing Feb 20 '15 at 20:44
  • @ForrestChanningHunter yes ! Absolutely ! – Christophe Feb 20 '15 at 20:47
  • @ForrestChanningHunter: NO!... you need to pick a good book about C++ and read it cover-to-cover, not just `std::vector`. Using C++ without reading about it is a real disaster, no matter how smart you are (actually being smart makes things worse, because sometimes the correct answer is not the logical one). Pick a good book from http://stackoverflow.com/q/388242/320726 and do your future yourself a big favor. – 6502 Feb 20 '15 at 20:52
  • 1
    @ForrestChanningHunter Start with The C++ Primer, newest edition (Not The C++ Primer Plus). If you're not using the standard container classes, algorithms, you're not programming C++, but even worse, you're working too hard! – DavidO Feb 20 '15 at 21:03
  • Thanks guys! I appreciate it. I will start reading tonight. I am also using Xcode to develop and compile the code, so is c++ standard on all the compilers and IDE's? – Channing Feb 20 '15 at 21:07