PDA

View Full Version : C++ Help-Array


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?

aman
04-29-2005, 09:03 PM
You should search these forums for the proper way to read through a file, then find a tutorial on pointer usage.

sage45
04-29-2005, 09:57 PM
My question is why are you pointing to constants??? As far as I know you cannot progmatically change a constant, they are constant; in other words unchanging... This is why they are so named: constants...

-sage-

shmoove
04-29-2005, 11:31 PM
const char* data[] = {"Test"};

// ...

char buffer[256];

// ...

data[b] = buffer;

There are many things going wrong in your code. Let's look at this few lines. Like Sage said, you are declaring data[] as a constant, but then you are trying to assign to it, that's not going to work.

But even if data wasn't a constant, it's a char array, so data[b] is a char (en element in a char array is a char). but you are trying to assign buffer into it and buffer is a char array, so these are unmatching types.

shmoove

Kura_kai
04-30-2005, 12:11 AM
......errr i think i got it. i was just going with the compiler errors. but is there a variable type equal to the java arraylist? if so what are a few commands for it?

oracleguy
04-30-2005, 09:00 PM
As far as I know you cannot progmatically change a constant, they are constant; in other words unchanging... This is why they are so named: constants...

Sadly, there is a way to re-cast a constant in your code so you can change it and then you can cast it back to a constant. God knows why they wrote that functionality into it but it is possible. I don't remember the exact code though, but maybe it is for the better. lol...

Kura_kai
05-01-2005, 06:50 PM
Ok from what i know i will have to keep re-reading the file to the right line.