The if statement.

This is a discussion on The if statement. within the C Programming forums, part of the General Programming Boards category; Can Someone please help me write this? Write an if statement which will increment a variable 'count' if and only ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    32

    The if statement.

    Can Someone please help me write this?

    Write an if statement which will increment a variable 'count' if and only if the input character 'ch' has a value of 3 or 7. NOTE: test for '3' first, then test for '7'

    I have

    Code:
    if (ch == 3 || ch ==7)
    ch + count;
    Also

    Consider the code fragment below. After the fragment executes, what is the value of 'z'?

    Code:
    int i = 5;
    float z, y = 3;
    z = y / i;
    Is this what we call a "garbage value"
    Last edited by alex1067; 03-16-2008 at 09:34 PM. Reason: Fixed tags.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,773
    ++count increments the variable named count. ch + count adds ch and count.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by alex1067 View Post
    Consider the code fragment below. After the fragment executes, what is the value of 'z'?

    Code:
    int i = 5;
    float z, y = 3;
    z = y / i;
    Is this what we call a "garbage value"
    z = 0.6
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    32
    Apparently, this code I am trying is still incorrect.

    Code:
     
    if (ch == 3 || ch ==7)
    ++count;
    Laserlight, how did you know to use pre increment instead of post increment

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by alex1067 View Post
    Code:
     
    if (ch == 3 || ch ==7)
    ++count;
    Laserlight, how did you know to use pre increment instead of post increment
    Here it makes no difference.

    Quote Originally Posted by alex1067 View Post
    Apparently, this code I am trying is still incorrect.
    Perhaps you need to compare to '3' and '7'?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by alex1067 View Post
    Apparently, this code I am trying is still incorrect.

    Code:
     
    if (ch == 3 || ch ==7)
    ++count;
    Laserlight, how did you know to use pre increment instead of post increment
    There isn't much of a difference between "add 1, then do nothing" and "do nothing, then add 1". When it's by itself on a line, it's not going to matter.

    Why do you think your code is incorrect?

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    32
    It is incorrect because I have to put in characters by pulling down on a tap on website and it has 22 spaces and that code is too short, when I pull down any tab, I have theses choices, it is a matter of putting them in order.

    'if' '(' ')' 'ch' '3' '7' '|' '&' '1' 'count' '+' ':' '&'

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Taking Dave's suggestion, I get to 21; maybe that's close enough, or you can use an extra semicolon at the end.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    32
    You mean comparing 3 and 7?

    How would I go about doing that?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by alex1067 View Post
    You mean comparing 3 and 7?

    How would I go about doing that?
    Quote Originally Posted by Dave_Sinkula View Post

    Perhaps you need to compare to '3' and '7'?
    instead of comparing to 3 and 7 as numbers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 06:18 AM
  4. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 09:56 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 03:49 PM

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