简体   繁体   English

在我的C程序中收到错误“意外的unqualified-id”。

[英]Recieving error “Unexpected unqualified-id” in my C program ..

I'm a beginner and I am having a problem with the code I'm writing .. I'm receiving the following error: "Unexpected unqualified-id before '{' token in line 9" 我是一个初学者,我正在编写的代码有问题..我收到以下错误:“第9行'{'令牌之前的意外unqualified-id”

Also, I don't know how to set the output and make it shown so if you guys could really help me with that I would be grateful .. 另外,我不知道如何设置输出并将其显示出来,所以如果你们真的可以帮助我,我将不胜感激..

And just to let you know .. I'm using the "Code Blocks" 只是让您知道..我正在使用“代码块”

#include<stdio.h>
#include<conio.h>

int read_temps(float temps[]);
int hot_days(int numOfTemp, float temps[]);
int printf_temps(int numOfTemp,float temps[],int numOfHotDays);

int main(void);
{

int index=0;
float tempVal;
float temps[31];
int numOfTemp,numOfHotDays;
do
 {
printf("\n Enter the noon temperature (500 as a sential value)");
scanf("%f",&tempVal);
if(tempVal!=500.0)
 {
  temps[index]=tempVal;
  index++;
 }
 }while(tempVal!=500.0);
 return index;
{
 int i;
 int count=0;
 for(i=0;i<numOfTemp;i++)
  {
 if(temps[i]>32.0)
     count++;
  }
 return count;
 }
 {
 float sum=0.0;
 int i;
 printf("\nTemperatures of the month");
 printf("\n-------------------------");

 for(i=0;i<numOfTemp;i++)
  {
 printf("\nDay %d : %.2fF",i+1,temps[i]);
 sum=sum+temps[i];
  }
 printf("\nNumber of Hot Days : %d",numOfHotDays);
 printf("\nAverage Temperature for a month : %.2f",sum/numOfTemp);

 }
 {
 clrscr();
 numOfTemp=read_temps(temps);
 numOfHotDays=hot_days(numOfTemp,temps);
 clrscr();
 printf_temps(numOfTemp,temps,numOfHotDays);
 getch();
 }
 }
int main(void);
{

Remove the semicolon. 删除分号。 You seem to misunderstand the format of function declaration and definition. 您似乎误解了函数声明和定义的格式。

Function definition: 功能定义:

void foo(void)
{
    //something
}

Function declaration: 函数声明:

void foo(void);

The code: 编码:

int main(void);
{

is actually a prototype for main followed by an opening brace. 实际上是main的原型,然后是开括号。 Since the prototype is a distinct semantic element, the brace is not a legal token at that point. 由于原型是一个独特的语义元素,因此,括号在那时还不是合法标记。

You need to remove the trailing semicolon ; 您需要删除结尾的分号; :

int main(void)
{

You also seem to have unreachable code in your main function following: 您的main功能中似乎也有无法访问的代码,如下所示:

return index;

That return statement is executed unconditionally following the do...while loop so the code following it can never be executed. return语句在do...while循环之后无条件执行do...while因此它之后的代码将永远无法执行。

That will become a lot clearer once you tidy up your formatting style (eg, use a four-space indent consistently). 整理格式样式后,这将变得更加清晰(例如,始终使用四个空格的缩进)。

In other words, something like this, where the unreachable code and unnecessary braces become obvious (the unnecessary braces are most likely the result of you forgetting to put in function definitions for the three sub-functions that you have prototypes for (a) ): 换句话说,在这样的情况下,无法访问的代码和不必要的括号变得很明显(不必要的括号很可能是您忘记为(a)的原型为三个子函数输入函数定义的结果):

#include <stdio.h>
#include <conio.h>

int read_temps (float temps[]);
int hot_days (int numOfTemp, float temps[]);
int printf_temps (int numOfTemp, float temps[], int numOfHotDays);

int main (void) {
    int index = 0;
    float tempVal;
    float temps[31];
    int numOfTemp, numOfHotDays;

    do {
        printf ("\n Enter the noon temperature (500 as a sentinel value)");
        scanf ("%f", &tempVal);
        if (tempVal!=500.0) {
            temps[index] = tempVal;
            index++;
        }
    } while (tempVal != 500.0);

    return index;

    {
        int i;
        int count = 0;
        for (i = 0; i < numOfTemp; i++) {
            if (temps[i] > 32.0)
                count++;
        }
        return count;
    }

    {
        float sum = 0.0;
        int i;
        printf ("\nTemperatures of the month");
        printf ("\n-------------------------");

        for (i = 0;i < numOfTemp; i++) {
            printf ("\nDay %d : %.2fF", i+1, temps[i]);
            sum = sum + temps[i];
        }
        printf ("\nNumber of Hot Days : %d", numOfHotDays);
        printf ("\nAverage Temperature for a month : %.2f", sum/numOfTemp);
    }

    {
        clrscr ();
        numOfTemp = read_temps (temps);
        numOfHotDays = hot_days (numOfTemp, temps);
        clrscr ();
        printf_temps (numOfTemp, temps, numOfHotDays);
        getch ();
    }
}

And one final note, though it's unrelated to your immediate problem. 最后一点,尽管与您的直接问题无关。 You should strive as much as possible to write portable code, which would entail avoiding the non-standard conio header file and the use of clrscr and getch (especially when getchar is available). 您应该尽力编写可移植的代码,这将避免使用非标准的conio头文件,而避免使用clrscrgetch (尤其是在getchar可用时)。


(a) If that is the case, you'll need to add the definition lines before each function and move them to outside of the main function. (a)如果这种情况,则需要在每个函数之前添加定义行,并将其移到主函数之外。

It's just semicolon after your main. 它只是您的主命令后面的分号。 Try this one. 试试这个。

#include<stdio.h>
#include<conio.h>

int read_temps(float temps[]);
int hot_days(int numOfTemp, float temps[]);
int printf_temps(int numOfTemp,float temps[],int numOfHotDays);

int main(void)
{

int index=0;
float tempVal;
float temps[31];
int numOfTemp,numOfHotDays;
do
 {
printf("\n Enter the noon temperature (500 as a sential value)");
scanf("%f",&tempVal);
if(tempVal!=500.0)
 {
  temps[index]=tempVal;
  index++;
 }
 }while(tempVal!=500.0);
 return index;
{
 int i;
 int count=0;
 for(i=0;i<numOfTemp;i++)
  {
 if(temps[i]>32.0)
     count++;
  }
 return count;
 }
 {
 float sum=0.0;
 int i;
 printf("\nTemperatures of the month");
 printf("\n-------------------------");

 for(i=0;i<numOfTemp;i++)
  {
 printf("\nDay %d : %.2fF",i+1,temps[i]);
 sum=sum+temps[i];
  }
 printf("\nNumber of Hot Days : %d",numOfHotDays);
 printf("\nAverage Temperature for a month : %.2f",sum/numOfTemp);

 }
 {
 clrscr();
 numOfTemp=read_temps(temps);
 numOfHotDays=hot_days(numOfTemp,temps);
 clrscr();
 printf_temps(numOfTemp,temps,numOfHotDays);
 getch();
 }
 }

You have not provided your used methods and used return before end of main. 在main结束之前,您尚未提供使用的方法和返回的信息。 That generate dead code. 那会产生无效代码。 so anyhow this won't give expected output. 所以无论如何这不会给出预期的输出。 Correct that first. 请先更正。 or update your question 或更新您的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM