这里有什么错误?在int I,j的部分; [英] What is the error here? In the part of int I, j;

查看:66
本文介绍了这里有什么错误?在int I,j的部分;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#define NUM_CANDIDATE 3
#define TOWN 3

int votes[NUM_CANDIDATE][TOWN];
candidate_entry (char*name[]) ;
{int i,j;
for(i=0;i<=NUM_CANDIDATE;++i)
{cout<<"Enter name of Candidate No."<<i+1;
cin>>name[i];
for(j=0;j<TOWN;++j);
cout<<"Enter #Vote for Candidate"<<name[i]<<"in Town"<<j+1<<;
cin>>votes[i][j];
}
}
 show_total_votes(char*name[])
{int total;
cout<< "Name     Town\n";
cout<< "of Candidate     1     2     3\n";
cout<<"Total\n<<" ;

cout<< "================================<<\n";
for(i=0;i<=NUM_CANDIDATE;++i)
{total=0;
cout<<"name[i]"<<name[i];
for(j=0;j<=TOWN;++j)
{total+=votes[i][j];
cout<<"total"<<total;
}
}
int main()
{char name[NUM_CANDIDATE][NAME_LEN];
int i;

char*name_pt[NUM_CANDIDATE];

clrscr();
for(i=0;i<NUM_CANDIDATE-1;++1)
name_pt[i]=name[i];

candidate_entry(name_pt);
show_total_votes(name_pt);
	       }
getch();
return(0);
}





我的尝试:





What I have tried:

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#define NUM_CANDIDATE 3
#define TOWN 3

int votes[NUM_CANDIDATE][TOWN];
candidate_entry (char*name[]) ;
{int i,j;
for(i=0;i<=NUM_CANDIDATE;++i)
{cout<<"Enter name of Candidate No."<<i+1;
cin>>name[i];
for(j=0;j<TOWN;++j);
cout<<"Enter #Vote for Candidate"<<name[i]<<"in Town"<<j+1<<;
cin>>votes[i][j];
}
}
 show_total_votes(char*name[])
{int total;
cout<< "Name     Town\n";
cout<< "of Candidate     1     2     3\n";
cout<<"Total\n<<" ;

cout<< "================================<<\n";
for(i=0;i<=NUM_CANDIDATE;++i)
{total=0;
cout<<"name[i]"<<name[i];
for(j=0;j<=TOWN;++j)
{total+=votes[i][j];
cout<<"total"<<total;
}
}
int main()
{char name[NUM_CANDIDATE][NAME_LEN];
int i;

char*name_pt[NUM_CANDIDATE];

clrscr();
for(i=0;i<NUM_CANDIDATE-1;++1)
name_pt[i]=name[i];

candidate_entry(name_pt);
show_total_votes(name_pt);
	       }
getch();
return(0);
}

推荐答案

它不在函数声明中:

It's not in a function declaration:
int votes[NUM_CANDIDATE][TOWN];
candidate_entry (char*name[]) ;
    {
    int i,j;
    for(i=0;i<=NUM_CANDIDATE;++i)
        {
        cout<<"Enter name of Candidate No."<<i+1;&lt;!-- newline="" --="">        cin>>name[i];



我猜你的意思是:


I'm guessing that what you meant to say was:

int votes[NUM_CANDIDATE][TOWN];
void candidate_entry (char*name[])
    {
    int i,j;
    for(i=0;i<=NUM_CANDIDATE;++i)
        {
        cout<<"Enter name of Candidate No."<<i+1;&lt;!-- newline="" --="">        cin>>name[i];



这不能解决所有问题(你的 main 函数是好好搞砸了)但是它会解决这个问题。



请你缩进你的代码吧!它使整个负载更容易发现像这样的问题......


That won't fix all the problems (your main function is well screwed up as well) but it'll fix that one.

And please, indent your code! It makes it a whole load easier to spot problems like this...


使用C / C ++,分号终止命令或声明。因此,请注意使用它。



使用函数定义(实现)时,必须没有分号:

With C/C++, a semicolon terminates a command or declaration. So take care where to use it.

With function definitions (implementations), there must be no semicolon:
// Function declaration:
void candidate_entry (char*name[]) ;

// Function definition (implementation):
void candidate_entry (char*name[])
{
    // ...
}





感谢理查德指出在发布的代码和初始解决方案中缺少返回类型的事实。所以一行代码中有错误。

[/编辑]



一个常见的错误也就是在最后放置一个分号一个语句:



Thanks to Richard about pointing to the fact the return type was missing in the posted code and in my initial solution. So there are to errors in one line of code.
[/EDIT]

A common mistake is also placing a semicolon at the end of a for statement:

for(j=0;j<TOWN;++j);



这将终止循环,并且循环内不会执行下一个语句。在这种情况下,所有现代编译器都会在使用高警告级别时生成警告(但古老的Turbo-C ++可能不会这样做)。



使用C ++,局部变量应该在使用时声明,而不是在C所要求的函数之上。这样做会对现代编译器发出错误:


This terminates the loop and the next statement is not executed inside the loop. All modern compilers generate a warning in this case when using a high warning level (but the ancient Turbo-C++ might not do so).

With C++, local variables should be declared when used and not on top of the function as required for C. Doing so would have issued an error with modern compilers:

for(int j=0;j<TOWN;++j);
// Compiler throws an error here about unknown variable j
//  because j is out of scope now
cout<<"Enter #Vote for Candidate"<<name[i]<<"in Town"<<j+1<<;



但是,这可能不适用于旧的Turbo-C ++编译器。



一些一般提示:



  • 确保代码易于阅读(
  • 选择最高编译器警告级别。
  • 发生错误或警告时,请阅读该消息并尝试理解。
  • 阅读有关消息的编译器文档或在Web中查找以获取更多信息。
  • 检查发生错误的代码行和前面的行。错误或警告通常来自前一行并稍后检测到。
  • 如果您仍然遇到困难,您可能会问这样一个社区。但是然后包含完整的消息并在发布的代码中指出相应的行。

  • 你应该尝试

    You should try
    candidate_entry (char*name[])
    {
    int i,j;





    并且缺少返回类型。



    And the return type is missing.


    这篇关于这里有什么错误?在int I,j的部分;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