Thread: Does this and this have differences?

  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-14-2008 at 12:21 AM. 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
    28,413
    Both are same.
    They are not, which is why robatino fixed the code example. '\n', not '/n', is the new line character.
    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

  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