The following piece of code (which will read a number from input.txt, provided of course that you enter 0) will compile and run fine:
Code:
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
int main()
{
int n;
cin >> n;
switch (n)
{
case 0:
char buffer[256];
ifstream inputfile ("input.txt");
if (! inputfile.is_open())
{
cout << "Error opening file";
}
while(! inputfile.eof() )
{
inputfile.getline (buffer,100);
n = atol(buffer);
cout << n;
}
break;
}
}
However, if I add another case into the switch, it will not compile:
Code:
switch (n)
{
case 0:
char buffer[256];
ifstream inputfile ("input.txt");
if (! inputfile.is_open())
{
cout << "Error opening file";
}
while(! inputfile.eof() )
{
inputfile.getline (buffer,100);
n = atol(buffer);
cout << n;
}
break;
case 1:
break;
}
Why does adding that other case make it not work?
P.S. I know the code is very strange... it is part of a program I am writing that does make sense, but I have identified this as the problem.
Thanks!