functions in switch statements?

hey guys im trying to activate a function from a switch with errors any ideas?

[steven@localhost cpp]$ c++ ftools.cpp -o ftools
ftools.cpp: In function ‘int main()’:
ftools.cpp:27: error: ‘descent’ was not declared in this scope
ftools.cpp: At global scope:
ftools.cpp:38: error: expected initializer before ‘descent’
[steven@localhost cpp]$

#include <iostream>
#include <cstdlib>

using namespace std;

int main()

{

int input;

cout << "Steve's Flight Tools.\n";
cout << "\n";
cout << "1. Descent Calculator.\n";
cout << "2. Fuel Planner.\n";
cout << "\n";

while (input != 0)
{

cout << "Please enter key for option!\n";
cin >> input;

switch(input)

{
case 1: descent();
break;
case 2: cout << "Fuel Planner. \n";
break;
default:
break;
}
}
return 0;
}

void function descent()
{
int alt;
int speed;

cout << "\n";
cout << "Welcome to the 3nm descent calculator tool\n";
cout << endl;
cout << "Please enter your altitude to loose?\n";
cin >> alt;
cout << "Please Enter your ground speed\n";
cin >> speed;
cout << endl;
cout << "Your descent distance is " << (alt * 3) / 1000 << "nm's\n";
cout << "Your descent speed is " << (speed / 2) * 10 << "fpm.\n";
cout << "\n";
}
void function descent() should be void descent()

Also, you should declare your function before you call it. Putting void descent(); before main will work.
Thanks that worked perfect, why did i have to remove 'function' in my training book it uses it?
also do all functions have to be declared before use?

thanks again :D
willissteve0 wrote:
why did i have to remove 'function' in my training book it uses it?

It does? What exactly does it say?

willissteve0 wrote:
do all functions have to be declared before use?

Yes, you can do it

either like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

void f(); //declaration

int main()
{
    f(); //call

    cout << "hit enter to quit...";
    cin.get();
    return 0;
}

void f() //definition
{
    cout << "Hi! I'm f!" << endl;
}

or like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

void f() //declaration & definition
{
    cout << "Hi! I'm f!" << endl;
}

int main()
{
    f(); //call

    cout << "hit enter to quit...";
    cin.get();
    return 0;
}
Last edited on
brilliant very help full thanks :)
i did double check my training book and you were correct must having confusing myself with javascript :-s
Topic archived. No new replies allowed.