1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

More noob C++ programmer help

Discussion in 'Programming & Webmastering' started by Lampmaster, Dec 3, 2009.

  1. Lampmaster

    Lampmaster New Member

    Joined:
    Aug 2, 2009
    Messages:
    156 (0.08/day)
    Thanks Received:
    16
    Location:
    Washington The State
    Hey guys, one last problem here.

    I have this function for outputting an array

    As it is, it outputs one element per line. Is there a way to make it output 10 elements per line?
  2. Aleksander

    Joined:
    Dec 2, 2009
    Messages:
    3,254 (1.88/day)
    Thanks Received:
    304
    The only way to output 10 lines is to get a library that integrates in the C++ as a function and does the same think again and again. Only experts do that. I did it with Nvidia's link libraries and it should always start like <stdio.h> or in this case <repeat.h>. The sqrt for example could not be known as a command if u dont put the <math.h> at the start below #include#. Maybe u can do that yourself, integrating the dll in your C++, but it needs specific commands which u need to know before using it. Otherwise.... ERROR
  3. dcf-joe

    dcf-joe

    Joined:
    Feb 11, 2008
    Messages:
    378 (0.16/day)
    Thanks Received:
    22
    Location:
    Nebraska, USA
    If I understand correctly what you are asking, yes you can have ten outputs per line. Think about those simple c++ programs that make pyramids and stuff with numbers. I can give you further information if you need it, but a hint for right now is that it would require nested for loops.
    Lampmaster says thanks.
  4. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (5.03/day)
    Thanks Received:
    5,615
    Location:
    Cheeseland (Wisconsin, USA)
    Create a variable to do the ten count. Reset the variable when it reaches 10. No need for nested loops.
    Code:
    ii = 0;
    tenloop = 1;
    while (ii < numberOfElements)
    {
        cout << stuff to output with no endl;
        ii++;
        tenloop++;
        if (tenloop == 11)
        {
              cout << endl;
              tenloop = 1;
        }
    }
    
    Lampmaster says thanks.
  5. Lampmaster

    Lampmaster New Member

    Joined:
    Aug 2, 2009
    Messages:
    156 (0.08/day)
    Thanks Received:
    16
    Location:
    Washington The State
    Awesome, that's exactly what I needed.
  6. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (5.03/day)
    Thanks Received:
    5,615
    Location:
    Cheeseland (Wisconsin, USA)
    When learning to program, it's sometimes difficult to wrap your head around the logic that you need to perform a given function. Stop and think it though.
    The idea is to make it as simple as possible and write the least code you have to, to accomplish the task at hand. There are many ways to code things, but if you can work toward the simplest way it removes superfluous code and makes it easier to understand, debug and maintain.
  7. dcf-joe

    dcf-joe

    Joined:
    Feb 11, 2008
    Messages:
    378 (0.16/day)
    Thanks Received:
    22
    Location:
    Nebraska, USA
    You can also do

    for(elementNumber = 0; elementNumber < arraySize; elementNumber++)
    {
    if((elementNumber + 1) % 10 == 0)
    {
    cout << list[elementNumber] << endl;
    }

    else
    {
    cout << list[elementNumber] << " ";
    }
    }
    Kreij says thanks.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page