Kura_kai
04-29-2005, 06:22 PM
I am having a problem with my C++ program. It keeps reading either the array wrong or the file.
// Contacts
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
const char* data[] = {"Test"};
int a=0,b=0;
const char* userResponse = "";
const char* cur = "";
int start ()
{
while (a<b)
{
cout << data[a] << endl;
a++;
/*cout << "Name:" << data[a] << endl;
cout << "Phone Number:" << data[a+1] << endl;
cout << "Email:" << data[a+2] << endl;*/
}
cin.get();
exit (1);
}
int main ()
{
char buffer[256];
ifstream examplefile ("Data.dat");
if (! examplefile.is_open())
{
cout << "Error opening file";
cin.get();
exit (1);
}
while (! examplefile.eof() )
{
examplefile.getline (buffer,100);
data[b] = buffer;
b++;
}
start();
}
Data.dat is another file which has this inside of it
Sample Name
Sample Phone Number
Sample Email
But the code i believe only reads the last line into the array. Help?
// Contacts
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
const char* data[] = {"Test"};
int a=0,b=0;
const char* userResponse = "";
const char* cur = "";
int start ()
{
while (a<b)
{
cout << data[a] << endl;
a++;
/*cout << "Name:" << data[a] << endl;
cout << "Phone Number:" << data[a+1] << endl;
cout << "Email:" << data[a+2] << endl;*/
}
cin.get();
exit (1);
}
int main ()
{
char buffer[256];
ifstream examplefile ("Data.dat");
if (! examplefile.is_open())
{
cout << "Error opening file";
cin.get();
exit (1);
}
while (! examplefile.eof() )
{
examplefile.getline (buffer,100);
data[b] = buffer;
b++;
}
start();
}
Data.dat is another file which has this inside of it
Sample Name
Sample Phone Number
Sample Email
But the code i believe only reads the last line into the array. Help?