Size of the array is: Engine RPM, so 10. I wanted it to auto adjust to the argument. |
However, the compiler cannot know that. All it knows is that you wrote
char rpM[];
and it has to generate instructions that allocate exactly that many bytes. Is that 0 bytes?
Use std::string. You can use getline, just like you did with the previous lines.
Besides, the >> does formatted input and whitespace is a delimiter. Thus, you current code reads only the first word (Engine) and leaves the "RPM" to stream. Guess what happens in the
inputData >> dataCount;
then?
No. Do not guess. Read the
http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/
Therein: "no characters were extracted, or the characters extracted could not be interpreted as a valid value of the appropriate type."
The getline to string reads both words and the std::string
auto adjusts -- plain arrays do not.
Nevertheless, the
inputData >> dataCount;
could fail. What to do?
Test the stream:
http://www.cplusplus.com/reference/ios/ios/operator_bool/
Rather than:
1 2
|
inputData >> dataCount;
// read data
|
do
1 2 3 4
|
if ( inputData >> dataCount ) {
// now we know that dataCount is a valid number
// read data
}
|
There can still be problems, so instead of
1 2 3 4 5
|
inputData >> dataCount;
for (i = 0; i < dataCount; i++)
{
inputData >> pressure[i] >> volume[i];
}
|
do
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
dataCount = 0;
if ( inputData >> dataCount ) {
if ( SIZE < dataCount ) dataCount = SIZE; // arrays cannot take more than SIZE
double pres;
double vol;
int count = 0;
while ( count < dataCount && inputData >> pres >> vol ) {
// we got a valid pres and vol
pressure[count] = pres;
volume[count] = vol;
++count;
}
dataCount = count; // real value
}
|
The getlines can fail too, but if they have failed, then inputData is already in failed state and reading to dataCount fails too.
PS. Do not use exit(). That is a harsh way to quit. Rather make the caller check and handle errors somehow. (For example, if dataCount is 0 after retreiveData() ...)