Expected initializer before int как исправить

Arduino Forum

Loading

You forgot a ; in line 4 of your code dude! (In the image you posted.)

Actually there is no need of line 4: int lsearch(int [], int, int). Because in the next line you are defining the function itself. You can skip the prototype declaration if you wish.

And from next time please post proper code, not just an image. And by code I mean actual code that is causing error. Here the code in your image is different from the one which you have typed in your post!

In your typed code you are calling lsearch as lsearch(arr[], N, ITEM) [line 34]. A call should be made like this: lsearch(arr, N, ITEM).

Here is you corrected code:

#include <iostream>
using namespace std;

int lsearch(int [], int, int);

int main() {
    int N, ITEM, INDEX, ar[20];
    cout << "How many elements? (max 20): " << endl;
    cin >> N;
    cout << "Enter elements: " << endl;
    for (int i = 0; i < N; i++)
        cin >> ar[i];
    cout << "Your array is as follows: " << endl;
    for (int i = 0; i < N; i++)
        cout << ar[i] << endl;
    cout << "Enter the element to be searched for: " << endl;
    cin >> ITEM;
    INDEX = lsearch(ar, N, ITEM);
    if (INDEX == -1)
        cout << "Element not found!" << endl;
    else
        cout << "Item found at index: " << INDEX << " position: " << INDEX + 1 << endl;
    return 0;
}

int lsearch(int ar[], int N, int ITEM) {
    for (int i = 0; i < N; i++)
        if (ar[i] == ITEM)
            return i;
    return -1;
}

Sample Run:

How many elements? (max 20): 5
Enter elements: 1 2 3 4 5
Your array is as follows: 
1
2
3
4
5
Enter the element to be searched for: 4
Item found at index: 3 position: 4

This code is practically the same (I guessed this code from your image):

#include <iostream>
using namespace std;

int lsearch(int[], int, int); // Your line 4 which isn't necessary and where you missed a semi-colon!
int lsearch(int ar[], int N, int ITEM) {
    for (int i = 0; i < N; i++)
        if (ar[i] == ITEM)
            return i;
    return -1;
}

int main() {
    // same as in above code
}

You should also check out this thread on why “using namespace std” is considered a bad practice.

expected initializer before ‘int’

prog3.cpp:10:1: error: expected initializer before ‘int’
int main ()
^
this is what i get when i run this program please any help regarding this it will be much appreciated and please tell me if i made this program workable like with boolean expression if the function and prototype are well declared and performing together

/*This program ask user to input Hour, Minute and seconds and if the user
put in the valid format it runs othervise it says error. */

#include <cstdio>

bool readTime(int &hours, int &minutes, int &seconds)

int main ()
{
int h,m,s;
if(readTime(h,m,s)){
printf(“%2d:%2d:%2d” h, m, s);
return 0;
}
}

