Thread: Approach

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    385

    Approach

    I wonder how a statement can be assignd/ replaced with one word.
    For the example below. How is it possible to assign:
    if (Value1 < Value2)..... then ...... statement = true; ?

    Then when I refer to the word &#180;statement&#180; then I actually refer to
    if (Value1 < Value2).
    What approch could be used for this ?

    Code:
    int Value1 = 100;
    int Value2 = 200;
    
    
    if (Value1 < Value2) ...

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Sound easy.

    (Value1 < Value2) will evaluate to true or false. Bools and Integers can hold true or false.

    bool result ;
    result = Value1 < Value2

    if (result) ...
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I am trying this code but somthing is wrong. I cant figure out what it could be.

    Code:
    int Value1 = 100;
    int Value2 = 200;
    int Value3 = 0;
    
    bool result;
    
    result = Value1 < Value2
    
    if (result) 
    { 
    	Value3 = 10; 
    }
    Last edited by Coding; 02-06-2008 at 10:33 PM.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Coding View Post
    I am trying this code but somthing is wrong. I cant figure out what it could be.
    Missing semicolon.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Coding View Post
    I am trying this code but somthing is wrong. I cant figure it out what it could be.

    Code:
    int Value1 = 100;
    int Value2 = 200;
    int Value3 = 0;
    
    bool result;
    
    result = Value1 < Value2;
    
    if (result) 
    { 
    	Value3 = 10; 
    }
    Listen to the errors your compiler reports.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    This doesn&#180;t work either. My whole code look like this. It seems strange. It looks right.

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <sstream> 
    #include <string>  
    #include <vector>
    
    
    int Value1 = 100;
    int Value2 = 200;
    int Value3 = 0;
    
    bool result;
    
    result = Value1 < Value2;
    
    if (result) 
    { 
    	Value3 = 10; 
    }

    My compiler says:

    see declaration of 'result'
    syntax error : 'if'
    syntax error : missing ';' before '{'
    'result' : redefinition; different basic types
    '{' : missing function header (old-style formal list?)

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't run code outside of a function. Put all that in main.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Coding View Post
    My compiler says:

    syntax error : missing ';' before '{'
    See, it told you right there. Read the messages.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Yes ofcourse. I forgot the int main()... Not the missin ; before {

    Thanks

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Another way to do this is to have a function return the result of the expression, like this:

    if ( result() ) ... ;

    bool result() { return Value1 < Value ; }
    Mainframe assembler programmer by trade. C coder when I can.

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If value three would be something else otherwise, you could also use the ternary operator, along with all the other logical alternatives.

    Code:
    int main ( )
    {
        int foo, bar, quz;
        foo = 100;
        bar = 200;
    
        quz = foo < bar ? 10 : -10;
    }
    But you know, C++ programming isn't necessarily about how much you can cram into an expression, but what is the most expressive or easiest to understand way that works. What you initially wrote would have been fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unsure on how to approach this concurrency problem
    By osiris^ in forum C# Programming
    Replies: 3
    Last Post: 04-29-2008, 11:47 PM
  2. Best Approach for Learning
    By UCnLA in forum C Programming
    Replies: 5
    Last Post: 03-21-2008, 02:35 AM
  3. Best approach
    By DV64h in forum Networking/Device Communication
    Replies: 6
    Last Post: 12-10-2006, 03:24 PM
  4. Angle of approach for C++
    By mepaco in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-13-2002, 07:08 PM
  5. software development approach...
    By dkt in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-16-2001, 09:15 PM