找下错误c++

来源:百度知道 编辑:UC知道 时间:2024/05/31 14:47:55
#include "stdafx.h"
#include "iostream.h"
#include "string.h"

struct person
{
char name[10];
int count;
}leader[3]={{"li",0},{"wang",0},{"zhang",0}};

void main ()
{

int i,j;
char a[10];
for (i=0;i<=5;i++)
{

cin>>a[10];
for(j=0;j<=2;j++)
{
if(strcmp(a[10],leader[j].name)==0)
leader[j].count++;
}
}
cout<<"over"<<endl;
for (i=0;i<=2;i++)
{
cout<<leader[i].name<<leader[i].count<<"\n";
}
}
error C2664: 'strcmp' : cannot convert parameter 1 from 'char' to 'const char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Error executing c

我注意到了几个问题.

1. 开头应该是 #include <stdio.h>, 其他也同样. 他们都是系统文件, 用<>, 个人建立的文件才用 ""

2. 自己新设置type, 最好是用typedef struct Person { ....}

3. leader[3] 是不是写成 person leader[3]?

4. strcmp 那一行里的, a[10] 只是一个char, 而不是string. 因此, 前面应该使用 cin >> a; 然后把后面改成
if(strcmp(a,leader[j].name)==0)

#include "iostream"
#include "string.h"
using namespace std;
struct person
{
char name[10];
int count;
}leader[3]={{"li",0},{"wang",0},{"zhang",0}};

void main ()
{

int i,j;
char a[10];
for (i=0;i<=5;i++)
{

cin>>a;
for(j=0;j<=2;j++)
{
if(strcmp(a,leader[j].name)==0)

leader[j].count++;
}
}
cout<<"over"<<endl;
for (i=0;i<=2;i++)
{
cout<<leader