Welcome to Dream.In.Code
Become a C++ Expert!

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!




functions using switch statement...its not the complete program only c

 
Reply to this topicStart new topic

functions using switch statement...its not the complete program only c, the program has a menu...the user has to select his\her choice...

virgofreak
19 Dec, 2007 - 08:37 AM
Post #1

New D.I.C Head
*

Joined: 19 Dec, 2007
Posts: 5


My Contributions
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 crazy.gif !!!!

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. code.gif

This post has been edited by Martyr2: 19 Dec, 2007 - 09:22 AM
User is offlineProfile CardPM
+Quote Post

Jingle
RE: Functions Using Switch Statement...its Not The Complete Program Only C
19 Dec, 2007 - 09:29 AM
Post #2

D.I.C Regular
***

Joined: 20 Oct, 2007
Posts: 250


My Contributions
void main()
{
char ch;
int opt(char &, int &, int &);

you call opt a void then you call it an int you cant do that
if you want it to return a value call it an int from the start.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Functions Using Switch Statement...its Not The Complete Program Only C
19 Dec, 2007 - 09:51 AM
Post #3

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
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;

    cout << "FIBONACCI SERIES.......................1" << endl;

    cout << "SUM OF THE DIGITS ENTERED..............2" << endl;

    cout << "ASCII CODE OF THE ENTERED CHARACTER....3" << endl;

    cout << "EXIT...................................4" << 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!" decap.gif
User is offlineProfile CardPM
+Quote Post

virgofreak
RE: Functions Using Switch Statement...its Not The Complete Program Only C
8 Jan, 2008 - 06:45 AM
Post #4

New D.I.C Head
*

Joined: 19 Dec, 2007
Posts: 5


My Contributions
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!! smile.gif

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
User is offlineProfile CardPM
+Quote Post

virgofreak
RE: Functions Using Switch Statement...its Not The Complete Program Only C
8 Jan, 2008 - 06:58 AM
Post #5

New D.I.C Head
*

Joined: 19 Dec, 2007
Posts: 5


My Contributions
thanks a billion for ur help....it worked .....!!!!!
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Functions Using Switch Statement...its Not The Complete Program Only C
8 Jan, 2008 - 07:00 AM
Post #6

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,143



Thanked: 77 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
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.

Also:

CODE

                    if(p=='n')
                        break;
                    else
                        continue;
                   }
                   opt1(2,a,i);
                   break;


I would use the following:
CODE

                    if(p!='n') {
                        continue;
                   }
                   opt1(2,a,i);
                   break;

so that way you only have one break.

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
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 03:28PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month