Hello All,

I am having a problem making my program display and output correctly. When i run my program my count will display but my word will just give me some kind of line. Can you please help me correct this?

Thanks

Ronnie

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

//declare a structure and variables

struct node
{
	mutable char word[50];
	int count;
	node *link; //does not have to be called link but must have the struct data type ex: node
};

//Create global variable that is going to represent the head of the list.

node* HeadPointer = NULL;

//Declare function prototypes

void InsertItem( char, int, node * );
void PrintList( node *);

int main ( )
{
	char InitWord[50] ="\0";
	int InitCount = 0;

	node* CurrentRecordPointer = NULL; //represent the new record we keep adding

	char NextChar = '\0';
	char ContinuationFlag = 'Y';

	while (toupper (ContinuationFlag) == 'Y' )
	{
		cout << "Enter The Word: " << endl;
		NextChar = cin.peek( );
		if ( NextChar == '\n' )
		{	
			cin.ignore( );
		}
		cin.get ( InitWord, 50 ) ;
		cout << endl;


		cout<<"Enter The Count: " << endl;
		cin >> InitCount;

		//Create a new record
		CurrentRecordPointer = new node;
		InsertItem (InitWord[50], InitCount, CurrentRecordPointer );

		//Set our HeadPointer equal to CurrentRecordPointer, because we just want to keep track of who's at the head of the list
		
		HeadPointer = CurrentRecordPointer;

		//ask the user if they want to add more records
		cout<<"Do you wish to enter any more items?" << endl;
		cout<<"Enter 'Y' or 'N' "<<endl;
		cin>> ContinuationFlag;
		cout<<endl;

	}//end of while loop

	//Call the printlist functions to display the items
	PrintList ( HeadPointer );
	return 0;
}

//function developed

//Insert data into linked list and set link to next node.

void InsertItem (char InitWord, int InitCount, node * CurrentRecordPointer)
{
	//CurrentRecordPointer-> ( word, Initword ); 
	CurrentRecordPointer->word[50] = InitWord;
	CurrentRecordPointer->count = InitCount;
	CurrentRecordPointer->link = HeadPointer;
}

void PrintList ( node* Head )
{
	//will do formatting here
	while ( Head != NULL )
	{
		
		cout<< Head->word[50] << " " << Head->count << endl;
		Head = Head->link; //gets a next record
	}
}

InitWord[50] is invalid, in this case, since your array is actually from 0-49. Also, if you want to copy the word to the struct member, you can use strcpy.

Lastly, if you're trying to pass that array to InsertItem, then you'd need to pass a pointer to it and the size. You can also try the string type as well.

To be clear, you have your notations reversed in lines 52 and 75. The variable you're passing at line 52 is InitWord (an array of 50 characters). The argument you're expecting at line 75 is char InitWord[50] .

If you want to display a char[] or pass it to a function just use the identifier, not the index.

And word[50] is a character which doesn't exists. When you declare an array A[50], the index ranges from 0 to 49.

This article has been dead for over six months. Start a new discussion instead.