TransWikia.com

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

Stack Overflow Asked by ElizabethCampbell441 on October 29, 2020

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"

2 Answers

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.

Correct answer by Ramzs on October 29, 2020

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)

Answered by Eric Echemane on October 29, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP