Recieving error "Unexpected unqualified-id" in my C program .. - c

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

Related

C programming parameters

#include <stdio.h>
void getScores(int a, char n[10][15], int s[10]) {
int score;
printf("Enter the number of students: ");
scanf("%d",&a);
for (int i=0; i < a;i++)
{
scanf("%s",n[i]);
scanf("%d",&score);
s[i]=score;
}
}
void printScores(int a, char n[10][15], int s[10] ) {
for (int i=0; i < a;i++)
{
printf("%s", n[a]);
printf(" ");
printf("%d\n",s[a]);
}
}
int main() {
char names[10][15];
int scores[10];
int num;
getScores(num,names,scores);
printScores(num,names,scores);
}
What I am trying to accomplish is have the parameter value of int a from the getScores function to be used in the printScores function as an array length as it is being used in getScores.
The arrays are saving its value when used in the print function but the a value is resetting to an unassigned number 896 when I need it to be what the user enters in the get function. Any tips?
Print scores will not print anything.
void getScores(int *a,
And the call
getScores(&num
You need also change accordingly the functions code

Identify an Armstrong number

I Wrote this code which can identify whether a number is a Armstrong number or not
#include <stdio.h>
#include <stdlib.h>
int n;
const int input()
{
printf("insert n:");
scanf("%d",&n);
return n;
}
int Num_amount()
{
int amount=0;
while(n>=10)
{
amount++;
n=n/10;
if(n<10)
amount++;
}
return amount;
}
int Armstrong()
{
n=input();
int v;
int z=0;
int y=10
int x=Num_amount();
int m[100]={};
int i;
for(i=0;n>=10;i++)
{
v=n%10;
m[i]=pow(v,x);
z=z+m[i];
y=y*10;
}
return z;
}
int main()
{
int z=Armstrong();
printf("%d",z);
}
When run with n=153 i always get 0.After several debugging,I found out the problem is somewhere in the Armstrong function(most likely)
int Armstrong()
{
n=input();
int v;
int z=0;
int y=10
int x=Num_amount();
int m[100]={};
int i;
for(i=0;n>=10;i++)
{
v=n%10;
m[i]=pow(v,x);
z=z+m[i];
y=y*10;
}
return z;
}
The debug watches indicate that instead of execute the for loop,it went straight to the return z line,I have tried everything but still can't figure it out.Can you tell me what the problem is?
You are getting the wrong result because of some logical error. When you are choosing a variable to be global, you need to consider that the variable value can be modified by any function and in this case, you have already modified its value in num_amount function. You have also made some logical error in Num_amount and Armstrong function.
You haven't included math.h header file for pow.
Here is your modified code,
#include <stdio.h>
#include <stdlib.h>
#include<math.h> //<-------------Should have included
int n;
const int input()
{
printf("insert n:");
scanf("%d",&n);
return n;
}
int Num_amount() //<------------modified
{
int z = n; //<--------take a copy of global n
int amount=0;
while(z>0)
{
amount++;
z=z/10;
}
return amount;
}
int Armstrong() //<------------modified
{
n=input();
int v;
int z=0;
int x=Num_amount();
int i;
while(n>0)
{
v=n%10;
z+=pow(v,x);
n/=10; //<-------modification of global n
}
return z;
}
int main()
{
int z=Armstrong();
printf("%d",z);
}
Found a lot problems with the code. Here is a modified version.
1. Do not use a global variable.
2. Make calculation for power easier.
3. Return the status of result, not the result. You want to check whether number is Armstrong or not.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int no_digits(int n){
int digits = 0;
while(n){
digits++;
n = n/10;
}
return digits;
}
int armstrong(){
int n;
printf("insert n:");
scanf("%d",&n);
int digits = no_digits(n);
int curnum = 0,original = n;
while(n){
curnum += pow(n%10,digits);
n /= 10;
}
if(original == curnum)
return 1;
return 0;
}
int main(){
if(armstrong())
printf("Is Armstrong\n");
else printf("Not Armstrong\n");
}
Let's take a look at your loop:
for(i=0;n>=10;i++)
{
v=n%10;
m[i]=pow(v,x);
z=z+m[i];
y=y*10;
}
What's the value of n at this point? You've set it in the previous call to Num_amount like so:
while(n>=10)
{
amount++;
n=n/10;
if(n<10)
amount++;
}
So, after Num_amount has finished executing, n must be less than 10, meaning the loop in Armstrong won't execute.
This is a big reason why you shouldn't use globals, even in a toy program like this. If you use it for different purposes in different places, you just create headaches like this.
At the very least, you should change your code such that n is passed as a parameter to Num_amount and Armstrong.
Your function Num_amount() return "n" value is already less than 10 and for loop never run.

Nested functions! error. please Enlighten me

Guys can you help me solve this problem is c programming. I dont get it why i get nested functions when i try to compile the code. I always got ISO forbids nested functions. Please help me understand the code. 3 days. im loosing my mind. Im still new to c programming. thank you!
#include<stdio.h>
#define lcost 35;
#define tax 0.85;
void readData(int* Len,int* Wid,float* Disc,float* cost);
int calcInPrice(float area,float iPrice,float cost,int Len,int Wid);
int calcSTotal(float sTotal,float Disc,float iPrice,float tDisc);
int calcTPrice(float tPrice,float ctax,float sTotal);
void printMes(int Len, int Wid);
void printCharges(float area,float cost,float iPrice,float Disc,float tDisc,float tPrice,float ctax,float sTotal);
int main(void)
{
int x;
int Len, Wid;
float item;
float area, cost, Disc, iPrice, sTotal, tDisc, tPrice, ctax;
do{
system("cls");
printf("[1] Perform\n");
printf("[2] Exit progres\n");
printf("\n\nInput Selection:");
scanf("%d", &x);
switch (x)
{
case 1:
readData(&Len,&Wid,&Disc,&cost);
calcInPrice(area,iPrice,cost,Len,Wid);
calcSTotal(sTotal,Disc,iPrice,tDisc);
printMes(Len,Wid);
printCharges(area,cost,iPrice,Disc,tDisc,sTotal,ctax,tPrice);
printf("\n\nPress any key to return to main menu");
getch();
break;
case2:
system("cls");
printf("Exiting Program");
break;
default:
printf("\n\nInvalid Selection!");
}
}while(x < 3);
void readData(int* Len,int* Wid,float* Disc,float* cost)
{
printf("Input Length of Room: ");
scanf("%d",Len);
printf("Input Width of Room: ");
scanf("%d",Wid);
printf("Input Discount of Customer: ");
scanf("%f",Disc);
printf("Input the cost per square foot: ");
scanf("%f",cost);
return;
}
int calcInPrice(float area,float iPrice,float cost,int Len,int Wid)
{
area = Len * Wid;
iPrice = (lcost * area) + (cost * area);
return;
}
int calcSTotal(float sTotal,float Disc,float iPrice,float tDisc)
{
Disc = (Disc / 100)*iPrice;
sTotal = iPrice - (tDisc);
return;
}
int calcTPrice(float tPrice,float ctax, float sTotal)
{
ctax = sTotal * tax;
tPrice = sTotal + ctax ;
}
void printMes(int Len, int Wid)
{
printf("\n\t\tMEASUREMENT\n\n");
printf("Length\t\t %d ft", Len);
printf("Width\t\t %d ft", Wid);
printf("\nArea\t\t %d square ft", Len * Wid);
return;
}
void printCharges(float area,float cost,float iPrice,float Disc,float tDisc,float tPrice,float ctax,float stotal)
{
float item = cost * area;
printf("\n\n\t\tCHARGES\n\n");
printf("DESCRIPTION\tCOST/SQ.FT.\tCHARGE");
printf("\n________\t _________\t______");
printf("\nCarpet \t%.2f \t %0.2f ",cost,item);
printf("\nLabor \t %lf \t %0.2f",lcost, area);
printf("\n\t\t\t__________");
printf("INSTALLED PRICE \t\t %.2f", iPrice);
printf("\nDiscount \t %.2f \t %.2f ",Disc,tDisc);
printf("\n\t\t\t\t______");
printf("\nSUBTOTAL\t\t\t %.2f",sTotal);
printf("\nTax\t\t\t\t %2f",ctax);
printf("\nTOTAL\t\t\t\t %.2f",tPrice);
return;
}
return 0;
}
Your code looks like this:
int main(void)
{
...
void readData(int* Len,int* Wid,float* Disc,float* cost)
{
...
}
... more functions ...
void printCharges(float area,float cost,float iPrice,float Disc,float tDisc,float tPrice,float ctax,float stotal)
{
...
}
return 0;
}
which means that you're "nesting" all of your function definitions (readData, printCharges, etc.) inside your main function.
That's not allowed.
Instead, you need to write something more like this:
int main(void)
{
...
return 0;
}
void readData(int* Len,int* Wid,float* Disc,float* cost)
{
...
}
... more functions ...
void printCharges(float area,float cost,float iPrice,float Disc,float tDisc,float tPrice,float ctax,float stotal)
{
...
}
You cannot define a function within another function in standard C.
You can declare a function inside of a function, but it's not a nested function.
gcc has a language extension that allows nested functions. They are nonstandard, and as such are entirely compiler-dependent.
The problem is that your main function doesn't end before you start to define your readData() function, so to the compiler it looks like you are defining "readData()" inside of main(). This is called nesting, and is not valid in C. This is one reason why it's important to choose a style for indentation and bracket placement in languages like C.
I'm going to help you figure out how to figure out how to fix it, by winding your program back to a better basis to start from, and you can gradually reintroduce the rest of your code -- don't just add it all in at once, but add it piece-by-piece and make sure it works as you go.
#include <stdio.h>
// proto-type, tells the compiler that we will define this function later.
void readData(int*, int*, float*, float*);
int main()
{
int Len, Wid;
float Disc, Cost;
printf("in main\n");
readData(&Len, &Wid, &Disc, &Cost);
return 0;
} // end of main()
void readData(int* Len, int* Wid, float* Disc, float* Cost)
{
printf("We're in readData now\n");
} // end of readData()
Here it is on ideone so you can see it works.

C program keeps crashing

My program keeps crashing. The codes seems legit and correct. Wonder what's the problem.
#include <stdio.h>
void queue(int length,int turns){
int permutations,totalTurns;
turns++;
if (length>0){
queue(length-1,turns);
if (length>1){
queue(length-2,turns);
}
}
else{
permutations++;
totalTurns+=turns;
}
}
int main()
{
while(true){
int length;
float average;
int permutations=0;
int totalTurns=0;
printf("Queue length: ");
scanf("%d", &length);
queue(length,-1);
average=totalTurns/permutations;
printf("The average turns are %f", average);
}
}
int permutations=0;
average=totalTurns/permutations;
You're dividing by zero.
Note that the permutations variable you've declared in main() is different from the one in queue().
You should probably return the permutations value from queue(), like this:
int queue(int length,int turns){
int permutations = 0;
...
return permutations;
}
int main(void) {
...
int permutations = queue(length,-1);
}
You should declare permutations as a global variable, i.e. outside main function as well as totalTurns because as others mentioned it's always 0 because even thought you declare it in function queue it's being forgotten outside it.
#include <stdio.h>
static int permutations=0;
static int totalTurns=0;
void queue(int length,int turns){
turns++;
if (length>0){
queue(length-1,turns);
if (length>1){
queue(length-2,turns);
}
}
else{
permutations++;
totalTurns+=turns;
}
}
int main()
{
while(true){
int length;
float average;
int totalTurns=0;
printf("Queue length: ");
scanf("%d", &length);
queue(length,-1);
average=totalTurns/permutations;
printf("The average turns are %f", average);
}
}

Why Do I Get "nested functions are disabled..." Error in my Code?

Basically, a program to find the largest prime factor of a number. Don't know if the logic is correct cuz I can't run it to test it. I know this is long. But please forgive me.
//largest_prime.c
#include <stdio.h>
int main() {
int num,large;
int prime(int);
int lprime(int);
printf("Enter number: ");
scanf("%d",&num);
large = lprime(num);
printf("Largest Prime = %d\n",large);
return 0;
}
int lprime(int num) {
int i=num-1,large;
while (i!=-1) {
if (num%i==0) {
if (prime(i)==1) {
large=i;
i=-1;
}
i--;
}
return large;
}
int prime(int num) {
int prime,i=num-1;
while (i!=-1) {
if(num%i==0) {
if(i!=1) {
i=-1;
return 0;
}
else
return 1;
}
i--;
}
}
I get this as output at terminal (I use Mac) using GCC:
largest_prime.c: In function ‘lprime’:
largest_prime.c:26: error: nested functions are disabled, use -fnested-functions to re-enable
largest_prime.c:39: error: syntax error at end of input
I tried -fnested-functions to re-enable. I get:
largest_prime.c: In function ‘lprime’:
largest_prime.c:39: error: syntax error at end of input
Please Help!
lprime is missing trailing '}' (in fact i believe you lost while's trailing '}')
your are missing a } after return large;
the compiler deduced you are beginning a new function declaration inside lprime hence
the nested function error
Functions cannot be nested in ISO C; enabling some compiler extension to allow then is not the solution; just make you code valid C. Your indentation style is not helping you here; but the fact that the last line of each function is not at the left margin should ring alarm bells. The following compiles but not without warnings (added lines marked):
//largest_prime.c
#include <stdio.h>
// Forward declarations
int lprime(int num) ; //********************
int prime(int num) ; //********************
int main() {
int num,large;
printf("Enter number: ");
scanf("%d",&num);
large = lprime(num);
printf("Largest Prime = %d\n",large);
return 0;
}
int lprime(int num) {
int i=num-1,large;
while (i!=-1) {
if (num%i==0) {
if (prime(i)==1) {
large=i;
i=-1;
}
i--;
} //********************
}
return large;
}
int prime(int num) {
int prime,i=num-1;
while (i!=-1) {
if(num%i==0) {
if(i!=1) {
i=-1;
return 0;
}
else
return 1;
}
i--;
}
}
The warnings are:
main.cpp(34) : warning C4101: 'prime' : unreferenced local variable
main.cpp(46) : warning C4715: 'prime' : not all control paths return a value
I have not executed the code, so whether it works correctly is for you to determine.

Resources