I have the following code, and am trying to read in a character string from the keyboard, and save it as the name of the vector... but as you can see from the output, the input is saved until the next cin statement, at which time the name is switched to 2!! I imagine it is some pointer problem, but I haven't fully mastered them (understatement).
I'm using Visual C++ 6.0 if it matters.
struct Vect
{
char *name;
int size;
float *entry;
};
Vect *vect;
void createNewVector()
{
// somewhere...
vect = new Vect[n];
.........
// set the name
cout << "Create New Vector \n\n Enter a name: ";
char *name;
cin >> name;
vect[vectors-1].name = name;
// checks the name values
cout << "Name1: v,n: " << vect[vectors-1].name << "," << name << endl;
char *entries;
// get the entries
cout << "Enter Number of Entries: ";
cin >> entries;
// check the name values again...
cout << "Name2: v,n: " << vect[vectors-1].name << "," << name << endl;
.........
}
/* output:
Create New Vector
Enter a name: a
Name1: v,n: a,a
Enter Number of Entries: 2
Name2: v,n: 2,2
*/
Why do the name values change when I don't change them???
I'm using Visual C++ 6.0 if it matters.
struct Vect
{
char *name;
int size;
float *entry;
};
Vect *vect;
void createNewVector()
{
// somewhere...
vect = new Vect[n];
.........
// set the name
cout << "Create New Vector \n\n Enter a name: ";
char *name;
cin >> name;
vect[vectors-1].name = name;
// checks the name values
cout << "Name1: v,n: " << vect[vectors-1].name << "," << name << endl;
char *entries;
// get the entries
cout << "Enter Number of Entries: ";
cin >> entries;
// check the name values again...
cout << "Name2: v,n: " << vect[vectors-1].name << "," << name << endl;
.........
}
/* output:
Create New Vector
Enter a name: a
Name1: v,n: a,a
Enter Number of Entries: 2
Name2: v,n: 2,2
*/
Why do the name values change when I don't change them???