为什么我的代码有数组数据类型错误? [英] Why my code is having array data type errors?
问题描述
#include<iostream.h>
#include<conio.h>
int main()
{
void examiner(char x[6],char y[6]);
char name[10];
int score,paper;
int firstchoice,secondchoice,thirdchoice,fourthchoice;
clrscr();
struct Testreport
{ int localscore;
float percentage;
};
textbackground(RED);
cout<<"\nWelcome to REVISION TEST\n"<<"\nPlease enter your name\t";
cin>>name[10];
cout<<"\nBasic Instructions:\nAnswer in one word\nDo not use capital letters\nDo not exceed 5 letters.\nPlease choose the first subject."<<"\nPHYSICS(1),CHEMISTRY(2),MATHS(3)\t";
cin>>firstchoice;
switch(firstchoice)
{
case 1:
PHYSICS:
{
Testreport physics;
int localscore1,localscore2,localscore;
float percentage;
char ans1[6],ans2[6],ans3[6],ans4[6],ans5[6];
char sol1[6]={'l','i','g','h','t','\0'};
char sol2[6]={'s','p','e','e','d','\0'};
char sol3[6]={'f','o','r','c','e','\0'};
char sol4[6]={'p','o','w','e','r','\0'};
char sol5[6]={'i','m','a','g','e','\0'};
paper=paper+1;
localscore1=score;
cout<<"\nQ1 What is this non mechanical transverse wave that makes things visible\n";
cin>>ans1[6];
examiner(ans1[6],sol1[6]);
cout<<"\nQ2 What is another term for magnitude of linear velocity\n";
cin>>ans2[6];
examiner(ans2[6],sol2[6]);
cout<<"\nQ3 What is mass X acceleration\n";
cin>>ans3[6];
examiner(ans3[6],sol3[6]);
cout<<"\nQ4 What is work done per unit time\n";
cin>>ans4[6];
examiner(ans4[6],sol4[6]);
cout<<"\nQ5 What is formed when light enters our eyes\n";
cin>>ans5[6];
examiner(ans5[6],sol5[6]);
localscore2=score;
localscore=(localscore2-localscore1);
percentage=((localscore/score)*100);
if(paper<3)
{
cout<<"\nWhat subject do you want to attempt next CHEMISTRY(1) or MATHS(2)\nEnter 1 or 2\n";
cin>>secondchoice;
if(secondchoice==1)
{
goto CHEMISTRY;
}
if(secondchoice==2)
{
goto MATHS;
}
}
if(paper>=3)
{
cout<<name[10]<<", your score is\t"<<score;
cout<<"\nYour score and percentage of total marks in Physics is\n"<<physics.localscore<<"\n"<<physics.percentage<<"%";
break;
}
}
case 2:
CHEMISTRY:
{
Testreport chemistry;
int localscore1,localscore2,localscore;
float percentage;
char ans6[6],ans7[6],ans8[6],ans9[6],ans10[6];
char sol6[6]={'i','d','e','a','l','\0'};
char sol7[6]={'o','x','i','d','e','\0'};
char sol8[6]={'i','o','n','i','c','\0'};
char sol9[6]={'s','o','l','i','d','\0'};
char sol10[6]={'a','r','g','o','n','\0'};
paper=paper+1;
cout<<"\nQ1 PV=nRT is also called_____gas equation\n";
cin>>ans6[6];
examiner(ans6[6],sol6[6]);
cout<<"\nQ2 Compounds with two atoms of oxygen are called\n";
cin>>ans7[6];
examiner(ans7[6],sol7[6]);
cout<<"\nQ3 A metal and a non metal form_____bond\n";
cin>>ans8[6];
examiner(ans8[6],sol8[6]);
cout<<"\nQ4 A ______has the strongest intermolecular forces\n";
cin>>ans9[6];
examiner(ans9[6],sol9[6]);
cout<<"\nQ5 What is the name of the element with 18 atomic number\n";
cin>>ans10[6];
examiner(ans10[6],sol10[6]);
localscore2=score;
localscore=(localscore2-localscore1);
percentage=((localscore/score)*100);
if(paper<3)
{
cout<<"\nWhat subject do you want to attempt next PHYSICS(1) or MATHS(2)\nEnter 1 or 2\n";
cin>>thirdchoice;
if(thirdchoice==1)
{
goto PHYSICS;
}
if(thirdchoice==2)
{
goto MATHS;
}
}
if(paper>=3)
{
cout<<name[10]<<", your score is\t"<<score;
cout<<"\nYour score and percentage of total marks in Chemistry is\n"<<chemistry.localscore<<"\n"<<chemistry.percentage<<"%";
break;
}
}
case 3:
MATHS:
{
Testreport maths;
int localscore1,localscore2,localscore;
float percentage;
char ans11[6],ans12[6],ans13[6],ans14[6],ans15[6];
char sol11[6]={'r','a','n','g','e','\0'};
char sol12[6]={'r','a','t','i','o','\0'};
char sol13[6]={'a','n','g','l','e','\0'};
char sol14[6]={'s','e','v','e','n','\0'};
char sol15[6]={'e','q','u','a','l','\0'};
paper=paper+1;
cout<<"\nQ1 What is the image of domain\n";
cin>>ans11[6];
examiner(ans11[6],sol11[6]);
cout<<"\nQ2 It is just like a fraction but can never be negative\n";
cin>>ans12[6];
examiner(ans12[6],sol12[6]);
cout<<"\nQ3 What is arc/radius\n";
cin>>ans13[6];
examiner(ans13[6],sol13[6]);
cout<<"\nQ4 How many prime numbers are there between 1 and 20 ( excluding 1 )\n";
cin>>ans14[6];
examiner(ans14[6],sol14[6]);
cout<<"\nQ5 4X4 is _________ to (10/2+3x4-1)\n";
cin>>ans15[6];
examiner(ans15[6],sol15[6]);
localscore2=score;
localscore=(localscore2-localscore1);
percentage=((localscore/score)*100);
if(paper<3)
{
cout<<"\nWhat subject do you want to attempt next PHYSICS(1) or CHEMISTRY(2)\nEnter 1 or 2\n";
cin>>fourthchoice;
if(fourthchoice==1)
{
goto PHYSICS;
}
if(fourthchoice==2)
{
goto CHEMISTRY;
}
}
if(paper>=3)
{
cout<<name[10]<<", your score is\t"<<score;
cout<<"\nYour score and percentage of total marks in Maths is\n"<<maths.localscore<<"\n"<<maths.percentage<<"%";
break;
}
}
getch();
return 0;
}
void examiner(char x[6],char y[6])
{
int i=0;
int check=0;
while((x[i]!='\0')&&(y[i]!='\0'))
{
if(x[i]==y[i])
{
check=check+1;
}
i=i+1;
}
if(check==60)
{
score=score+4;
cout<<"\n"<<name[10]<<"\tyour answer is correct.Your score is\t"<<score;
}
if(check<60)
{
score=score-10;
cout<<"\n"<<name[10]<<"\tyour answer is wrong.Your score is\t"<<score;
}
}
}</conio.h></iostream.h>
推荐答案
查看给出错误的行的代码。也许,对于初学者(以及其他人的负载)来说这是一个:
Look at your code at the line that gives the error. Probably, it's this one for starters (and loads of others):
examiner(ans1[6],sol1[6]);
然后查看函数的定义:
Then look at the definitions of the function:
void examiner(char x[6],char y[6])
其中说它需要两个数组,每个数组包含6个字符;
和参数的定义:
Which says it wants two arrays, each of 6 characters;
And the definitions of the parameters:
char ans1[6],...;
char sol1[6]={'l','i','g','h','t','\0'};
所以ans1是一个包含6个字符的数组,sol1也是如此。
但是你传递给函数的是什么?
ans1 [6]
和 sol1 [6]
这两个都是:
1)单个字符值,而不是数组,而不是实际声明的数组的边界之外的
和
2) - 在C,C#,C ++中和大多数其他语言一样,数组索引是基于零的,所以一个六字符数组,将有ar [0],ar [1],... ar [5]但是没有元素在ar [6]
那么请再想一想你要做的事情,如果你想要传递两个字符或两个数组,那就确定一下 - 并传递正确的数据!
So ans1 is an array of 6 chars, and so is sol1.
But what are you passing to the function?
ans1[6]
and sol1[6]
both of which are:
1) single character values, not arrays at all
and
2) outside the bounds of the actual declared arrays - in C, C#, C++, and most other languages, the array index is zero based, so a six character array, will have ar[0], ar[1], ... ar[5] but no element at ar[6]
So think again exactly what you are trying to do, and work out if you want to pass two characters, or two arrays - and pass the right data!
这篇关于为什么我的代码有数组数据类型错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!