You want to ask the user for amount of columns per line, and then print that amount of columns. Basically your while loop needs to count the amount of loops it's done, and if (amount % count == 0) cout << endl;. just make sure your count is never zero.
I'll leave the formatting to you, though it looks like you've got it I hate that stuff.
hey guys, im in the same class as mk620t. I really have no idea even how to start. would a for loop be better than while? any advice or code would be greatly appreciated.
Do I need to switch the current arguments for modulus listed above?
#include <iostream> //includes c++ library
#include <cmath> //includes math functions
#include <iomanip> //allows manipulation
using namespace std; //allows use of cin and cout
int main() //start of main argument
{
int degree; //declare integer variable degree
loop: //This is the point that the goto loop jumps to
cout<<"Number of columns: "; //prints input prompt to screen
cin>>degree; //stores using input to variable degree
cout<<endl; //prints an extra blank line to the screen
switch (degree) //Beginning of switch based on variable degree
{
case 0: //Used when input is '0'
break; //Tells the program to break or end
case 1: //Used when input is '1'
for(int degree=0; degree<15; degree++) //Start of for loop. degree equals 0, when degree is less than 15, increase degree by 1
{
cout << setw(6) << right << pow(2,degree) << endl; //Sets the width of display to 6, right justifies and uses the pow function to calculate
// 2^0, 2^1, 2^2,...... after each value prints the line ends.
}
cout << endl; //Prints a blank line to the screen
goto loop; //Goes back to the beginning of the loop
case 2: //Used when input is '2'
for(int degree=0; degree<15; degree++) //Start of for loop. degree equals 0, when degree is less than 15, increase degree by 1
{
cout << setw(6) << right << pow(2,degree++); //Sets width of display to 6, right justifies, uses pow function
cout << setw(7) << right << pow(2,degree) << endl; //Sets width to 7, right justifies, uses pow function, ends line.
//Function loops until the desired values are printed
}
cout << endl; //Prints a blank line to the screen
goto loop; //Goes back to the beginning of the loop
case 3: //Used when input is '3'
for(int degree=0; degree<15; degree++) //Start of for loop. degree equals 0, when degree is less than 15, increase by 1
{
cout << setw(6) << right << pow(2,degree); //Sets width of display to 6, right justifies, uses pow function
cout << setw(7) << right << pow(2,++degree); //Sets width to 7, right justifies, uses pow function
cout << setw(8) << right << pow(2,++degree) << endl; //Sets width to 8, right justifies, used pow function, ends line.
//Function loops until the desired values are printed
}
cout << endl; //Prints a blank line to the screen
goto loop; //Goes back to the beginning of the loop
case 4:
for(int degree=0; degree<15; degree++) //Start of for loop. degree equals 0, when degree is less than 15, increase by 1
{
cout << setw(6) << right << pow(2,degree); //Sets width of display to 6, right justifies, uses pow function
cout << setw(7) << right << pow(2,++degree); //Sets width to 7, right justifies, uses pow function
cout << setw(8) << right << pow(2,++degree); //Sets width to 8, right justifies, uses pow function
cout << setw(9) << right << pow(2,++degree) << endl; //Sets width to 9, right justifies, uses pow function, ends line
//Function loops until the desired values are printed
}
cout << endl; //Prints a blank line to the screen
goto loop; //Goes back to the beginning of the loop
case 5:
for(int degree=0; degree<15; degree++) //Start of for loop. degree equals 0, when degree is less than 15, increase by 1
{
cout << setw(6) << right << pow(2,degree); //Sets width of display to 6, right justifies, uses pow function
cout << setw(7) << right << pow(2,++degree); //Sets width to 7, right justifies, uses pow function
cout << setw(8) << right << pow(2,++degree); //Sets width to 8, right justifies, uses pow function
cout << setw(9) << right << pow(2,++degree); //Sets width to 9, right justifies, uses pow function
cout << setw(10) << right << pow(2,++degree) << endl; //Sets width to 10, right justifies, uses pow function, ends line
//Function loops until the desired values are printed
}
cout << endl; //Prints a blank line to the screen
goto loop; //Goes back to the beginning of the loop
case 6:
for(int degree=0; degree<15; degree++) //Start of for loop. degree equals 0, when degree is less than 15, increase by 1
{
cout << setw(6) << right << pow(2,degree); //Sets width of display to 6, right justifies, uses pow function
cout << setw(7) << right << pow(2,++degree); //Sets width to 7, right justifies, uses pow function
cout << setw(8) << right << pow(2,++degree); //Sets width to 8, right justifies, uses pow function
cout << setw(9) << right << pow(2,++degree); //Sets width to 9, right justifies, uses pow function
cout << setw(10) << right << pow(2,++degree); //Sets width to 10, right justifies, uses pow function
cout << setw(11) << right << pow(2,++degree) << endl; //Sets width to 11, right justifies, uses pow function, ends line
//Function loops until the desired values are printed
}
cout << endl; //Prints a blank line to the screen
goto loop; //Goes back to the beginning of the loop
default: //The case that the switch defaults to is no recognizable input is available
cout << "Number of columns out of range [0-6]. Try again..." << endl; //Prints out of range prompt
goto loop; //Goes back to the beginning of the loop
I'm having trouble with it printing out more than 15 values. Happens when 15 isn't cleanly divisible by the number of rows. I understand that modulus fixes this, I just don't understand how to implement it.
case 2: for(int degree=0; degree<15; degree++)
{
cout << setw(6) << right << pow(2,degree++);
cout << setw(7) << right << pow(2,degree) << endl; }
cout << endl; //Prints a blank line to the screen
goto loop; //Goes back to the beginning of the loop
case 3: for(int degree=0; degree<15; degree++)
{
cout << setw(6) << right << pow(2,degree);
cout << setw(7) << right << pow(2,++degree);
cout << setw(8) << right << pow(2,++degree) << endl;
}
cout << endl;
goto loop;
case 4:
for(int degree=0; degree<15; degree++)
{
cout << setw(6) << right << pow(2,degree);
cout << setw(7) << right << pow(2,++degree);
cout << setw(8) << right << pow(2,++degree);
cout << setw(9) << right << pow(2,++degree) << endl;
}
cout << endl; //Prints a blank line to the screen
goto loop; //Goes back to the beginning of the loop
case 5:
for(int degree=0; degree<15; degree++)
{
cout << setw(6) << right << pow(2,degree);
cout << setw(7) << right << pow(2,++degree);
cout << setw(8) << right << pow(2,++degree);
cout << setw(9) << right << pow(2,++degree);
cout << setw(10) << right << pow(2,++degree) << endl; }
cout << endl;
goto loop;
case 6:
for(int degree=0; degree<15; degree++)
{
cout << setw(6) << right << pow(2,degree);
cout << setw(7) << right << pow(2,++degree); cout << setw(8) << right << pow(2,++degree); cout << setw(9) << right << pow(2,++degree); cout << setw(10) << right << pow(2,++degree); cout << setw(11) << right << pow(2,++degree) << endl;
}
cout << endl;
goto loop;
default: cout << "Number of columns out of range [0-6]. Try again..." << endl;
Much better. Whenever you expect someone else to read your code (heck, even if no one will read it), it should be very neat and readable.
Now...
cout << setw(7) << right << pow(2,degree++);
Should be:
cout << right << setw(7) << pow(2,degree++);
If you want to pass non-double values to pow (since it wouldnt let me pass an int), then convert them to doubles, or initialize them as doubles in the first place, otherwise I myself, end up with a C2668:
cout << right << setw(7) << pow (2, double(degree++);
I'll be editing this post for more corrections - I'm using compiler Microsoft Visual C++ 2010 Express for this.
Edit: Without completely redesigning your program or giving you the answer, here's something to consider:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
case 1:
for (int i = 0; i < 15; i++) /* you actually don't need a switch statement for
this, in my own program I just used a for statement, much simpler that way */
{
cout << right << setw(7) << pow(2,double(i)); // using the pow function
counting++;
if (counting == columns) /* if 6 numbers are printed, and columns = 6,
this "if" statement will output to the console an endline */
{
cout << endl;
counting = 0; // reset the number counter to 0
}
}
cout << endl;
goto loop;
Let me know when you get your program to work as shown in the example, if you're still having trouble, I'll be here.