| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi.
I'm a newbie when it comes to programming...I'm trying to write my first program that contains functions that I write. I really need some help. I'm getting the error "syntax error before `)' token" every where I try to call a function...I'm not even sure if how I'm writing the functions is correct. Any help is much appreciated. #include <iostream.h> #include <stdlib.h> #include <stdio.h> float calc_peso(float); float calc_yen(float); float calc_euro(float); int get_choice(int); int main() { int amount, i; int selection; char answer = 'y'; float result; selection = get_choice(int); for( ;selection != 4; ) { if(selection == 1) result = calc_peso(float); else if(selection == 2) result = calc_yen(float); else selection = calc_euro(float); } system("PAUSE"); } int get_choice(); { int choice; cout<<"Please make a selection to obtain conversion:\n Enter 1 to convert to" <<" peso.\n Enter 2 to convert to Yen.\n Enter 3 to convert to Euro.\n" "Enter 4 to quit."<<endl; cin >>selection; return choice; } float calc_peso(float x) { float result; for(i = 0; answer == 'y'; ++i) { cout <<"Please enter a US dollar amout to convert : "<<endl; cin >>amount; result = x * 0.08822; cout <<result<<endl; cout <<"Would you like to enter another?"<<endl; cin >>answer; } return result; } float calc_yen(float x) { float result; for(i = 0; answer == 'y'; ++i) { cout <<"Please enter a US dollar amout to convert : "<<endl; cin >>amount; result = x * 105.26; cout <<result<<endl; cout <<"Would you like to enter another?"<<endl; cin >>answer; } return result; } |
|
#2
|
|||
|
|||
|
I have pointed out the errors in your code.
#include <iostream.h> #include <stdlib.h> #include <stdio.h> float calc_peso(float); float calc_yen(float); float calc_euro(float); int get_choice(int); int main() { int amount, i; int selection; char answer = 'y'; float result; // Error1: You are passing a built in type as the argument for the function. You need to pass in a variable such as, selection = get_choice( amount ); or selection = get_choice( 10 ); selection = get_choice(int); // I have no clue what arguments you plan on passing to the calc_peso, calc_yen, calc_euro functions. for( ;selection != 4; ) { if(selection == 1) // Same as Error1 result = calc_peso(float); else if(selection == 2) // Same as Error1 result = calc_yen(float); else // Same as Error1 selection = calc_euro(float); } system("PAUSE"); } // Error2: When you declared this function up top it had an int parameter what happened to it here? int get_choice(); { int choice; cout<<"Please make a selection to obtain conversion:\n Enter 1 to convert to" <<" peso.\n Enter 2 to convert to Yen.\n Enter 3 to convert to Euro.\n" "Enter 4 to quit."<<endl; cin >>selection; return choice; } float calc_peso(float x) { float result; for(i = 0; answer == 'y'; ++i) { cout <<"Please enter a US dollar amout to convert : "<<endl; cin >>amount; result = x * 0.08822; cout <<result<<endl; cout <<"Would you like to enter another?"<<endl; cin >>answer; } return result; } float calc_yen(float x) { float result; for(i = 0; answer == 'y'; ++i) { cout <<"Please enter a US dollar amout to convert : "<<endl; cin >>amount; result = x * 105.26; cout <<result<<endl; cout <<"Would you like to enter another?"<<endl; cin >>answer; } return result; }[/QUOTE] You are missing a function definition for calc_euro. Also your function definitions have been placed inside the main function. Your function declarations go at the top and the function definitions go below. However placing them inside main will not generate an error this is poor style. You should try googlin for some beginner tutorials on c++. |
|
#3
|
|||
|
|||
|
Thanks so much for the input... I have updated my code a little. Maybe now it makes
a little more sence or maybe not..hehe. Well at least it runs now. The only thing is that it does not drop out of the loop when its suppose to. I will do some tutorials. #include <iostream.h> #include <stdlib.h> #include <stdio.h> float calc_peso(float); float calc_yen(float); float calc_euro(float); int get_choice(); int main() { //set up variables int amount, i; int selection; float result; float p = 0.08822, y = 105.26, e = 1.321; selection = get_choice(); //select what to convert us dollars to for( ;selection != 4; ) { if(selection == 1) result = calc_peso(p); else if(selection == 2) result = calc_yen(y); else selection = calc_euro(e); } system("PAUSE"); }//end of main int get_choice() { int choice; cout <<"Please make a selection to obtain conversion:\n Enter 1 to convert to" <<" peso.\n Enter 2 to convert to Yen.\n Enter 3 to convert to Euro.\n" "Enter 4 to quit."<<endl; cin >>choice; return choice; } float calc_peso(float p) { float result, amount; int i; char choice; for(;choice = 'y' ![]() { cout <<"Please enter a US dollar amount to convert : "<<endl; cin >>amount; result = amount * p; cout <<result<<endl; cout <<"Would you like to enter another? y or n"<<endl; cin >> choice; } return result; } float calc_yen(float y) { float result, amount; int i; char answer = 'y'; for(i = 0; answer == 'y'; ++i) { cout <<"Please enter a US dollar amout to convert : "<<endl; cin >>amount; result = amount * y; cout <<result<<endl; cout <<"Would you like to enter another?"<<endl; cin >>answer; } return result; } float calc_euro(float e) { float result, amount; int i; char answer = 'y'; for(i = 0; answer == 'y'; ++i) { cout <<"Please enter a US dollar amout to convert : "<<endl; cin >>amount; result = amount * e; cout <<result<<endl; cout <<"Would you like to enter another?"<<endl; cin >>answer; } return result; } |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Function help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|