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.
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;
}
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";
}
This program seems to work fine until it goes to display the data after the sort function, it will display by destination but it bypasses displaying by airline. Then it gives the access violation window and I'm not 100% sure on what it even means. If anybody could explain and help that would be much appreciated.
#include <iostream>
#include <string>
using namespace std;
double flight_number[3], number_passengers[3], max_records, max_pass, min_pass;
string airline_name[3], destination[3], duplicate[3];
char reply, swapping;
int row, index[3];
void setup();
void load_arrays();
void display_airline();
void display_destination();
void sort();
int main()
{
setup();
load_arrays();
display_airline();
display_destination();
system("pause");
return 0;
}
void setup()
{
max_records = 3;
max_pass = 275;
min_pass = 50;
}
void load_arrays()
{
for (row = 0; row < max_records; row++)
{
cout << "Charter Number " << row << endl << endl;
cout << "Please enter..." << endl;
cout << "Airline Name----> ";
cin >> airline_name[row];
cout << endl;
cout << "Flight Number----> ";
cin >> flight_number[row];
cout << endl;
cout << "Destination----> ";
cin >> destination[row];
cout << endl;
cout << "Passenger Load----> ";
cin >> number_passengers[row];
cout << endl;
while (number_passengers[row] < min_pass || number_passengers[row] > max_pass)
{
cout << "You have entered an invalid amount of passengers." << endl;
cout << "There must be between 50 and 275 passengers. > ";
cin >> number_passengers[row];
cout << endl;
}
}
}
void display_airline()
{
system("cls");
for (row = 0; row < max_records; row++)
{
duplicate[row] = airline_name[row];
}
sort();
cout << "Airline" << " Flight" << " Destination" << " Passenger" << endl;
cout << "Name" << " Number" << " Load" << endl;
for (row = 0; row < max_records; row++)
{
cout << airline_name[index[row]] << " " << flight_number[index[row]] << " " << destination[index[row]] << " " << number_passengers[index[row]];
cout << endl;
}
}
void display_destination()
{
system("cls");
for (row = 0; row < max_records; row++)
{
duplicate[row] = destination[row];
}
sort();
cout << "Destination" << " Flight" << " Flight" << " Passenger" << endl;
cout << " Name" << " Number" << " Load" << endl;
for (row = 0; row < max_records; row++)
{
cout << destination[index[row]] << " " << airline_name[index[row]] << " " << flight_number[index[row]] << " " << number_passengers[index[row]];
cout << endl;
}
}
void sort()
{
for (row = 0; row < max_records; row++)
{
index[row] = row;
}
swapping = 'Y';
while (swapping == 'Y')
{
swapping = 'N';
for (row = 0; row < max_records; row++)
{
if (duplicate[row] > duplicate[row + 1])
{
swap (duplicate[row], duplicate[row + 1]);
swap (index[row], index[row + 1]);
swapping = 'Y';
}
}
}
}
also this is the full violation error: Unhandled exception at 0x009984F6 in Program 6.exe: 0xC0000005: Access violation reading location 0x03947188.
for (row = 0; row < max_records; row++)
if (duplicate[row] > duplicate[row + 1]).
What's duplicate[row+1] when row==3? You want the sort to go to max_records-1 since it works on pairs of elements rather than individual elements.
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;
}