简体   繁体   中英

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"

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. 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:

return index;

That return statement is executed unconditionally following the do...while loop so the code following it can never be executed.

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) ):

#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).


(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.

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. That generate dead code. so anyhow this won't give expected output. Correct that first. or update your question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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