#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    45
    Rep Power
    13

    Making an interactive multiplication table with a do..while loop.


    This is the code I have to make a multiplication table:

    Code:
    #include<iostream>
    #include<iomanip>
    using namespace std;
    
    int main()
    {
     int size;
     int i=0, j=0;
    
     cout<<"Enter a number to display that number's multiplication table: ";
     cin>>size;
    
     cout<<endl<<size<<" by "<<size<<" Multiplication Table"<<endl;
     cout<<endl<<"    |";
     
     for(i=1;i<=size;i++)
       cout<<setw(3)<<i<<" ";
     cout<<endl;
     
     for(i=0;i<=size;i++)
       cout<<"____";
    
     for(i=1;i<=size;i++)
       {
        cout<<endl<<setw(3)<<i<<" |";
        for(j=1;j<=size;j++)
        cout<<setw(3)<<i*j<<" ";
       }
      cout<<endl;
     return 0;
    }
    Which looks like this if the user enters in 4 for example:

    4 by 4 Multiplication Table


    | 1 2 3 4
    _______________
    1 | 1 2 3 4
    2 | 2 4 6 8
    3 | 3 6 9 12
    4 | 4 8 12 16
    ^^ except it's formatted better, I don't know why it's squeezed together in the quote. This isn't the problem though.

    Now I need to add a sentinel? or create a sentinel? (I don't know how it's written properly sorry) so that after the multiplication table is displayed, and then the user is asked "Do you want to create another one (y or n)?"

    If the user enters in 'y', then the program once again asks what number to enter for the multiplication table. (this happens after every time the table runs and the user types in 'y') If the user enters in 'n', then the program stops running.

    I know I need to put all that code in a do..while loop, but I don't know how. And how do I do the thing with the sentinel? Can anybody help please?
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,381
    Rep Power
    1871
    Try to get this program working
    Code:
    void showMultTable ( ) {
        cout << "This is my table" << endl;
        // hint, put most of your current main in here, after the Y/N
        // bit is working OK
    }
    
    int main ( ) {
      char input;
      do {
        showMultTable();
        cout << "Again" ;
        cin >> input;
      } while ( input == 'y' );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    45
    Rep Power
    13
    Originally Posted by salem
    Try to get this program working
    Code:
    void showMultTable ( ) {
        cout << "This is my table" << endl;
        // hint, put most of your current main in here, after the Y/N
        // bit is working OK
    }
    
    int main ( ) {
      char input;
      do {
        showMultTable();
        cout << "Again" ;
        cin >> input;
      } while ( input == 'y' );
    }
    Sorry, I don't understand what you did on top there. I never learned that. You have a void showMultTable on top. That looks like a function to me. I'm confused. :confused:
  6. #4
  7. No Profile Picture
    Redpill
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2005
    Posts
    1,660
    Rep Power
    151
    Do the entire multiplication table thing in the subfunction, and put the do...while loop in main(). Salem is suggesting you move the entire number input and table output to showMultTable, but you could leave it in main() inside the do...while loop too.
    Code:
    #include <stdio.h>
    int main(int o,char**O){return o>-1?o-2||!main(-1,1+O)?!!fprintf(stderr,"%s [0-"
    "9]{81}\n",*O):main(-83,++O):o>-83?(*O)[-1-o]?81==(o=-o-1)||o[*O]<'0'||'9'<o[*O]
    ?0:main(-2-o,O):o==-82:o>-164?(*O)[-83-o]<'1'?main(o-82,O):main(--o,O):o+164?o>-
    246?(*O)[-165-o]<'1'?main(o-82,O):main(--o,O):o+246?o>-328?(*O)[o=-o-247]<='8'?(
    main(-328-o,(++o[*O],O)),main(-247-o,O)):!(o[*O]='0'):(o=-o-328)<729?(o%9/3*3-o%
    27+o/243*9+o/81%3&&(*O)[o%81]==(*O)[o%81-o%27+o%9/3*3+o/243*9+o/81%3])||(o%81-o%
    9-o/81*9&&(*O)[o%81]==(*O)[o%9+o/81*9])||(o/81-o%9&&(*O)[o%81]==(*O)[o%81-o%9+o/
    81])?0:main(-409-o,O):main(-165-o%81,O):!puts(*O):0                           ;}

IMN logo majestic logo threadwatch logo seochat tools logo