C++ 链表中的字符数组
我试图通过链表显示字符数组,但我的输出总是这样======================][。一些奇怪的行内容而不是用户键入的单词。以下是代码,是什么导致了此问题C++ 链表中的字符数组,c++,arrays,linked-list,char,character,C++,Arrays,Linked List,Char,Character,我试图通过链表显示字符数组,但我的输出总是这样======================][。一些奇怪的行内容而不是用户键入的单词。以下是代码,是什么导致了此问题 #include <iostream> #include <iomanip> #include <array> using namespace std; //declare struct struct node { char word[25] ; int count;
#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;
}
}
#包括
#包括
#包括
使用名称空间std;
//声明结构
结构节点
{
字符字[25];
整数计数;
双数;
节点*链接;
};
//创建一个全局变量HeadPointer并初始化为NULL
node*HeadPointer=NULL;
//声明函数原型声明
void插入项(char[25],int,double,node*);
作废打印列表(节点*);
int main()
{
//创建一个表示单词和计数的变量,并要求用户输入单词和计数
//创建一个节点并将项目插入该节点
char InitWord[25]=“\0”;
int InitCount=0;
双初始数=0;
char NextChar='\0';
//创建一个变量以指向列表中的位置
节点*CurrentRecordPointer=NULL;
char ContinuationFlag='Y';
while(toupper(ContinuationFlag)='Y')
{
coutCurrentRecordPointer->word[25]=InitWord[25]
这只是复制一个字符-超出缓冲区边界,以启动。您可能正在寻找strcpy
。谢谢,但代码中会是什么样子?您是否阅读了strcpy
的文档,并发现它缺少?您对它的哪一部分有困难?我不介意使用strcpy,我只是问一下将其放在代码插入项中的何处,以代替CurrentRecordPointer->word[25]=InitWord[25];