need a little help with some Functions.

This is a discussion on need a little help with some Functions. within the C++ Programming forums, part of the General Programming Boards category; i am trying to make a program using functions where i want a input,output,time,and charge all to be functions. the ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    25

    Question need a little help with some Functions.

    i am trying to make a program using functions
    where i want a input,output,time,and charge all to be functions.
    the problem i am having is with putting error checking into the function.
    for example say i have this function below
    void input (char,int,int)
    and i want it to read in 2 times ( the ints) and a char from one of the following, but not output anything until the end of the program.
    " choose one of the following "
    " a is for apple "
    "p is for pineapple"
    say the user enters a K, which isnt valid.....how could i use a switch statement for this?
    would it be something like this?
    case a:
    case A: (some statement here?)
    break;
    case p:
    case P: (some statement here?)
    break;
    default: "Invaild data has been entered"

    any ideas? thanks in advance........

  2. #2
    William
    Guest
    I don't quite get your problem. Is it with detecting errors, or is it handling errors. The detecing part can be done with a switch (for example). As for handling you can have your fucntion return an error code. If it returns 0 then all went OK, if it returns 1 then soemthnig went wrong... The other path is to use exceptions. For this you'll need to use the try, throw, catch stuff. Exceptions is a little more complicated, but is way better in the long.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    25

    i guess just detecting.....i have no idea how to attempt the execptions yet.

    i guess jsut handle them an tell the user that invaild data was entered.
    say i used ch for the char i know this much
    switch(ch)
    case a:
    case A: (what should i put here? since i dont want it to output anything in this function)
    could this work
    "case A: choice = apples"
    just assigning it to something? an using the defualt for the error checking?
    also with the function that figures the cost would using an if statement like this,
    say 1-3are .75, 4-10 are .5, and more than 10 are .25
    if (amount <=3)
    {cost = .75*amount;
    }
    else if (amount>=4||amount<=10)
    {cost =.50*amount;
    }
    else if (amount>10)
    {cost =.25*amount;
    }
    an then make a different function to deal with pineapples.? or is there a better way to figure the cost?

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    when will you people learn exception handling is not error handling. An error in input is something that can be foreseen and dealt with locally. This is a common example of an error.An exception is similar to an error but its not. Its quite hard to put into words when you should use error handling and when to use exception handling.You should use exceptions to cover something exceptional going wrong such as new not being able to supply you with the memory you asked for.For things that can be easily foreseen such as input error or not being able to open a file etc. simple error handling is enough.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    I think what he's trying to say is that exceptions are for errors made by the program (runtime bugs) and regular error handling is used for errors from outside sources (ie a person). if not, I am

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21