-
July 26th, 2009, 07:34 PM
#1
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
-
July 27th, 2009, 12:36 AM
#2
Re: Class output/input methods
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.
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.
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.
-
July 27th, 2009, 07:29 AM
#3
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?
-
July 27th, 2009, 08:12 PM
#4
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.
-
July 27th, 2009, 08:20 PM
#5
Re: Class output/input methods
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.
-
July 27th, 2009, 08:27 PM
#6
Re: Class output/input methods
Originally Posted by micky_577
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.
-
July 27th, 2009, 08:31 PM
#7
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.
-
July 27th, 2009, 08:41 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|