bool readTime(int &hours, int &minutes, int &seconds);
{
int hh,mm,ss;

printf(“please enter time in format 09:30:50n”);
int count = scanf(“%2d:%2d:%2d”, &hh, &mm, &ss);
}
// did they get the right format?
if (count !=3){
printf(“Invalid formatn”);
return false;
}
// is the number of hours correct?
if ((hh <0)||(hh >23)){
printf(“Invalid hour %d n”, hh);
return false;
}
//is number of minutes wrong?
if ((mm <0)||(mm >0)){
printf(“Invalid Minutes %d n”, mm);
return false;
}
//is number of seconds wrong?
if ((ss <0)||(ss >0)){
printf(“Invalid seconds %d n”, mm);
return false;
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cstdio>

bool readTime(int &hours, int &minutes, int &seconds) ; // semicolon

int main ()
{
    // ...
}

bool readTime(int &hours, int &minutes, int &seconds) //no semicolon
{
    // ...
}

@JLBorges

now this is what i get after doing your instructions

prog3.cpp: In function ‘int main()’:
prog3.cpp:14:28: error: expected ‘)’ before ‘h’
printf(“%2d:%2d:%2d” h, m, s);
^
prog3.cpp:14:35: warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat=]
printf(“%2d:%2d:%2d” h, m, s);
^
prog3.cpp: In function ‘bool readTime(int&, int&, int&)’:
prog3.cpp:24:8: warning: unused variable ‘count’ [-Wunused-variable]
int count = scanf(“%2d:%2d:%2d”, &hh, &mm, &ss);
^
prog3.cpp:25:4: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
prog3.cpp: At global scope:
prog3.cpp:27:4: error: expected unqualified-id before ‘if’
if (count !=3){
^
prog3.cpp:32:4: error: expected unqualified-id before ‘if’
if ((hh <0)||(hh >23)){
^
prog3.cpp:37:4: error: expected unqualified-id before ‘if’
if ((mm <0)||(mm >0)){
^
prog3.cpp:42:4: error: expected unqualified-id before ‘if’
if ((ss <0)||(ss >0)){
^
prog3.cpp:47:1: error: expected declaration before ‘}’ token
}
^

You have extra braces.

You will find it extremely helpful to
1. Indent your code consistently. Use an editor which does this for you.
2. Type the closing brace at the same type you type the opening brace. Use an editor which does this for you.

Here is a “fixed” version. (there’s still some issues, but it will compile with warnings.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <cstdio>

bool readTime(int &hours, int &minutes, int &seconds);

int main ()
{
  int h,m,s;
  if(readTime(h,m,s)){
    printf("%2d:%2d:%2d" h, m, s);
    return 0;
  }
}

bool readTime(int &hours, int &minutes, int &seconds)
{
  int hh,mm,ss;

  printf("please enter time in format 09:30:50n");
  int count = scanf("%2d:%2d:%2d", &hh, &mm, &ss);

  // did they get the right format?
  if (count !=3){
    printf("Invalid formatn");
    return false;
  }
  // is the number of hours correct?
  if ((hh <0)||(hh >23)){
    printf("Invalid hour %d n", hh);
    return false;
  }
  //is number of minutes wrong?
  if ((mm <0)||(mm >0)){
    printf("Invalid Minutes %d n", mm);
    return false;
  }
  //is number of seconds wrong?
  if ((ss <0)||(ss >0)){ 
    printf("Invalid seconds %d n", mm);
    return false;
  }
}

@mobzzi

i run your program but then i see this issue it will be helpful if i get perfectly running program so that i know for my upcoming exam how to make this type of program run. I am currently preparing for my exam. hope to see some help from you guys out there

In function ‘int main()’:
9:26: error: expected ‘)’ before ‘h’
9:33: warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat=]
In function ‘bool readTime(int&, int&, int&)’:
41:1: warning: control reaches end of non-void function [-Wreturn-type]

This line :
printf("%2d:%2d:%2d" h, m, s);

Should be :
printf("%2d:%2d:%2d", h, m, s); // An extra comma (,)

Last edited on

@sakurasouBusters

thank you for the help but why am i facing problem like this?

progs3.cpp:41:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^

Notice that the compiler provides you with the location of any errors that it encounters.

1
2
In function 'int main()':
9:26: error: expected ')' before 'h'

Line 9, column 26 — usually there’s a filename, too. There’s a missing comma (I missed it, apologies.)

If the problem is a syntax error, the compiler will report the error no earlier than it appears. Look at the location of the first error and then look backwards towards the beginning of the file until you find it.

@mbozzi

thank you for the help its so much appreciated and helpful for my upcoming exam and future programming errors. but can you help me with my issue on line 41?

Topic archived. No new replies allowed.

Error: expected initializer before ‘int’

I can’t figure out whats wrong with my code. When I compile it, the only error I get is “expected initializer before ‘int’ in line 9. I’m still pretty new and uneducated in this, so I’m not too sure what the heck is going on. Any help is appreciated.

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

    int m, d, year, choice, i, passed, orig, yy; 
    char more = 'y';
    int  days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int leap(int x)
int main()
{ 
{
  while (more == 'y' || more == 'Y') {
  	printf ("ntThis program will find days passed or date in the year");
  	printf ("nttt1)  Input date (mm/dd/yyyy) to find dates passed");
  	printf ("nttt2)  Input passed days to find date in the year");
  	printf ("nnttYour choice (1/2): ");
  	scanf  ("%d",&choice);	}
  }
  		char letter (int c)
  		{
  		if (c == 1);{
  	
    int daysInMonth = 0;
    int daysPassed = 0;
    int leapyear = 0;     
    printf("nnnttPlease input date (mm-dd-yyyy): ");
    scanf ("%d/%d/%d", &m, &d, &year);
    if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
            leapyear=1;  
      switch(m-1)  {
        case 12:
          daysInMonth=31;
          daysPassed += daysInMonth;             
        case 11:
          daysInMonth=30;
          daysPassed += daysInMonth;
        case 10:
          daysInMonth=31;
          daysPassed += daysInMonth;
        case 9:
          daysInMonth=30;
          daysPassed += daysInMonth;
        case 8:
          daysInMonth=31;
          daysPassed += daysInMonth;
        case 7:
          daysInMonth=31;
          daysPassed += daysInMonth;
        case 6:
          daysInMonth=30;
          daysPassed += daysInMonth;
        case 5:
          daysInMonth=31;
          daysPassed += daysInMonth;
        case 4:
          daysInMonth=30;
          daysPassed += daysInMonth;
        case 3:
          daysInMonth=31;
          daysPassed += daysInMonth;
        case 2:
          if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
            leapyear=1;
            daysInMonth=29;
            daysPassed += daysInMonth;
          }  
          else{
            daysInMonth=28;
            daysPassed += daysInMonth;
          }    
        case 1:
          daysInMonth=31;
          daysPassed += daysInMonth;
        case 0:  
          if(m <=7 && m % 2 != 0 && d <= 31 || m >= 8 && m % 2 == 0 && d <= 31)   
            daysPassed += d;
          else 
            if(m == 2 && leapyear == 1 && d <= 29 || m == 2 && leapyear == 0 && d <= 28)
              daysPassed += d;
            else
             if(m <=6 && m % 2 == 0 && d <= 30 && m != 2|| m >= 9 && m % 2 != 0 && d <= 30)
               daysPassed += d;
             else{
               printf("ntttYour input mm/dd/yyyy is wrong!");     
               break;        
             }           
        printf("nntttThere are %d days past in the year", daysPassed);          
        break;   
        default: printf("ntttYour input mm/dd/yyyy is wrong!");
    	}
    }
	 	if (c == 2)
{
	{
    if (x % 4 == 0 && x % 100 != 0 || x % 400 == 0)
        return 1;
    else
        return 0;
}


    
    do   {
        printf ("nttInput days: ");
        scanf  ("%d", &passed);
        printf ("ntt      Year: ");
        scanf  ("%d", &yy);
        
        if (leap(yy))
            days[1] = 29;
        else
            days[1] = 28;
        
        orig = passed;
        
        for (i = 0; passed > 0; i++)
            passed = passed - days [i];
        passed = passed + days [i - 1]; 
        
        printf ("nttThe date is %d-%d-%d",
                                            i, passed, yy, orig);
		}
        printf ("nnttDo more (Y/N)? ");
        scanf  ("%s", &more);		}
       }while (more == 'y'  ||  more == 'Y');
   
  return 0;        
}

C language error (2) Expected Initializer Before “Int”

tags: C language learning

Article catalog

  • Problem description
  • 2. Solve
  • Summarize

Problem description

2 CPP files, one .h file, Linux compile error

main.cpp:3:1: error: expected initializer before ‘int’

My main.cpp has no problem, but error is in main.cpp, because the main.cpp contains .h file, .h, the last statement is not added “;” “

2. Solve

Find the included header file to see if you need to join;

Summarize

Pay attention to the code to avoid this error.

Intelligent Recommendation

More Recommendation

Добавить комментарий