为什么会出现这样的有关结构体问题,求解答
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>
#include<iostream.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#define N 8 //假设选手人数不超过8
typedef struct node{
int num; //编号
char name[20]; //姓名
int sum; //票数
}info[N + 1];
info *p;
int numbers; // 总共的选手人数
FILE *fp;
void tianjia()
{
int i = 1;
p = (info *)malloc(sizeof(info));
char a[10], b[10];
cout<<"请输入选手的数量:"<<endl;
int n;
cin>>n;
numbers = n;
if(n<=1)
{
cout<<"输入错误!选手数量必须大于1,请从新输入:"<<endl;
tianjia();
}
else
{
cout<<"请输入"<<n<< "名选手的编号和姓名"<<endl;
while (n--)
{
cin>>p[i]->num>>p[i]->name;
i++;
}
cout<<"输入完成!"<<endl;
cout<<"选手信息如下:"<<endl;
cout<<"编号 姓名"<<endl;
for (i = 1; i <= numbers; i++)
cout<<p[i]->num<<" "<<p[i]->name<<endl;
}
}
void toupiao(){
int i;
for (i = 1; i <= numbers; i++)
p[i]->sum = 0;
cout<<"投票开始啦!请观众通过按键进行投票,按'1'为1号选手投票,按'2'为2号选手投票,以此类推,以按'0'作为投票结束标记"<<endl;
cout<<"投票现在正式开始:"<<endl;
int n;
while (1){
cin>>n;
if (n == 0)
break;
while (n<1 || n>numbers){
cout<<"不存在此号码,请重新输入:"<<endl;
cin>>n;
}
p[n]->sum++;
}
cout<<"投票完成!"<<endl;
cout<<"投票情况如下:"<<endl;
cout<<"编号 姓名 票数"<<endl;
for (i = 1; i <= numbers; i++)
cout<<p[i]->num<<" "<<p[i]->name<<" "<<p[i]->sum<<endl;
}
//希尔排序
void shellinsert(info *R, int n)
{
int i;
int j = 0;
for(int dk=n/2;dk>=1;dk=dk/2)
{
for (i = dk + 1; i <= n; i++)
{
if (R[i]->sum >R[i - dk]->sum){ //小于时,需R[i]将插入有序表
*R[0] = *R[i]; //存储待插入的记录
for (int j = i - dk; j>0 && R[j]->sum <R[0]->sum; j = j - dk)
*R[j + dk] = *R[j]; //记录后移
*R[j + dk] = *R[0]; //插入到正确位置
}
}
}
cout<<"最终排名如下:"<<endl;
cout<<"名次 编号 姓名 票数"<<endl;
int mm = 1;
for (i = 1; i <= numbers; i++){
if (i >= 2 && p[i]->sum <p[i - 1]->sum)
mm++;
cout<<mm<<" "<<p[i]->num<<" "<<p[i]->name<<" "<<p[i]->sum<<endl;
}
}
void menu()
{
cout<<" * * * * * * * * * * * * * * * * * * * * "<<endl;
cout<<" * * * * * * * * * * * * * * "<<endl;
cout<<" ** 电视大赛观众投票及排名系统** "<<endl;
cout<<" * * * * * * * * * * * * * * "<<endl;
cout<<" * * * * * * * * * * * * * * * * * * * * "<<endl;
cout<<" 1.添加选手信息 "<<endl<<" 2.投票" <<endl<<" 3.排序"<<endl<<" 4.退出"<<endl;
}
void main()
{
int i;
while (1)
{
menu();
cout<<"请输入您要选择的操作序号,按回车键确认:"<<endl;
cin>>i;
while (i<1 || i>4)
{
cout<<"输入有误,请重新输入:"<<endl;
cin>>i;
}
switch (i)
{
case 1: tianjia(); break;
case 2: toupiao(); break;
case 3: shellinsert(p, numbers);break;
case 4: cout<<"感谢使用!"<<endl; exit(0);
}
}
}
其中结构体typedef struct node{
int num; //编号
char name[20]; //姓名
int sum; //票数
}info[N + 1];
中为什么要N+1,如果是N的话,那在输入选手数量的时候就执行不下去了,请问是什么问题呢??