something wrong with code???
-
Here's a simple program I wrote just now: #include "iostream.h" #include "fstream.h" void main() { int array; ifstream infile ("binga"); infile >> array; cout << array << endl; } the file binga looks like this: 1,2,3,4,5,6 a,b,c,d,e,f one,two,three,four,five,six So when I run the program, why do get the output as -858993460 shouldn't the output just be 1
-
Here's a simple program I wrote just now: #include "iostream.h" #include "fstream.h" void main() { int array; ifstream infile ("binga"); infile >> array; cout << array << endl; } the file binga looks like this: 1,2,3,4,5,6 a,b,c,d,e,f one,two,three,four,five,six So when I run the program, why do get the output as -858993460 shouldn't the output just be 1
-
Here's a simple program I wrote just now: #include "iostream.h" #include "fstream.h" void main() { int array; ifstream infile ("binga"); infile >> array; cout << array << endl; } the file binga looks like this: 1,2,3,4,5,6 a,b,c,d,e,f one,two,three,four,five,six So when I run the program, why do get the output as -858993460 shouldn't the output just be 1
I think something wrong in your computer or your OS. Because in my computer, it's right. Output is: 1 Hung Son A Vietnamese student i-g.hypermart.net dlhson2001@yahoo.com
-
Your program does the equivalent of atoi("1,2,3,4,5,6") when enters infile>>array. You have to use space or CR to separate the numbers, not commas. rechi
-
Here's a simple program I wrote just now: #include "iostream.h" #include "fstream.h" void main() { int array; ifstream infile ("binga"); infile >> array; cout << array << endl; } the file binga looks like this: 1,2,3,4,5,6 a,b,c,d,e,f one,two,three,four,five,six So when I run the program, why do get the output as -858993460 shouldn't the output just be 1
Your main problem is simply that it's poorly written code. For one, void main is NOT valid C++. For another, you don't initialise your variable, and for a third, you don't check to see if you've succeeded in opening a file, which you give only a relative path to.
int main()
{
int array = -1;
ifstream infile ("c:\binga");if (infile.isopen())
{
infile >> array;
cout << array << endl;
}
else
cout << "Unable to open file";
}I think you'll find it cannot open/find the file and is giving you the initial value of array. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002