Swapping do while loop

Hi, what's the best way to remove the do/while loop and just have it go once? It's looping nonstop so the program is never ending and it's not supposed to do that? Suggestions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

using namespace std;

int main()
{
    int number;
 do 
 { 
    cout << "Please enter an integer number.";
    while (!(cin >> number))
    {
        cout << "Please enter numeric data only! Your number: ";
        cin.clear();
        cin.ignore();
    }
    while (number < 0)
    {
        cout << "Please enter positive numbers only! Your number: ";
        cin >> number;
        cin.clear();
        cin.ignore();
    }
    
    cout << "Multiplication table" << endl;
    
  for (int i=0; i<number; i++)
  {
      for (int j=0; j<number; j++)
      {
          cout << (i+1)* (j+1) << " ";
      }
      cout << endl;
  }
 }while(true);
  return 0;
}  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <iomanip>

int main()
{
        int number = 1 ;

   // input:
        while( std::cout << "enter a positive integer less than 21: " )
        {
            if( std::cin >> number )
            {
                if( number > 0 && number < 21 ) break ; // goto tabulate ;
                std::cout << "that is out of range. try again\n" ;
            }

            else
            {
                std::cout << "that is not an integer. try again\n" ;
                std::cin.clear() ;
                std::cin.ignore( 1000, '\n' ) ;
            }
        } // goto input ;

    // tabulate:
        std::cout << "\n\n" ;
        for( int i = 1 ; i <= number ; ++i )
        {
            for( int j = 1 ; j <= number ; ++j ) std::cout << std::setw(4) << i*j ;
            std::cout << "\n\n" ;
        }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    while (true)
    { 
        int number;
        cout << "Please enter a positive integer (0 to quit): ";
        while (true)
        {
            if (!(cin >> number))
                cout << "Please enter numeric data only! Your number: ";
            else if (number < 0)
                cout << "Please enter positive numbers only! Your number: ";
            else
                break;
            cin.clear();
            cin.ignore(9999, '\n');
        }
        cin.ignore(9999, '\n');  // eat extra chars, e.g.,  123 abc

        if (number == 0)
            break;

        cout << "Multiplication table\n";
        for (int i=0; i<number; i++)
        {
            for (int j=0; j<number; j++)
                cout << setw(4) << (i+1)*(j+1) << ' ';
            cout << '\n';
        }
    }

    return 0;
}

Last edited on
Here describe coding for an integer number is not true cin >> number;
cin.clear();
cin.ignore();
in this place, you can use int ignore values which can produce the best "Please enter numeric data only! Your number: "; that give you a clear integer number for out put


#include <iostream>

using namespace std;

int main()
{
int number;

do
{
cout << "Please enter an integer number.";
while (!(cin >> number))
{
cout << "Please enter numeric data only! Your number: ";
cin.clear();
cin.ignore();
}
while (number < 0)
{
cout << "Please enter positive numbers only! Your number: ";
cin >> number;
cin.clear();
cin.ignore();

For more visit:- http://www.traininginlucknow.in/best-c-language-training-in-Lucknow.html
Topic archived. No new replies allowed.