Pls.,, help me to find the error in ths program...
this program should accept white space,so i use string and getline...it doesn't have error detected but it doesn't come up with the desire output.
thank you in advance...

#include<iostream>
#include<string>

using namespace std;
struct node
{
	string name;
	node *nxt;
};

node *start_ptr = NULL;
node *current;
int option;

void add()
{
	node *temp, *temp2;
	temp = new node;
	cout<<"Enter your friend's name: ";
	getline(cin, temp->name);
	temp->nxt = NULL;
	
	if(start_ptr==NULL)
	{
		start_ptr = temp;
		current = start_ptr;
	}
	else
	{
		temp2 = start_ptr;
		while(temp2->nxt != NULL)
		{
			temp2=temp2->nxt;
		}
		temp2->nxt = temp;
	}
}

void display_list()
{
	node *temp;
	temp = start_ptr;
	cout<<endl;
	if(temp==NULL)
		cout<<"LIST OF FRIENDS\n\nlist is empty";
	else
	{
		cout<<"LIST OF FRIENDS\n\n";
		while(temp != NULL)
		{	
			cout<<temp->name;
			if(temp==current)
				cout<<" -->current node";
			cout<<endl;
			temp=temp->nxt;
		}
		cout<<"End of List!\n";
	}
}

void delete_start_node()
{
	node *temp;
	temp = start_ptr;
	start_ptr = start_ptr->nxt;
	delete temp;
}

void delete_end_node()
{
	node *temp1, *temp2;
	if(start_ptr==NULL)
		cout<<"The list is empty\n";
	else
	{
		temp1=start_ptr;
		if(temp1->nxt==NULL)
		{
			delete temp1;
			start_ptr=NULL;
		}
		else
		{
			while(temp1->nxt != NULL)
			{
				temp2=temp1;
				temp1=temp1->nxt;
			}
			delete temp1;
			temp2->nxt=NULL;
		}
	}
}

void move_current_on()
{
	if(current->nxt==NULL)
		cout<<"You are at the end of the list";
	else
		current=current->nxt;
}

void move_current_back()
{
	if(current==start_ptr)
		cout<<"You are at the start of the list";
	else
	{
		node *previous;
		previous=start_ptr;
		while(previous->nxt != current)
		{
			previous=previous->nxt;
		}
		current=previous;
	}
}

void main()
{
	start_ptr=NULL;
	do
	{
		display_list();
		cout<<"\nSelect your option\n"
			<<"[0] Exit\n"
			<<"[1] Add friends to the list\n"
			<<"[2] Delete friends from the start of the list\n"
			<<"[3] Delete friends from the end of the list\n"
			<<"[4] Move current pointer on one friend\n"
			<<"[5] Move current pointer back one friend\n\n>>";
		cin>>option;

		switch(option)
		{
		case 1: add(); break;
		case 2: delete_start_node(); break;
		case 3: delete_end_node(); break;
		case 4: move_current_on(); break;
		case 5: move_current_back(); break;
		}
	}
	while(option != 0);
}

Recommended Answers

All 5 Replies

Since we don't know what the desired output is nor what the program is supposed to do, what can we tell you?

You can't input a friend because there's an 'enter' key still coming in cin. use cin.get(); after the cin>>option

this program should add a name that accept "space" to a list, to delete at the start and end of the list, and to move "current node"(given name in the program) on and back.

You can't input a friend because there's an 'enter' key still coming in cin. use cin.get(); after the cin>>option

getline() function is in the library of #incude <conio.h>
u should include the library..i tried it and it works....

>getline() function is in the library of #incude <conio.h>
Um, no. The getline function (the one that accepts a std::string) is declared in the <string> header.

>but it doesn't come up with the desire output.
Here's a pattern that you'd be wise to learn so you can spot it again:

#include <iostream>
#include <string>

int main()
{
  using namespace std;

  int value;
  string line;

  cout<<"Enter a value: ";
  cin>> value;
  cout<<"Enter a line: ";
  getline ( cin, line );
  cout<<"Value: "<< value 
    <<"\nLine: "<< line <<'\n';
}

Formatted input (cin's >> operator) and unformatted input (getline) don't play well together. The error in this specific pattern is that while cin's >> operator trims leading whitespace, it doesn't trim trailing whitespace, which causes the newline character to be left in the stream for the next input to deal with. However, getline uses a newline character as a delimiter by default, so when it sees that character it terminates immediately. The effect is that the getline call is "skipped".

The fix is to remove the newline character using cin.ignore() , but a lot of people don't want to deal with dirty streams so they'll simply clear out the remaining characters as garbage and leave the stream pristine for the next input:

#include <iostream>
#include <string>
#include <ios>    // For streamsize
#include <limits> // For numeric_limits

int main()
{
  using namespace std;

  int value;
  string line;

  cout<<"Enter a value: ";
  cin>> value;

  // Clear the stream so getline is happy
  cin.ignore ( numeric_limits<streamsize>::max(), '\n' );

  cout<<"Enter a line: ";
  getline ( cin, line );
  cout<<"Value: "<< value 
    <<"\nLine: "<< line <<'\n';
}

>node *start_ptr = NULL;
>node *current;
>int option;
Global variables are difficult to work with (despite their seeming simplicity), you should learn how to pass your variables as function parameters. Also, you should include <cstddef> if you want to use the NULL macro. It's not guaranteed that either <iostream> or <string> define it for you.

>void main()
The main function returns int, period. Just change void to int and you're good. Standard C++ says that 0 will be returned automagically, so using the archaic void main is actually wasting you a keystroke on top of potentially being undefined behavior.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.