Join 149,933 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,705 people online right now. Registration is fast and FREE... Join Now!
the program has a menu...the user has to select his\her choice...n da computer does it...i dun know how to put the function to use...helpppppp !!!!
CODE
# include <iostream.h> # include <conio.h> # include <process.h> void main() { char ch; void opt(char &, int &, int &); clrscr(); gotoxy (29,02); cout<<"M A I N M E N U "; gotoxy (20,04); cout<<"FIBONACCI SERIES.......................1"; gotoxy (20,05); cout<<"SUM OF THE DIGITS ENTERED..............2"; gotoxy (20,06); cout<<"ASCII CODE OF THE ENTERED CHARACTER....3"; gotoxy (20,07); cout<<"EXIT...................................4"; cout<<"\n\n\n\nEnter yout choice...."; ch=getche(); cout<<"\nThe entered choice is......"; putch(ch); getch(); switch (ch) { case 1: clrscr(); int z,f; cout<<"\nFIBONACCI SERIES..."; cout<<"\nEnter an integer upto which the series is to be displayed..."; cin>>z; int y=opt(1,z,f); cout<<"\nThe series is..."<< f; break; } } int opt (char &ch,int &x,int &f) {
switch (ch) { case 1: clrscr(); int f1=0, f2=1, f; cout<< f1 << f2; while (f<=x) { cout<< f; f1=f2, f2=f; f=f1+f2; } } return f; }
*edit: In the future use code tags, please and thank you.
This post has been edited by Martyr2: 19 Dec, 2007 - 09:22 AM
well you had many things wrong with this and hopefully the changes I have made for you will help push you in the right direction. First of all, I took out all the statements which are not exactly standard to C++. Next I had to pull out your declaration and put it at the top then modify it to match the signature of your function you defined below (you had it as void when it should have been int to match). I also made the first two parameters const integers passed by value. You don't and shouldn't pass by reference on these since they are not modified in any way during the execution of opt().
I also made a few modifications in opt so that your variables will have proper scope, initialized "f" before passing it to opt() otherwise it would have a null and not work. Modified a few of your statements to now include endline statements. Broke the program a little to show it a little better. Plus a few other things.
The code is below...
CODE
#include <iostream> #include <conio.h> #include <process.h> using namespace std;
// Notice declaration outside of main and matches signature of function below. // It needs to return int to match what you defined. // The first two parameters don't need to be passed by reference since they are not modified.
int opt(const int, const int, int &);
void main() { char ch;
// Main menu items display cout << "M A I N M E N U " << endl;
// Get the user's input and show them their choice. cout << "\n\n\n\nEnter yout choice...."; ch = getche();
cout << "\nThe entered choice is......" << ch;
switch (ch) { case '1': int z,f = 0;
// Prompt for the number to show the series up to cout << "\nFIBONACCI SERIES..."; cout << "\nEnter an integer upto which the series is to be displayed..."; cin >> z;
// Show the series and then opt will return the next value in the series int y = opt(1,z,f);
cout << "\nNext in the series is..." << y << endl; break; } }
// Displays the fibonacci series and returns the next value in the series. int opt (const int ch, const int x, int &f) {
int f1=0, f2=1;
switch (ch) { case 1:
while (f<=x) { cout << f << " "; f1=f2, f2=f; f=f1+f2; } }
return f; }
So read some of the in code comments I put and take a look at the changes. You should see the result as a list of the fibonacci series and then the next value in the sequence returned. I hope this makes some sense and enjoy!
"At DIC we be fibonacci series wielding code ninjas!"
i am sory last tym i cudnt post da whole program......so here it goes.....i cleared all my errors but still ......the output seems to be wrong!!! plus i cannot cum out of the for loop in "case 2".....plzzzz tell me wats wrong wit my program....help me out as soon as possible ...thnx a lottt in advance!!
CODE
# include <iostream.h> # include <conio.h> # include <process.h> void main() { char ch; int opt (const int,const int, int ); void opt1 (const int,int[],int); void opt2 (const int, int); clrscr(); l1: gotoxy (29,02); cout<<"M A I N M E N U "<<endl; gotoxy (20,04); cout<<"FIBONACCI SERIES.......................1"<<endl; gotoxy (20,05); cout<<"SUM OF THE DIGITS ENTERED..............2"<<endl; gotoxy (20,06); cout<<"ASCII CODE OF THE ENTERED CHARACTER....3"<<endl; gotoxy (20,07); cout<<"EXIT...................................4"<<endl; cout<<"\n\n\n\nEnter your choice...."; ch=getche(); cout<<"\nThe entered choice is......"; putch(ch); getch(); switch (ch) { case '1': clrscr(); int z,f=0; cout<<"\n\tFIBONACCI SERIES"<<endl; cout<<"\nEnter an integer upto which the series is to be displayed..."<<endl; cin>>z; int y=opt(1,z,f); cout<<"\nNext in the series is..."<< y; break;
case '2': clrscr(); int a[50],i; char p; for (i=0; i<50;i++) { cout<<"\nEnter a number..."; cin>>a[i]; cout<<"\nDo you want to continue...(y/n)"; cin>>p; if(p=='n') break; else continue; } opt1(2,a,i); break;
case '3': clrscr(); char c; cout<<"\nEnter a character..."; cin>>c; opt2(3,c); break;
case '4': exit (0); goto l1;
default: cout<<"\n\n\t\tW R O N G C H O I C E !!!!!"; break; } }
int opt (const int ch,const int x,int f) { int f1=0, f2=1; switch (ch) { case 1: while (f<=x) { cout<<f<<"\t"; f1=f2, f2=f; f=f1+f2; } } return f; }
void opt1 (const int ch, int b[], int j) { int s1=0; switch (ch) { case 2: clrscr(); for (int i=0;i<=j;i++) { s1=s1+b[i]; } cout<<"\nThe sum is....."<<s1; } } void opt2 (const int,int ch) { switch(ch) { case 3: cout<< "\nThe ASCII code is..."<<ch; } }
This post has been edited by virgofreak: 8 Jan, 2008 - 07:11 AM
Why are you declaring variables inside of the case?
CODE
case '1': clrscr(); int z,f=0;
I would declare everything outside of the case statements. When you try to reference these variables, if you have not hit case '1', then they will not exist.
Another point... I would strongly suggesting avoid from using the goto command. If you want to use goto commands, code in VB. C/C++ allows you the ability to create functions. Use them instead.
This post has been edited by no2pencil: 8 Jan, 2008 - 07:01 AM