CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2009
    Posts
    24

    Class output/input methods

    Hi,

    I have following questions and wondering if someone can help.

    1. Why don't class output methods typically print labels and or spacing? Shouldn't the program always outputs?

    2. Why don't class input methods typically disply a prompt for the user? Shouldn't the program always prompt before inputs?

    3. Given a value in the (numeric ) varibale x, show a single cout statement to print a message to the user telling them if x's value is negative, positive or zero.

    Any help would be greatly appreciate.

    Thanks
    Micky

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Class output/input methods

    Quote Originally Posted by micky_577
    1. Why don't class output methods typically print labels and or spacing? Shouldn't the program always outputs?
    A function should do one thing and do it well. So, it may well be that labels and spacing make sense, or it may be that those are unnecessary and thus should be up to the user of the class.

    Quote Originally Posted by micky_577
    2. Why don't class input methods typically disply a prompt for the user? Shouldn't the program always prompt before inputs?
    Consider non-interactive input, e.g., reading from a file.

    Quote Originally Posted by micky_577
    3. Given a value in the (numeric ) varibale x, show a single cout statement to print a message to the user telling them if x's value is negative, positive or zero.
    This is not a question; it is an instruction.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Class output/input methods

    1) Class outputs will output whatever the programmer tells them to output. If outputting spacing is appropriate, it should be coded that way.

    2) What if the input doesn't come from a user? What if errors in input need to be handled in different ways depending on context? What if you need to get the user's input before you set that value in a class?

    3) What's your question?

  4. #4
    Join Date
    Jun 2009
    Posts
    24

    Re: Class output/input methods

    My question is

    How to write a single cout statment that print message telling user that entered number (x) is negative, positive or zero?

    In a way How to write below code in one line?

    If ( x> 0)
    cout <<" Number is positive";
    else if (x<0)
    cout << " Number is negative ";
    else
    cout<< " Number is zero";

    Here is my try

    cout << x>0 ? "Number is positive" : x<0 ? "Number is negative : x==0 ? "NUmber is zero";

    It does not compile. There is syntax error in it. Please somebody help.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Class output/input methods

    Quote Originally Posted by micky_577
    How to write a single cout statment that print message telling user that entered number (x) is negative, positive or zero?

    In a way How to write below code in one line?
    I would not suggest writing it that way. What you have now is readable. If you try and compress it, you would likely have to use the conditional operator twice, resulting in somewhat obfuscated code. That said, the solution is to use parentheses to group, since the conditional operator has a lower precedence than the right shift operator.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Class output/input methods

    Quote Originally Posted by micky_577 View Post
    My question is

    How to write a single cout statment that print message telling user that entered number (x) is negative, positive or zero?

    In a way How to write below code in one line?

    If ( x> 0)
    cout <<" Number is positive";
    else if (x<0)
    cout << " Number is negative ";
    else
    cout<< " Number is zero";

    Here is my try

    cout << x>0 ? "Number is positive" : x<0 ? "Number is negative : x==0 ? "NUmber is zero";

    It does not compile. There is syntax error in it. Please somebody help.
    Readability is much more important than being as concise as you can. Use the if statement.

  7. #7
    Join Date
    Jun 2009
    Posts
    24

    Re: Class output/input methods

    This is question that has been asked in a test. I am not programming. I need to reduce it to one sentence to answer a question.

    Please help.

  8. #8
    Join Date
    Jun 2009
    Posts
    24

    Re: Class output/input methods

    Ok guys I was able to fix syntax error. Here it is.

    cout<<((x>1)?"positive"((x<0)?"Negative":"zero")));

    Thanks for your help.

    Micky

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured