Thread: Nested loops Re: Jumping in to C++

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    4

    Nested loops Re: Jumping in to C++

    Hi. I've been working on the first nested loop problem from "Jumping in to C++ book"

    I cant seem to get the first column to line up properly, well in fact it's the 9th column is in the between the fist column on every other line. I'll link a pic I think to better explain.
    Nested loops Re: Jumping in to C++-nested-loop-jpg

    here's the code from the exercise as it is in the book

    Code:
    include <iostream>
    using namespace std;
    int main()
    {
        for ( int i = 0; i < 10; i++)
        {
        cout << '\t' << i; //\t represents Tab to format the output
        }
        for ( int i = 0; i < 10; ++i )
        {
         cout  << i;
         for ( int j = 0; j < 10; ++j )
        {
        cout << '\t' << i * j;
        }
         cout << '\n';
        }
    }
    I've been trawling the forum, and adjusted the code for some time now, but I guess I'm still to green under the collar. Thanks for any tips.
    Last edited by Hendo; 01-26-2014 at 08:08 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Try say < 7 for all of your loops, just to verify that it's doing the right thing.

    Because < 10 looks to be wider than your default terminal window.

    You could do this as well
    nested_loops.exe > results.txt

    And examine the results.txt file in a text editor.
    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.

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    4
    Wow I'm so stupid lol. Thanks Salem. I just changed the defaults on my console the code was fine. I can't believe I spent hours trying to figure this out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested loops!
    By mp252 in forum C++ Programming
    Replies: 4
    Last Post: 03-28-2013, 02:38 PM
  2. Help with nested loops?? Please help!
    By GoBlue13 in forum C++ Programming
    Replies: 12
    Last Post: 02-28-2013, 07:27 AM
  3. Nested loops
    By zeondik in forum C# Programming
    Replies: 2
    Last Post: 10-26-2008, 06:58 AM
  4. Need help, nested loops
    By engstudent363 in forum C Programming
    Replies: 2
    Last Post: 02-17-2008, 10:23 PM
  5. nested for loops
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 11-17-2001, 11:44 AM