Does this and this have differences?

This is a discussion on Does this and this have differences? within the C++ Programming forums, part of the General Programming Boards category; Does this code: Code: //This programme shows an example of if...else statement #include <iostream> #include <string> int main() { using ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Location
    Malaysia
    Posts
    13

    Does this and this have differences?

    Does this code:

    Code:
    //This programme shows an example of if...else statement
    #include <iostream>
    #include <string>
    
    int main()
    {
        using namespace std;
    
        string response;
    
        cout<< "What is your favorite programming language? ";
        cin>> response;
        if (response == "C++")
        cout<< "You have a great taste. "<< response << " is a great language.";
        else
        cout<< "It's not as good as C++, but "<< response<< " is a great programming language too.";
    
        return 0;
    }
    have any difference with this code:

    Code:
    //This programme shows an example of if...else statement
    #include <iostream>
    #include <string>
    
    int main()
    {
        using namespace std;
    
        string response;
    
        cout<< "What is your favorite programming language? ";
        cin>> response;
        if (response == "C++")
        cout<< "You have a great taste. C++ is a great language.";
        else
        cout<< "It's not as good as C++, but "<< response<< " is a great programming language too.";
    
        return 0;
    }
    If no, which is better?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    How about this? Now you can change the specific language easily. I added a newline at the end.
    Code:
    //This programme shows an example of if...else statement
    #include <iostream>
    #include <string>
    
    int main()
    {
        using namespace std;
    
        const string bestresponse = "C++";
        string response;
    
        cout<< "What is your favorite programming language? ";
        cin>> response;
        if (response == bestresponse)
        cout<< "You have a great taste. " << bestresponse << " is a great language.";
        else
        cout<< "It's not as good as " << bestresponse << ", but "<< response<< " is a great programming language too.";
        cout << '\n';
    
        return 0;
    }
    Last edited by robatino; 01-13-2008 at 11:21 PM. Reason: fixed newline

  3. #3
    Registered User
    Join Date
    Jan 2008
    Location
    Malaysia
    Posts
    13
    Quote Originally Posted by robatino View Post
    How about this? Now you can change the specific language easily. I added a newline at the end.
    Code:
    //This programme shows an example of if...else statement
    #include <iostream>
    #include <string>
    
    int main()
    {
        using namespace std;
    
        const string bestresponse = "C++";
        string response;
    
        cout<< "What is your favorite programming language? ";
        cin>> response;
        if (response == bestresponse)
        cout<< "You have a great taste. " << bestresponse << " is a great language.";
        else
        cout<< "It's not as good as " << bestresponse << ", but "<< response<< " is a great programming language too.";
        cout << '/n';
    
        return 0;
    }
    wow...nice. I've never thought of using const. By the way, your newline should be this "\n" instead of '/n'.

  4. #4
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Both are same.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    Both are same.
    They are not, which is why robatino fixed the code example. '\n', not '/n', is the new line character.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by laserlight View Post
    They are not, which is why robatino fixed the code example. '\n', not '/n', is the new line character.
    I think the post was referring to the OP's two code examples in the first post, not my code.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, it took me copy'n'paste from the original post and running "diff" to even see the difference.

    There is no determinable difference from the executable, but there is of course a small difference in the way the code gets produced by the compiler - there is more code for the first piece of code, since it's referencing the "response" variable to output what is essentially a constant value. For flexibility, robatino's example is better, as everyone agrees.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

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