File input...

If you input in the file from the program below,
then it should be something like this:
1
2
3
4
5
n
number1
number2
...
number(n-1)

How to change the program so that the inserted input will look like this
1
2
n
number1 number2...number(n-1)


Thanks in advance.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  #include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin("myfile.in");
int n;
fin>>n;
int number[n];
for (int i =0;i<n;i++)
{
fin>>number[i];
}
return 0;
}
Last edited on
Answer: nothing. operator >> already extracts whitespace delimeted values.

But you have a bit of nonstandard C++ you should avoid:
1
2
fin>>n;
int number[n];
You cannot create array of size not known at the compile time. Some compilers allows you to use VLA extension, but you should turn nonstandard features off and not use them. Because they are nonstandard and by definition nit guaranteed to work everywhere. Use vector or explicitly create a dynamic array through new.
Last edited on
closed account (N36fSL3A)
I could be wrong, but I thought it was C99 standard, or something like that.
Yes, C99 allows that, but this is C++ and C++ does not.
It is in C99. However C++ prohibits its use.
Topic archived. No new replies allowed.