-
February 24th, 2009, 03:50 PM
#1
Interactive Multiplication Table...probably easy solution
Okay, for my C/C++ class, I need to make a C++ program that displays multiplication tables, with the number of rows and columns for each table being entered by the user. Assume a max of a 20x20 table. The program must also support generating multiple tables, until the user chooses to exit the program. Invalid entries should result in an error message.
Basically, we're learning about looping.
Here's what I have so far...
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main () {
int x, y;
cout << "Enter the number of rows (20 max):" << endl;
cin >> x;
if(x>20){
cout << "You must enter a value less than 20. Please restart the program." << endl;
system ("pause");
abort();
}
cout << "Enter the number of columns (20 max):" << endl;
cin >> y;
if(y>20){
cout << "You must enter a value less than 20. Please restart the program." << endl;
system ("pause");
abort();
}
for(x=1; x<20; x++){
cout << setw(5) << x;
}
for(y=1; y<20; y++){
cout << setw(5) << y;
cout << endl;
}
system("pause");
}
The problem I'm having is that whether I enter 4 or 20, the table lists numbers out to 20, and it also doesn't fill in all of the rows or columns, only the first two. I imagine that's where the looping comes in to play, but I don't know how =P. Learning C++ through lectures is a terrible idea ><.
Thanks in advance!!
-
February 24th, 2009, 04:54 PM
#2
Re: Interactive Multiplication Table...probably easy solution
Two things. First, you just went to the trouble of obtaining valid bounds in the "x" and "y" variables; then you immediately discard those numbers when you set each to 1 in the first statement of the for loops. Use different variables for your loop counters, and don't hard-code 20 as the number of loop iterations.
Second, in order to make a table you're going to need 2 *nested* loops, not 2 sequential loops as you have now. As in, loop over every row and within each row loop over every column.
-
February 24th, 2009, 07:58 PM
#3
Re: Interactive Multiplication Table...probably easy solution
Lindley's response was good. I would only add that instead of asking once for input and checking, just put the input prompt in a loop that continues as long as the input is invalid. Make sure you check for negative numbers as well, not just over 20.
Edit: Also, I would suggest making your variable names more descriptive. 'x' and 'y' could mean anything
-
February 24th, 2009, 10:19 PM
#4
Re: Interactive Multiplication Table...probably easy solution
Here's what I have now:
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main () {
int x, y;
int rows, columns;
cout << "Enter the number of rows (20 max):" << endl;
cin >> x;
if(x>20){
cout << "You must enter a value less than 20. Please restart the program." << endl;
system ("pause");
abort();
}
if(x<=0){
cout << "You must enter a value greater than 0. Please restart the program." << endl;
system ("pause");
abort();
}
cout << "Enter the number of columns (20 max):" << endl;
cin >> y;
if(y>20){
cout << "You must enter a value less than 20. Please restart the program." << endl;
system ("pause");
abort();
}
if(y<=0){
cout << "You must enter a value greater than 0. Please restart the program." << endl;
system ("pause");
abort ();
}
cout << setw(5);
for(rows = 1; rows < 20; rows++){
cout << setw(5) <<rows;
}
cout << endl;
for(rows = 1; rows < 20; rows++){
cout << setw(5) << rows;
for(columns = 1; columns < 20; columns++){
cout << setw(5) <<rows * columns;
}
cout << endl;
}
system("pause");
}
I'm having trouble making the cin value set the number of rows and columns. I don't really know what I accomplished in defining "rows" and "columns" and making them separate from "x" and "y". Maybe I misunderstood what I'm supposed to do.
Also, I chose "x" and "y" because in my mind, they represent the x-axis and y-axes...rows and columns, respectively =P. Yeah...I'm a nerd.
-
February 24th, 2009, 10:23 PM
#5
Re: Interactive Multiplication Table...probably easy solution
You're still hard-coding 20 on the loops. You don't want to do that, figure out what to replace it with.
Also, if I were you I'd swap the meaning of (x,y) with (rows,columns). The former make more sense as counter variables while the latter make more sense as limit variables----which is the reverse of what you seem to be trying for now.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|