My first question is this:
I want to ask the user to input a list of numbers in a program. The program will keep asking for numbers as long as zero isn't entered. After its done asking for numbers, I want the program to output the numbers that should be in the list. I am implementing this with a linked list.
Here is my code
#include <iostream>
#include <cstddef>
using namespace std;
struct node
{
int data;
node* next;
};
void main ()
{
int num = 0;
node *x = new node;
do
{
cout <<"Enter a number ";
cin >> num;
x ->data = num;
x ->next = NULL;
}while(num != 0);
for(node*curr = x; curr != NULL; curr= curr->next) //Traverse the list and outputs each number
{
cout << curr->data;
}
}
I know the problem lies somewhere around here:
x ->data = num; x ->next = NULL;
It only seems to output the zero it should traverse the list and output the contents in the list.
For some reason only zero seems to be in the list.
This post has been edited by Steffan: 28 March 2009 - 04:04 PM

New Topic/Question
Reply




MultiQuote




|