-
C++ code question
I need to remove an existing structure from a linked list of structures and the argument passed to remove() should be the address of the structure preceding the record to be removed....is this a good start?
Code:
#include <iostream>
#include <string>
using namespace std;
struct Info
{
string name;
string teleno;
};
void remove(Info*);
int main()
{
Info * val;
int i;
cout<< "Enter a name: ";
cin>> val -> name;
cout<< "Enter a telephone number: ";
cin>> val -> teleno;
return 0;
}
-
It is ok as a start. You need to have your val point actually point to something, although I don't know why it needs to be a pointer.
You also don't have a linked list. Were you supposed to make the structure a linked list node?
You'll also probably have to walk through the list to find the element to delete.
It's hard to say whether that really is a good start because I don't know the rest of the context.
-
does this look a bit better?
Code:
struct Info
{
string name;
string teleno;
Info * next;
};
void remove(Info*);
int main()
{
Info num;
num = remove();
cout << "The name is: " << num.name
cout << "The telephone number is: " << num.teleno << endl;
return 0;
}
-
Info looks more like a list now, but I'm not sure what you are trying to do inside your main function.
Do you have a written out (in words) description of everything you plan on doing?