链表中的字符数组

时间:2014-10-12 00:49:34

标签: c++ arrays linked-list char character

我试图通过链表显示一个char数组,但我的输出总是这样======================] [。一些奇怪的行,而不是用户输入的单词。这里是代码,是什么导致了这个问题?

#include <iostream>
#include <iomanip>
#include <array>

using namespace std;

//declare struct

struct node
{
    char word[25] ;
    int count;
    double number;
    node *link;
};

//create a global variable HeadPointer initialize to NULL

node * HeadPointer = NULL;

//declare function prototype declarations

void InsertItem ( char[25], int, double, node * );
void printList ( node * );

int main ( )
{
    // create a variable that represents the word and the count, and ask the user for the word and count
    //create a node and inser items into the node
    char InitWord [25] ="\0";
    int InitCount = 0;
    double Initnumber = 0;
    char NextChar = '\0';

    //create a variable to point to where you are in the list
    node * CurrentRecordPointer = NULL;

    char ContinuationFlag = 'Y';

    while ( toupper ( ContinuationFlag ) == 'Y' )
    {
        cout << "Enter the word: " <<endl;
        NextChar = cin.peek ( );

        if ( NextChar =='\n' )
        {
            cin.ignore ( );
        }
        cin.get( InitWord, 24 );

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

        cout<< "Enter the Number: " <<endl;
        cin>> Initnumber;

        CurrentRecordPointer = new node;

        InsertItem ( InitWord, InitCount, Initnumber, CurrentRecordPointer );
        HeadPointer = CurrentRecordPointer;

        cout<< "Do you wish to enter any more items?" <<endl;
        cout<< "Enter 'Y' or 'N' " << endl;
        cin>> ContinuationFlag;
    } //end of while loop

    //call to print the list

    printList (HeadPointer);

    return 0;
}

//implement functions

void InsertItem ( char InitWord[25], int InitCount, double Initnumber, node * CurrentRecordPointer)
{
    CurrentRecordPointer->word[25] = InitWord[25];
    CurrentRecordPointer->count = InitCount;
    CurrentRecordPointer->number = Initnumber;
    CurrentRecordPointer->link = HeadPointer;
}

void printList ( node * Head )
{
    while ( Head != NULL)
    {
        cout<< Head->word << " " << Head->count << " "<< fixed << showpoint << setprecision (2) << Head->number << endl;
        Head = Head->link;
    }
}

0 个答案:

没有答案