0

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"

sup39
  • 1,081
  • 5
  • 14
  • 1
    Rather than reading into an `int` with `cin >> numForTable;` , perhaps read a line or into a `char`. – chux - Reinstate Monica Oct 19 '20 at 03:18
  • 1
    If the user enters a non-integer value for `cin >> numForTable` then the operation will fail, which is something you can test for. But I rather recommend that you [read full lines into strings](https://en.cppreference.com/w/cpp/string/basic_string/getline). Then if the string contains some special "exit the program" text you can end the program. Or if [trying to convert the string into an integer](https://en.cppreference.com/w/cpp/string/basic_string/stol) fails then you can also exit. – Some programmer dude Oct 19 '20 at 03:23
  • 2
    You are checking `userSelection` in the condition of `do-while` loop but it is not initialized (via assignment or user input). You need to take user input and validate that for the quit case. Here's an example: https://godbolt.org/z/ceq1z9. Relevant: https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Azeem Oct 19 '20 at 03:36

2 Answers2

1

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Ramzs
  • 28
  • 7
  • sorry this is late but is there a way to make the table go up to 10? @Ramzs – ChadWarden441 Oct 21 '20 at 04:37
  • @ElizabethCampbell441 just increase the limiter in `while (numForTable < 1 || numForTable > 10)` to `while (numForTable < 1 || numForTable > 20)' or something. but then, you must modify the alphabet checker, I'll leave it for you as an exercise. – Ramzs Oct 21 '20 at 06:19
1

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)

Eric Echemane
  • 94
  • 1
  • 9
  • I assume where you have "// I do the same here", that's where I put the main multiplication table code? `cout << "\n"` `<< "MULTIPLICATION TABLE: " << numForTable << "'s" << endl` to `cout << "|----|";` `cout << endl;` – ChadWarden441 Oct 19 '20 at 06:09
  • what I mean is, if the user enters a number that is less than 1 or greater than 9, you prompt the user to enter a new number, in that case, it is possible for the user to input a string or character that's why it must be catch as well or else the loop will never ends. – Eric Echemane Oct 19 '20 at 12:06