Example program from the textbook does not compile

I typed this program exactly as it is in the textbook by: Robert Lafore
it gives the following errors:
C:\Dev-Cpp\Functions\beeptest.cpp In function 'int main()':
8 10 C:\Dev-Cpp\Functions\beeptest.cpp [Error] 'twobeep' was not declared in this scope

10 9 C:\Dev-Cpp\Functions\beeptest.cpp [Error] 'getche' was not declared in this scope

C:\Dev-Cpp\Functions\beeptest.cpp At global scope:

16 10 C:\Dev-Cpp\Functions\beeptest.cpp [Error] ISO C++ forbids declaration of 'twobeep' with no type [-fpermissive]

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

//tests the twobeep function
#include<iostream>
#include<cstdio>
using namespace std;
 
int main(){
	twobeep();
	cout<<"Type any character: " ;
	getche();
	twobeep();
}

// twobeep()
// beeps the speaker twice
 twobeep()
{
	int k;
	cout<< "\x7" ;                //first beep
	for ( k = 1; k <5000; k++)    // delay
	      ;                       // (null statement
	cout << "\x7" ;               //second beep
}

  
16 10 C:\Dev-Cpp\Functions\beeptest.cpp [Error] ISO C++ forbids declaration of 'twobeep' with no type [-fpermissive]

The function needs a return type. If it doesn't return any value the return type should be void.

1
2
3
4
void twobeep()
{
	...
}


8 10 C:\Dev-Cpp\Functions\beeptest.cpp [Error] 'twobeep' was not declared in this scope

The function needs to be declare before you can use it. You either have to define the twobeep function above main, or you could just declare it above main and leave the definition below main.

1
2
3
4
5
6
7
8
9
10
11
12
void twobeep();

int main()
{
	twobeep();
	...
}

void twobeep()
{
	...
}


10 9 C:\Dev-Cpp\Functions\beeptest.cpp [Error] 'getche' was not declared in this scope

getche is a non-standard function that came with some old compilers. You could try including conio.h but if that doesn't work you'll have to use something else.

1
2
3
4
5
6
7
int main()
{
	twobeep();
	cout << "Press enter to continue!";
	cin.ignore(100, '\n');
	twobeep();
}
Last edited on
Topic archived. No new replies allowed.