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.
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)
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;
}
Right now Im trying to write a program that can take an input file that has a bunch of integers, and then show the amount of numbers in certain ranges. For example:
If the input file has 20, 30, 40, 50, 60. And the ranges are 1-20, 21-40, 41-60
The output would be
1-20: 1
21-40: 2
41-60: 3
etc.
Im new to programming, so Im just having a little problem with my code, I know im close. Im using arrays to store them, but I wasnt sure how to make an array that adapts to how many integers there are in a file. So this is what I got so far. (Just for this exercise, Im trying to make the max amount the array will store 100 integers).
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int ranges(int);
int main()
{
ifstream indata;
ofstream outdata;
indata.open("scores1.txt");
outdata.open("scoresoutput.txt");
if(!indata)
{
cout << "Unable to open file" << endl;
return 1;
}
int range1=0;
int range2=0;
int range3=0;
int range4=0;
int range5=0;
int range6=0;
int range7=0;
int range8=0;
const int max=100;
int score[max];
while(indata)
{
for(int i=0; i < max ; i++)
{
indata >> score[i];
indata.ignore(1);
if(0 <= score[i] <= 24)
{
range1++;
}
else if(25 <= score[i] <= 49)
{
range2++;
}
else if(50 <= score[i] <=74)
{
range3++;
}
else if(75<= score[i] <= 99)
{
range4++;
}
else if(100 <= score[i] <= 124)
{
range5++;
}
else if(125 <= score[i] <= 149)
{
range6++;
}
else if(150 <= score[i] <= 174)
{
range7++;
}
else if(175 <= score[i]<= 200)
{
range8++;
}
}
}
outdata << "Range" << setw(10) << "Number of Students" << endl <<
"0-24" << setw(10) << range1 << endl<<
"25-49" << setw(10) << range2 << endl <<
"50-74" << setw(10) << range3 << endl <<
"75-99" << setw(10) << range4 << endl <<
"100-124" << setw(10) << range5 << endl <<
"125-149" << setw(10) << range6 << endl <<
"150-174" << setw(10) << range7 << endl <<
"175-200" << setw(10) << range8 << endl;
return 0;
}
Right now, the output just displays 0-24 100. Any easier way to do this than what I am doing now?
Array is your good friend. Try to use an array to manage the counts:
int range_count[8];
for (int i = 0; i < 8; ++i) {
range_count[i] = 0;
}
and in the for-loop inside your while-loop:
for(int i = 0; i < max ; ++i) {
indata >> score[i];
indata.ignore(1);
if (score[i] >= 0 && score[i] <= 200) {
range_count[score[i] / 25]++; // since your range intervals is a constant 25.
}
}
and finally, in the output:
outdata << "Range" << setw(10) << "Number of Students" << endl;
for (int i = 0; i < 8; ++i) {
i * 25 << "-" << i * 25 + 24 << setw(10) << range_count[i] << endl;
}