Is it possible?

Is it possible to call a function from a switch statement code example is. {
int main(int argc, char *argv[])
{
cout << "Enter your account number: ";
cin >> account;
switch (account)
{
case 1223229227:
{
void acc1223229227(int account);
break;
}
case 1223224225:
{
//void test();
void acc1223224225(int account);
break;
}
case 1223228229:
{
void acc1223228229(int account);
break;
}
default:
{
void def();
break;
}
}
system("PAUSE");
return EXIT_SUCCESS;
}

Comments

  • : Is it possible to call a function from a switch statement code example is. {
    [code]
    : int main(int argc, char *argv[])
    : {
    : cout << "Enter your account number: ";
    : cin >> account;
    : switch (account)
    : {
    : case 1223229227:
    : {
    : void acc1223229227(int account);
    : break;
    : }
    : case 1223224225:
    : {
    : //void test();
    : void acc1223224225(int account);
    : break;
    : }
    : case 1223228229:
    : {
    : void acc1223228229(int account);
    : break;
    : }
    : default:
    : {
    : void def();
    : break;
    : }
    : }
    : system("PAUSE");
    : return EXIT_SUCCESS;
    : }
    :
    [/code]
    [blue]If you have all these functions: acc1223228229(), acc1223224225(), acc1223229227() - then it is possible. However, if you try to map ANY entered number into a function - not possible. Besides, very strange design - what if you have millions of accounts? You telling me, that you need millions of functions - each for its own account?! That seems like bad design. What is the difference between one account and the other? You should have a structure (or object in C++) describing the account DATA and BEHAVIOUR and then have you system built around that structure.[/blue]
  • : : Is it possible to call a function from a switch statement code example is. {
    : [code]
    : : int main(int argc, char *argv[])
    : : {
    : : cout << "Enter your account number: ";
    : : cin >> account;
    : : switch (account)
    : : {
    : : case 1223229227:
    : : {
    : : void acc1223229227(int account);
    : : break;
    : : }
    : : case 1223224225:
    : : {
    : : //void test();
    : : void acc1223224225(int account);
    : : break;
    : : }
    : : case 1223228229:
    : : {
    : : void acc1223228229(int account);
    : : break;
    : : }
    : : default:
    : : {
    : : void def();
    : : break;
    : : }
    : : }
    : : system("PAUSE");
    : : return EXIT_SUCCESS;
    : : }
    : :
    : [/code]
    : [blue]If you have all these functions: acc1223228229(), acc1223224225(), acc1223229227() - then it is possible. However, if you try to map ANY entered number into a function - not possible. Besides, very strange design - what if you have millions of accounts? You telling me, that you need millions of functions - each for its own account?! That seems like bad design. What is the difference between one account and the other? You should have a structure (or object in C++) describing the account DATA and BEHAVIOUR and then have you system built around that structure.[/blue]
    :


    Also, you aren't calling functions, you are writing function prototypes in the middle of the switch statement, which doesn't make sense.


  • [b][red]This message was edited by SilverDEATHC at 2007-1-23 8:10:5[/red][/b][hr]
    [b][red]This message was edited by SilverDEATHC at 2007-1-23 8:8:30[/red][/b][hr]
    : : : Is it possible to call a function from a switch statement code example is. {
    : : [code]
    : : : int main(int argc, char *argv[])
    : : : {
    : : : cout << "Enter your account number: ";
    : : : cin >> account;
    : : : switch (account)
    : : : {
    : : : case 1223229227:
    : : : {
    : : : void acc1223229227(int account);
    : : : break;
    : : : }
    : : : case 1223224225:
    : : : {
    : : : //void test();
    : : : void acc1223224225(int account);
    : : : break;
    : : : }
    : : : case 1223228229:
    : : : {
    : : : void acc1223228229(int account);
    : : : break;
    : : : }
    : : : default:
    : : : {
    : : : void def();
    : : : break;
    : : : }
    : : : }
    : : : system("PAUSE");
    : : : return EXIT_SUCCESS;
    : : : }
    : : :
    : : [/code]
    : : [blue]If you have all these functions: acc1223228229(), acc1223224225(), acc1223229227() - then it is possible. However, if you try to map ANY entered number into a function - not possible. Besides, very strange design - what if you have millions of accounts? You telling me, that you need millions of functions - each for its own account?! That seems like bad design. What is the difference between one account and the other? You should have a structure (or object in C++) describing the account DATA and BEHAVIOUR and then have you system built around that structure.[/blue]
    : :
    :
    :
    : Also, you aren't calling functions, you are writing function prototypes in the middle of the switch statement, which doesn't make sense.
    :
    :
    :
    I am trying to call the functions I am going by my code book and what my compiler allows.
    Let me clarify I am just trying to create an account operator for a mock power company and just wonder if I need a header file I will copy my code here:
    [code]
    #include
    #include
    #include
    #include
    #include
    #include

    using namespace std;
    void acc1223229227(int account);
    void acc1223224225(int account);
    void acc1223228229(int account);
    void test();
    void def();
    int account;
    ifstream bill;
    ofstream billout;
    void test()
    {
    cout << "This is a test.";
    }

    int main(int argc, char *argv[])
    {
    cout << "Enter your account number: ";
    cin >> account;
    switch (account)
    {
    case 1223229227:
    {
    void acc1223229227(int account);
    break;
    }
    case 1223224225:
    {
    //void test();
    acc1223224225(int account);
    break;
    }
    case 1223228229:
    {
    void acc1223228229(int account);
    break;
    }
    default:
    {
    void def();
    break;
    }
    }
    system("PAUSE");
    return EXIT_SUCCESS;
    }
    void acc1223229227(int account)
    {
    string name;
    int powerusg;
    double kwhr;
    bill.open("1223229227.dat");
    billout.open("1223229227.dat");
    getline(bill, name);
    bill >> account;
    bill >> powerusg;
    if (powerusg <= 200)//code for data manipulation
    {
    kwhr = .11;
    }
    else
    {
    kwhr = .08;
    }
    cout << "You Owe EZ Watt Power Company $" << kwhr * powerusg << ".00";
    cout << endl << "EZ Watt is charging you "<< kwhr << " cents per hour of useage." << endl;
    bill.close();
    billout.close();

    }
    void acc1223224225(int account)
    {
    string name;
    int powerusg;
    double kwhr;
    bill.open("1223224225.dat");
    billout.open("1223224225.dat");
    getline(bill, name);
    bill >> account;
    bill >> powerusg;
    if (powerusg <= 200)//code for data manipulation
    {
    kwhr = .11;
    }
    else
    {
    kwhr = .08;
    }
    cout << "You Owe EZ Watt Power Company $" << kwhr * powerusg << ".00";
    cout << endl << "EZ Watt is charging you "<< kwhr << " cents per hour of useage." << endl;
    bill.close();
    billout.close();

    }
    void acc1223228229(int account)
    {
    string name;
    int powerusg;
    double kwhr;
    bill.open("1223228229.dat");
    billout.open("1223228229.dat");
    getline(bill, name);
    bill >> account;
    bill >> powerusg;
    if (powerusg <= 200)//code for data manipulation
    {
    kwhr = .11;
    }
    else
    {
    kwhr = .08;
    }
    cout << "You Owe EZ Watt Power Company $" << kwhr * powerusg << ".00";
    cout << endl << "EZ Watt is charging you "<< kwhr << " cents per hour of useage." << endl;
    bill.close();
    billout.close();

    }
    void def()
    {
    cout << "NOT KNOWN ACCOUNT NUMBERa" << endl;
    }

    [/code]

    Thank you for all the help if you are able to help me.




  • You didn't read my post.

    Anyway, why do you need functions at all? That program is doing the same thing in several places. Why not make things easy and logical instead:

    [code]
    #include
    #include
    #include

    using namespace std;



    int main()
    {
    string account;
    string filename;

    ifstream bill_in;
    ofstream bill_out;


    cout << "Enter your account number: ";
    cin >> account;

    filename = account + ".dat";

    bill_in.open(filename.c_str());

    // ...and so on...




    return 0;
    }
    [/code]
  • : You didn't read my post.
    :
    : Anyway, why do you need functions at all? That program is doing the same thing in several places. Why not make things easy and logical instead:
    :
    : [code]
    : #include
    : #include
    : #include
    :
    : using namespace std;
    :
    :
    :
    : int main()
    : {
    : string account;
    : string filename;
    :
    : ifstream bill_in;
    : ofstream bill_out;
    :
    :
    : cout << "Enter your account number: ";
    : cin >> account;
    :
    : filename = account + ".dat";
    :
    : bill_in.open(filename.c_str());
    :
    : // ...and so on...
    :
    :
    :
    :
    : return 0;
    : }
    : [/code]
    :
    Thank you I was just trying to do stuff so Thank you.
  • : : You didn't read my post.
    : :
    : : Anyway, why do you need functions at all? That program is doing the same thing in several places. Why not make things easy and logical instead:
    : :
    : : [code]
    : : #include
    : : #include
    : : #include
    : :
    : : using namespace std;
    : :
    : :
    : :
    : : int main()
    : : {
    : : string account;
    : : string filename;
    : :
    : : ifstream bill_in;
    : : ofstream bill_out;
    : :
    : :
    : : cout << "Enter your account number: ";
    : : cin >> account;
    : :
    : : filename = account + ".dat";
    : :
    : : bill_in.open(filename.c_str());
    : :
    : : // ...and so on...
    : :
    : :
    : :
    : :
    : : return 0;
    : : }
    : : [/code]
    : :
    : Thank you I was just trying to do stuff so Thank you.
    :
    [blue]
    I recommend taking a look at std::vector for storing your accounts.
    [/blue]
  • : : : You didn't read my post.
    : : :
    : : : Anyway, why do you need functions at all? That program is doing the same thing in several places. Why not make things easy and logical instead:
    : : :
    : : : [code]
    : : : #include
    : : : #include
    : : : #include
    : : :
    : : : using namespace std;
    : : :
    : : :
    : : :
    : : : int main()
    : : : {
    : : : string account;
    : : : string filename;
    : : :
    : : : ifstream bill_in;
    : : : ofstream bill_out;
    : : :
    : : :
    : : : cout << "Enter your account number: ";
    : : : cin >> account;
    : : :
    : : : filename = account + ".dat";
    : : :
    : : : bill_in.open(filename.c_str());
    : : :
    : : : // ...and so on...
    : : :
    : : :
    : : :
    : : :
    : : : return 0;
    : : : }
    : : : [/code]
    : : :
    : : Thank you I was just trying to do stuff so Thank you.
    : :
    : [blue]
    : I recommend taking a look at std::vector for storing your accounts.
    : [/blue]
    :
    [green] would you explain what std::vector is? [/green]
  • [b][red]This message was edited by gautam at 2007-1-29 13:7:38[/red][/b][hr]
    : [green] would you explain what std::vector is? [/green]
    :

    It is a standard template type which can hold a set of classes for you. I would suggest you learn c++ properly before going into stl. However I will still give an example of how to use it.

    if you have a account class like the following

    [code]
    class Account
    {
    private :
    int id;
    std::string name;
    ....
    .....

    };
    [/code]

    You can store this in a std::vector like this

    [code]
    std::vector MyAccount;

    // And access something like this - if it has information
    Account a = MyAccount[2];
    [/code]

    Now you can access it as an array because std::vector is contiguous. This will give you constant time access.

    However first learn c++ well and this will all make sense in due course.




  • : [b][red]This message was edited by gautam at 2007-1-29 13:7:38[/red][/b][hr]
    : : [green] would you explain what std::vector is? [/green]
    : :
    :
    : It is a standard template type which can hold a set of classes for you. I would suggest you learn c++ properly before going into stl. However I will still give an example of how to use it.
    :
    : if you have a account class like the following
    :
    : [code]
    : class Account
    : {
    : private :
    : int id;
    : std::string name;
    : ....
    : .....
    :
    : };
    : [/code]
    :
    : You can store this in a std::vector like this
    :
    : [code]
    : std::vector MyAccount;
    :
    : // And access something like this - if it has information
    : Account a = MyAccount[2];
    : [/code]
    :
    : Now you can access it as an array because std::vector is contiguous. This will give you constant time access.
    :
    : However first learn c++ well and this will all make sense in due course.
    :
    :
    :
    :
    :
    [green]I am learning C++ but I am not that far yet. Thank you.[/green]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories