Having trouble with multiplication table generator in C++ - c++

So I made this program in C++ in which the user is prompted to enter a number and a multiplication table is generated based on the number that is inputted. It will continuously ask the user to input a number until an alphabet letter is pressed to quit.
I'm trying to make the table go up to 10, as it only goes up to 9. When you try to enter 10, it only generates a 1 x 1 table then the program closes. Also, I'm trying to make it so that when you enter a negative number such as -3, it displays something like "Number must be greater than 0" then the program continues to ask for a number input.
Here is my code so far
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
char userSelection;
int numForTable;
int col;
int row;
cout << "Please enter a number (press q or any letter to quit) ";
while(cin>>userSelection)
{
int numForTable = (int)userSelection - 48;
if(numForTable<1 or numForTable > 12)
{
cout << "" << endl;
cout<<"Have a nice day"<<endl;
return 0;
}
else
{
cout << "\n"
<< "Multiplication table for: " << numForTable << " is:" << endl
<< "\n"
<< " " << 1;
for (col = 2; col <= numForTable; ++col)
cout << " " << col;
cout << endl;
cout << " ----|";
for (col = 2; col <= numForTable; ++col)
cout << "----|";
cout << endl;
for (col = 1; col <= numForTable; ++col)
{
cout << setw(2) << col << "|";
for (row = 1; row <= numForTable; ++row)
cout << setw(4) << col * row << "|";
cout << endl;
cout << " -|----";
for (row = 2; row <= numForTable - 1; ++row)
cout << "|----";
cout << "|----|";
cout << endl;
}
}
cout << "" << endl;
cout << "Please enter a number (press q or any letter to quit) ";
}
return 0;
}

You're reading a single character:
while(cin>>userSelection)
And then converting it to its numeric value:
int numForTable = (int)userSelection - 48;
The behaviour you're looking for would be better served by"
cin >> numForTable;
And choosing a different sentinel. Alternatively, read a full line of input and then extract the number from that string after checking for the q.

Related

Trying to have c++ program quit by inputting a letter

I made a program that generates a multiplication table for the number that is inputted by the user:
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
char userSelection;
int numForTable;
int col;
int row;
do
{
cout << "Please enter a number for your multiplication table: " << endl;
cin >> numForTable;
while (numForTable < 1 || numForTable > 10)
{
cout << "Please enter a number between 1 & 10." << endl;
cin >> numForTable;
}
cout << "\n"
<< "MULTIPLICATION TABLE: " << numForTable << "'s" << endl
<< "\n"
<< " " << 1;
for (col = 2; col <= numForTable; ++col)
cout << " " << col;
cout << endl;
cout << " ----|";
for (col = 2; col <= numForTable; ++col)
cout << "----|";
cout << endl;
for (col = 1; col <= numForTable; ++col)
{
cout << setw(2) << col << "|";
for (row = 1; row <= numForTable; ++row)
cout << setw(4) << col * row << "|";
cout << endl;
cout << " -|----";
for (row = 2; row <= numForTable - 1; ++row)
cout << "|----";
cout << "|----|";
cout << endl;
}
}
while (userSelection != 'q');
return 0;
}
It continuously asks the user to input a number until the program is closed, but I'm trying to make it so the program closes when the user inputs any alphabet letter followed by a message that says something like "Have a nice day"
I totally used your program to write the table, and only make the mechanism to switch between writing the table or exiting the program. Since your program only writes the table for integer between 1-10, which is 2-9, I used the ASCII code of the char for it. Here's the code.
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
char userSelection;
int numForTable;
int col;
int row;
while(cin>>userSelection)
{
int numForTable = (int)userSelection - 48;
if(numForTable<2 or numForTable > 9) //because the limit of the table is between 1-10, that is 2-9
{
cout<<"Have a nice day"<<endl;
return 0;
}
else
{
cout << "\n"
<< "MULTIPLICATION TABLE: " << numForTable << "'s" << endl
<< "\n"
<< " " << 1;
for (col = 2; col <= numForTable; ++col)
cout << " " << col;
cout << endl;
cout << " ----|";
for (col = 2; col <= numForTable; ++col)
cout << "----|";
cout << endl;
for (col = 1; col <= numForTable; ++col)
{
cout << setw(2) << col << "|";
for (row = 1; row <= numForTable; ++row)
cout << setw(4) << col * row << "|";
cout << endl;
cout << " -|----";
for (row = 2; row <= numForTable - 1; ++row)
cout << "|----";
cout << "|----|";
cout << endl;
}
}
}
return 0;
}
First, it changes the char input to the ASCII code, then it checks whether the number ranges from 2 to 9, then execute the program according to it. Please do correct me if i have any mistake on it.
You can type and input a character or string in 'numForTable' to quit the program but it will throw an error because of a type mismatch.
To handle this, you can use try and catch in c++
//start of do while loop
cout << "Please enter a number for your multiplication table: " << endl;
try{
cin >> numForTable;
if(!numForTable){ //if numForTable is not int, it will throw the the catch will be executed
throw 0;
}
}catch(...){
cout<< "Have a nice day";
return 0;
}
while (numForTable < 1 || numForTable > 10)
{
cout << "Please enter a number between 1 & 10." << endl;
// I do the same here
try{
cin >> numForTable;
if(!numForTable){
throw 0;
}
}catch(...){
cout<< "Have a nice day";
return 0;
}
}
I tried this and it worked
Also I changed the do while to -- do...while(true)

how to find sum of cells surrounding a user input cell in 2d array?

this is what I have so far with the srand(0) omitted for ease of calculation the values are close except for the outer limits. the formatting for the output is all correct but the sum value for the surrounding cells is usually in the millions if the chosen cell is on column or row 9.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int matrix[9][9];
int row_n, col_n, sum, surround_sum, i, j;
cout << "\t columns" << endl;
cout << " 1 2 3 4 5 6 7 8 9 " << endl;
for (int i = 0; i < 9; i++)
{
cout << "row " << i + 1 << " ";
for (int j = 0; j < 9; ++j)
{
matrix[i][j] = rand() % 10;
cout << matrix[i][j] << " ";
}
cout << endl;
}
cout << "what array cell would you like to see? (press enter after each entry)" << endl;
cout << "row = ";
cin >> row_n;
cout << "column = ";
cin >> col_n;
cout << "the number " << matrix[row_n - 1][col_n - 1] << " is in cell " << row_n << "," << col_n << endl;
for (int i=(row_n-2);i<(row_n+1);i++){
for(int j=(col_n-2);j<(col_n+1);j++){
if (i>9||i<0)
i=0;
if (j>9||j<0)
j=0;
sum+=matrix[i][j];
surround_sum= sum-matrix[row_n-1][col_n-1]+2;
}}
cout << "the sum of the cells surrounding cell " << row_n << "," << col_n << " is " << surround_sum;
return 0;
}

C++ simple two-dimensional array overwritten by last input

So I've run into a problem while writing this simple line of code for a school project. What happens is that after asking for user input to fill an array with values, the input for the last column overwrites all the other columns. I've tried finding the problem myself but I just can't seem to find it!
Part of the code that I think is problematic:
for (col = 0; col < arraywidth; col++) {
for (row = 0; row < arrayheight; row++) {
cout << "Please input a value for element " << col << ", " << row << "." << endl;
cin >> T[col][row];
}
}
Thanks for your help. Here's the full code:
#include <iostream>
using namespace std;
int main()
{
// ------------- Variables ---------------------------------
int arrayheight, arraywidth, col, row, maxe, pos_c, T[arrayheight][arraywidth];
// ---------------------------------------------------------
// ------------- Array Creation & Filling ------------------
cout << "Please insert the height of the array." << endl;
cin >> arrayheight;
cout << "Please insert the width of the array." << endl;
cin >> arraywidth;
cout << "Now we will be inputting values in the two-dimensional array." << endl;
for (col = 0; col < arraywidth; col++) {
for (row = 0; row < arrayheight; row++) {
cout << "Please input a value for the element " << col << ", " << row << "." << endl;
cin >> T[col][row];
}
}
// ---------------------------------------------------------
// ------------- Displaying the Array ----------------------
cout << "Now we will be displaying the array." << endl;
for (col = 0; col < arraywidth; col++) {
cout << endl;
for (roaw = 0; row < arrayheight; row++) {
cout << T[col][row] << " | ";
}
}
// ---------------------------------------------------------
// ------------- Logical Function as Requested -------------
cout << " " << endl;
for (col = 0; col < arraywidth; col++) {
maxe = -99;
for (row = 0; row < arrayheight; row++) {
if (T[col][row] > maxe) {
maxe = T[col][row];
pos_c = col;
}
}
cout << "The maximum value of the column " << col << " " << "is " << maxe << endl;
}
// ---------------------------------------------------------
return 0;
}
Picture of what happens:
You have the declaration of the array t[arrayheight][arraywidth] before you assign to those variables. You need to move that declaration down to after the variables are read from the user.
Also, C++ doesn't allow variable-length arrays -- that's a G++ extension. You should use std::vector<std::vector<int>> or dynamic allocation with new.

Nested For - Loops to create multiplication table C++

I've been trying to overcome this problem for a few hours now and I seem to have one approach to the situation. It seems that the use of selection statements worked in creating the table necessary. Although there are formatting issues.
I'd like to know if there was a way to create the same table
using only nested for-loops as mentioned by our professor.
Are the selection statements necessary or can we implement a system of nested for loops to acquire the same results?
The image below is the required table:
But the image below is what I have:
Below is my code:
for (int i = 0; i <= numChoice; ++i)
{
if (i == 0)
{
for (int k = 1; k <= numChoice; ++k)
{
cout << " " << k;
}
cout << "\n";
}
else
{
cout << i << " | ";
for (int j = 1; j <= numChoice; ++j)
{
if (j*i <= 9)
{
cout << " " << j*i << "|";
}
else if (j*i > 9 && j*i <= 100)
{
cout << " " << j*i << "|";
}
else if (j*i > 99 && j*i <= 999)
{
cout << " " << j*i << "|";
}
}
cout << "\n";
for (int k = 0; k <= numChoice; ++k)
{
if (k == 0)
{
cout << "-|";
}
else
{
cout << "----|";
}
}
cout << "\n";
}
}
The following code uses no if else constructs. The formatting can be got by using setw, used for setting the width of integers.Following code produces perfect output.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i,j;
cout<<" "<<1;//5 space chars
for(i = 2;i <= 10;++i)
cout<<" "<<i;//4 space chars
cout<<endl;
cout<<" ----|";
for(i = 2;i <= 10;++i)
cout<<"----|";
cout<<endl;
for(i = 1;i <= 10;++i)
{
cout<<setw(2)<<i<<"|";
for(j = 1;j <= 10;++j)
cout<<setw(4)<<j*i<<"|";
cout<<endl;
cout<<" -|----";
for(j = 2;j <= 9;++j)
cout<<"|----";
cout<<"|----|";
cout<<endl;
}
return 0;
}
#FranticCode. I'm also in the same class as you and was having problems with this homework assignment as well. I still don't understand it, but I figured out how to manipulate Sumeet's code to give us correct format. The ONLY thing I am having a problem with now is adding an empty space AFTER the first multiplication table and before the menu redisplay. I'll share what I have and maybe you can figure it out. Still going to ask the professor to review chapter 5 because I would like to learn it rather than just submit the homework.
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
char userSelection;
int numForTable;
int col;
int row;
do
{
cout << "MENU" << endl
<< "a) Generate Multiplication Table" << endl
<< "q) Quit the program" << endl
<< "Please make a selection: ";
cin >> userSelection;
if (userSelection == 'a')
{
cout << "Please enter a number for your multiplication table: " << endl;
cin >> numForTable;
while (numForTable < 1 || numForTable > 10)
{
cout << "Please enter a number between 1 & 10." << endl;
cin >> numForTable;
}
cout << "\n"
<< "MULTIPLICATION TABLE: " << numForTable << "'s" << endl
<< "\n"
<< " " << 1;
for (col = 2; col <= numForTable; ++col)
cout << " " << col;
cout << endl;
cout << " ----|";
for (col = 2; col <= numForTable; ++col)
cout << "----|";
cout << endl;
for (col = 1; col <= numForTable; ++col)
{
cout << setw(2) << col << "|";
for (row = 1; row <= numForTable; ++row)
cout << setw(4) << col * row << "|";
cout << endl;
cout << " -|----";
for (row = 2; row <= numForTable - 1; ++row)
cout << "|----";
cout << "|----|";
cout << endl;
}
}
else if (userSelection != 'q')
{
cout << "Invalid Selection\n" << endl;
}
else if (userSelection == 'q')
{
cout << " You have chosen to quit the program. Thank you for using!" << endl;
}
}
while (userSelection != 'q');
//system("PAUSE");
return 0;
}
got curious to see if i could add the lines as easy as i claimed, it took a bit of fiddling, but here's the result (updated code below to also have lines).
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int counter;
int counter2;
int amount;
cout << " |-----------------------------------------------------------|" << endl; // first line of table.
for(counter=1;counter<11;counter++){ // the 2 for lines create our 2 dimensional table
for(counter2=1;counter2<11;counter2++){
cout << " | " << setw(3) << counter*counter2; // setw(3) is a function of <iomanip>,
//setting minimum width to 3 for numbers.
}
cout << " |" << endl; // this here is being added to the end of each line and starts a new line.
cout << " |-----------------------------------------------------------|" << endl; // this is being inserted between each line, and starts a new line.
}
return 0;
}
Use the following construct:
for (int i=0; i<=numChoice; i++) // display first row of numbers
cout <<"\t" << i << "\t";
cout << "\n";
for (int i=0; i <=numChoice; i++) {
cout << i << "\t";
for (int j=0; j <=numChoice; j++)
cout << i*j << "\t";
cout << "\n";
}

Two Dimensional Array outputs one continuous row

Hello I am new to C++ and am having trouble understanding why this two dimensional array is only producing one row and many columns. It reads the correct information but does not output it with the correct columns and rows.
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <windows.h>
using namespace std;
char pat [9][9]= {'$','$','$','$','$','$','$','$','$',
'$','$','$','$','$','$','$','$','$',
'$','$','$','$','$','$','$','$','$',
'$','$','$','$','$','$','$','$','$',
'$','$','$','$','$','$','$','$','$',
'$','$','$','$','$','$','$','$','$',
'$','$','$','$','$','$','$','$','$',
'$','$','$','$','$','$','$','$','$',
'$','$','$','$','$','$','$','$','$'}; // 9x9 matrix
int main ()
{
int pattern,
dimensions;
do
{
cout << "1) Display Pattern 1" << endl; //menu selections
cout << "2) Display Pattern 2" << endl;
cout << "3) Display Pattern 3" << endl;
cout << "4) Display Pattern 4" << endl;
cout << "5) Exit Program" << endl << endl;
cout << "Please select an option. ";
cin >> pattern;
if (pattern == 1)
{
system("cls");
do
{
cout << "Note: Choose a number between 1 and 10." << endl;
cout << "Choose a number ";
cin >> dimensions;
cout << endl;
if (dimensions > 1 && dimensions < 10)
/* based on the user's input for dimension
it will output a square i.e. 2x2, 3x3, 4x4 etc */
{
cout << "True!" << endl;
for (int rows = 0; rows < dimensions; rows++)
{
for (int cols = 0; cols < dimensions; cols++)
cout << pat[cols][rows];
}
}
else
{
cout << "Error! Number is not between this set!" << endl;
Sleep(3000);
cout << endl;
}
}
while (pattern == 1);
}
else if (pattern == 2)
{
system("cls");
do
{
cout << "Note: Choose a number between 1 and 10." << endl;
cout << "Choose a number ";
cin >> dimensions;
cout << endl;
if (dimensions > 1 && dimensions < 10)
{
cout << "True!";
}
else
{
cout << "Error! Number is not between this set!" << endl;
Sleep(3000);
cout << endl;
}
}
while (pattern == 2);
}
else if (pattern == 3)
{
system("cls");
do
{
cout << "Note: Choose a number between 1 and 10." << endl;
cout << "Choose a number ";
cin >> dimensions;
cout << endl;
if (dimensions > 1 && dimensions < 10)
{
cout << "True!";
}
else
{
cout << "Error! Number is not between this set!" << endl;
Sleep(3000);
cout << endl;
}
}
while (pattern == 3);
}
else if (pattern == 4)
{
system("cls");
do
{
cout << "Note: Choose a number between 1 and 10." << endl;
cout << "Choose a number ";
cin >> dimensions;
cout << endl;
if (dimensions > 1 && dimensions < 10)
{
cout << "True!";
}
else
{
cout << "Error! Number is not between this set!" << endl;
Sleep(3000);
cout << endl;
}
}
while (pattern == 4);
}
else if (pattern == 5)
{
return 0;
}
else
{
cout << "Please input a valid entry." << endl << endl;
Sleep(3000);
cout << endl;
}
}
while (pattern != 5);
}
When you're printing the array:
for (int rows = 0; rows < dimensions; rows++)
{
for (int cols = 0; cols < dimensions; cols++)
cout << pat[cols][rows]
}
You never print a new line, so it's all on one row. You want something like this:
for (int rows = 0; rows < dimensions; rows++)
{
for (int cols = 0; cols < dimensions; cols++)
cout << pat[cols][rows]
cout << endl;
}
If you want to initialize a multidimensional array, you have to do it like so:
int multiarray[3][3] =
{
{1,2,3},
{4,5,6},
{7,8,9}
};
Using nested braces to separate the dimensions.
After this to output it the way you want you do it as such:
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
std::cout << multiarray[i][j];
}
std::cout << std::endl;
}