Thread: Chapter 2 practice problems

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    3

    Chapter 2 practice problems

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout << "Thomas\n""Jake\n""Bonita\n""Jackson\n""Paul\n";
    }
    Write a program that displays multiple lines of text onto the screen, each one displaying names of friends. Does this look correct? And these problems are from Alan Allain's C++ programming book

  2. #2
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    If you're to separate each name on the same 'cout' then it has to look like this:

    Code:
    cout << "Thomas\n" <<"Jake\n" <<"Bonita\n" <<"Jackson\n" <<"Paul\n";
    this one is also acceptable:

    Code:
    cout<<"Thomas\n";
    cout<<"Jake\n";
    cout<<"Bonita\n";
    cout<<"Jackson\n";
    cout<<"Paul\n";
    Last edited by Khabz; 03-17-2013 at 04:44 PM.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    3
    Ok, and is that for other people to read the code more easily? Or is that just part of the syntax? Thanks again.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Does this look correct?
    Do you have a compiler? They don't bite. But yes, the compiler understands the OP code. Two string literals next to each other just get fused by the compiler.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    3
    Yes I am using code blocks. It does run fine on the compiler, but since I am new to c++ I was just checking to see if programmers get angry or confused when reading code a certain way.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I can only speak for myself, but I've never been confused by it. It's more likely that they just don't know. It doesn't hurt anything if they don't know; it's just handy in case you need to break up a long string literal for legibility reasons.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    FYI: Microsoft Compiler does not merge literal strings as well as GCC does; so, the first method might not work with MS Visual C compiler.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by stahta01 View Post
    FYI: Microsoft Compiler does not merge literal strings as well as GCC does; so, the first method might not work with MS Visual C compiler.
    I've never encountered any problem having any version of VC do it, and I do use it from time to time.
    Perhaps that was with a very old version.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Since everything is squished together on one line, I find that there is no benefit to that over writing:
    Code:
    cout << "Thomas\nJake\nBonita\nJackson\nPaul\n";
    On the other hand, this may be more readable:
    Code:
    cout << "Thomas\n"
            "Jake\n"
            "Bonita\n"
            "Jackson\n"
            "Paul\n";
    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

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by stahta01 View Post
    FYI: Microsoft Compiler does not merge literal strings as well as GCC does; so, the first method might not work with MS Visual C compiler.

    Tim S.
    The OP's code compiles perfectly well on Microsoft's Visual C++ 2012 compiler.
    A C compiler would obviously not compile the code at all.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Quote Originally Posted by stahta01 View Post
    FYI: Microsoft Compiler does not merge literal strings as well as GCC does; so, the first method might not work with MS Visual C compiler.
    First I've heard of it. All versions of Microsoft Visual C++ at least from 6.0 on merge string literals just fine.

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by antred View Post
    First I've heard of it. All versions of Microsoft Visual C++ at least from 6.0 on merge string literals just fine.
    Try compiling the Code::Blocks source code using Microsoft VC++; that is one of the several things that stops it from being done without a lot of editing.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by Elysia View Post
    The OP's code compiles perfectly well on Microsoft's Visual C++ 2012 compiler.
    A C compiler would obviously not compile the code at all.
    Ah, um, it wouldn't compile C++ code as C code, he means.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > Try compiling the Code::Blocks source code using Microsoft VC++; that is one of the several things that stops it from being done without a lot of editing.
    Are you talking about an entire source tree, which probably uses all sorts of "extensions" not specified in the original C++ standard?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Salem View Post
    > Try compiling the Code::Blocks source code using Microsoft VC++; that is one of the several things that stops it from being done without a lot of editing.
    Are you talking about an entire source tree, which probably uses all sorts of "extensions" not specified in the original C++ standard?
    Yes.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 03-07-2013, 06:55 AM
  2. K&R chapter 8
    By Tool in forum C Programming
    Replies: 9
    Last Post: 02-22-2010, 02:05 PM
  3. Standish Chapter 7 (C/Unix)
    By cchallenged in forum C Programming
    Replies: 16
    Last Post: 09-18-2006, 11:44 PM
  4. Practice Problems
    By Eultza in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:13 PM
  5. Practice C Problems, with answers
    By john123 in forum C Programming
    Replies: 3
    Last Post: 12-01-2002, 06:46 PM