w4tchm4n Posted December 13, 2009 at 08:48 PM Report Share #300205 Posted December 13, 2009 at 08:48 PM Boas, Eu tenho aqui um problema com um programa. O programa serve para calcular a Média de dois valores, e se a média for superior a 10 ou 50 (são duas funções), exibe uma mensagem, se não, exibe outra. O meu problema é que não consigo fazer com que, ao premir '1' ou '2', ele chame a respectiva função. Aí segue o código: #include <iostream> using namespace std; bool choice() { cout << "Seleccione o modo que pretende..." << endl; cout << " 1 - Ensino Basico " << endl; cout << " 2 - Ensino Secundario " << endl; char answer; cin >> answer; switch (answer) { case '1': return void ensino_basico(); case '2': return void ensino_secundario(); } } void ensino_basico() { int number; int number2; int media; cout << "Insira a nota do primeiro teste: "; cin >> number; cout << "Insira a nota do segundo teste: "; cin >> number2; media = ( number + number2 ) / 2; cout << "A media do aluno = " << media << endl; if ( media >= 50 ) cout << "O Aluno transitou. " << endl; else cout << "O Aluno reprovou. " << endl; } void ensino_secundario() { int number; int number2; int media; cout << "Insira a nota do primeiro teste: "; cin >> number; cout << "Insira a nota do segundo teste: "; cin >> number2; media = ( number + number2 ) / 2; cout << "A media do aluno = " << media << "\n" << endl; if ( media >= 10) cout << "O Aluno transitou. " << endl; else cout << "O Aluno reprovou. " << endl; } int main() { choice(); system("pause"); return 0; } Obrigado desde já pela ajuda 😉 Cumprimentos. Link to comment Share on other sites More sharing options...
zpgm Posted February 4, 2010 at 12:05 PM Report Share #309603 Posted February 4, 2010 at 12:05 PM Boas, Existem 2 situações distintas: 1ª - Na utilização da expressão switch a sintaxe é switch(option) { case option_value_1 : faz_qualquer_coisa; break; case option_value_2 : faz_outra_coisa; break; ... case option_value_n : faz; break; default: comportamento_por_defeito; break; } 2ª - porque é que a função choice() é do tipo bool? Porque não do tipo void e assim teria a seguinte sintaxe: void choice() { cout << "Seleccione o modo que pretende..." << endl; cout << " 1 - Ensino Basico " << endl; cout << " 2 - Ensino Secundario " << endl; char answer; cin >> answer; switch (answer) { case '1': ensino_basico(); break; case '2': ensino_secundario(); break; } } Bom trabalho, ZP. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now