Approach

This is a discussion on Approach within the C++ Programming forums, part of the General Programming Boards category; I wonder how a statement can be assignd/ replaced with one word. For the example below. How is it possible ...

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

    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
    Katy, Texas
    Posts
    2,309
    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) ...
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    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 09:33 PM.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,237
    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,185
    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
    383
    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,185
    You can't run code outside of a function. Put all that in main.

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,237
    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
    383
    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
    Katy, Texas
    Posts
    2,309
    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 ; }
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    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, 02: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, 06: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

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