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:50\n");
int count = scanf("%2d:%2d:%2d", &hh, &mm, &ss);
}
// did they get the right format?
if (count !=3){
printf("Invalid format\n");
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:50\n");
  int count = scanf("%2d:%2d:%2d", &hh, &mm, &ss);

  // did they get the right format?
  if (count !=3){
    printf("Invalid format\n");
    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.