Like Tree4Likes
  • 1 Post By Khabz
  • 1 Post By whiteflags
  • 1 Post By laserlight
  • 1 Post By antred

Chapter 2 practice problems

This is a discussion on Chapter 2 practice problems within the C++ Programming forums, part of the General Programming Boards category; 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 ...

  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.
    Kyle Spalding likes this.

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,666
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,666
    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.
    Kyle Spalding likes this.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    2,549
    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.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,300
    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
    21,632
    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";
    hex_dump likes this.
    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

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    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
    227
    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.
    Salem likes this.

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    2,549
    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.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,666
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    2,549
    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.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 03-07-2013, 05:55 AM
  2. K&R chapter 8
    By Tool in forum C Programming
    Replies: 9
    Last Post: 02-22-2010, 01: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, 11:13 AM
  5. Practice C Problems, with answers
    By john123 in forum C Programming
    Replies: 3
    Last Post: 12-01-2002, 05:46 PM

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