Sooo yeah. Not one of those derps begging for a solution, but my solution simply isn't working out how I want it to. TLDR;

Letters A-T are supposed to obey the number of coulmns entered by the user, then output them.

My program obeys the number of coulmns by recognizing your input, but it does not put the letters back into the number of coulmns accordingly. It is just outputting them. I know there has to be some really stupid, silly issue I'm overlooking but Im trying to finish this up.

Any help is good help.

#include <cmath>
#include <iostream>      
#include <iomanip>

using namespace std;    // Declares that we will be using the namespace library.

int main()             // The beginning of where the C++ program
{
    // Variables declared
    int ncol;
    double poweroftwo;
    char letter;

    // Main Do-while loop (nonstop)
    do
    {
        do // Do - while loop to check user input
        {
            cout<<"Please input a number of columns: [8+ ends program, 0 is invalid. 1-7 are valid] "; //Ask the user to input the number of columns
            cin>>ncol;

        if ((ncol>=0)&&(ncol<=7)) // If ncol is the range, we close the do-while loop
                break;
            else // If it is not, we display the error message and the loop keep going
                cout<<"Number of columns out of range [1-7]. Try again..."<<endl;
        }
        while(1); //We make it infinite, only close at the break (columns are in the range)

        if (ncol==0) break; //If the user input is 0, we close the program before any display
        {
                     for(letter = 'A'; letter <= 'T'; letter++)
             cout<<setw(5)<<letter; //We display the number using a set width
        }

        cout<<endl; // To start the next user input in a new line after the previous display
    }
    while(1); //We make it infinite, only close at the break (columns=0)

    return 0;                  //Causes the main function to finish.
}

Recommended Answers

All 2 Replies

I appreciate that you are one of the very few people that comment the code but
1) your excessively long lines with comments make your logic hard to follow
2) most of your comments are meaningless.

For example:

using namespace std; // Declares that we will be using the namespace library.
Relatively obvious...

-------------

int main() // The beginning of where the C++ program
Also obvious...

-------------

    // Variables declared
    int ncol;
    double poweroftwo;
    char letter;

Yeah, so? A description of the variables would be much more useful

-------------

// Main Do-while loop (nonstop)
do

Why do you nead a main do-while loop that's nonstop?

-------------

{        while(1); //We make it infinite, only close at the break 

Didn't you already say this at the "do"?

Comments are supposed to help the reader understand what you wrote and why, not just regurgitate the bleeding obvious. What's the reason you use the statements? How does it help you get to your final solution?

And why use an infinite do-while? An infinite while makes more sense because you know immediately it's an infinite loop, so your comment can be used to inform the reader of something useful.

I have no real idea what you are trying to accomplish. Your comments seem to indicate you input a number and output the letters in that many columns. If so, where in the output loop do you test if you've output enough columns and start back at column 1?

In the loop on line 31 you are displaying all the letters 'A' through 'T' on the same line. What you need to do is put a line break when the loop counter reaches the number of columns input on line 20. You will probably need two counters, one that counts from 0 to ncols and another that counts from 'A' to 'T' as it is doing now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.