Hi, all. I am very new to C++ and am close to losing my mind with the homework. We are asked to generate a multiplication table using a 'for statement'. User is to input a number between 1-12; the program will generate a multiplication table and should loop for no more than 15 times.


Sample table should look like this:

Multiplier         Multiplicand         Product
6                    1                      6
6                    2                      12
6                    3                      18
6                    4                      24
etc.

My code looks like this:

#include <fstream>
#include <iostream>
#include <stdio.h>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
//*****MAIN*****
int main(void)
{
  int         choice,            //output choice
    val_1,             //first number [multiplier]
    x,                 //multiplicand
    result;            //x * val_1

  char        name[21];          //name
  ofstream    outfile;           //to write to an output file

  //*****INPUT SECTION*****
  system ("cls");
  cout << "Enter name:  ";
  cin.getline(name, 21);
  cout << "Enter number between 1 through 12:  ";
  cin >> val_1;
  //*****PROCESS SECTION*****
  for ( int x = 1; x<=15; x++ )
  {result = x * val_1;}


  //OUTPUT SECTION
  system ("cls");
  cout << "Output Choice: 1 (Screen) 2 (File), choose 1 or 2: ";
  cin >> choice;
  if(choice==2)
    outfile.open("f.multitable.dta");
  else
    outfile.open("con");
  system ("cls");
  outfile << setiosflags(ios::showpoint|ios::fixed)<< setprecision(2);
  outfile << "Name: " << name << endl << endl << endl << endl;
  outfile << setw(45) << "Multiplication Table" << endl;
  outfile << setw(45) << "====================" << endl << endl;
  outfile << setw(20) << "Multiplier" << setw(20) << "Multiplicand" << setw(20)<<"Product" << endl;
  outfile << setw(20) << val_1 << setw(20) << x << setw(20) << result << endl;
  outfile.close();
  return 0;
}

Code formatted and tags added. -Narue

Any help would be most appreciated. It is due in class tomorrow [Friday, 4/22 at 7:00 p.m.] Thanks!!!!

Instead of putting all of that framework around your loop, first get a simple loop working so that you know what's needed:

#include <iomanip>
#include <iostream>

using namespace std;

void table_row ( int multiplier )
{
  for ( int i = 1; i <= 12; i++ )
    cout<< setw ( 4 ) << i * multiplier;
  cout<<endl;
}

int main()
{
  for ( int i = 1; i <= 12; i++ )
    table_row ( i );
}

VERY NEW?!?!?

I took a full credit course on C++ wrote my exam and everything, And I couldnt hope to make a program like that without loads of help, and the power of ctrl c and ctrl v and the web. New at it, PAH!

VERY NEW?!?!?

I took a full credit course on C++ wrote my exam and everything, And I couldnt hope to make a program like that without loads of help, and the power of ctrl c and ctrl v and the web. New at it, PAH!

Some people (like myself) are shifting over from languages like Pascal, VB and Java to learn C++ after those kind of courses. I'm one of the Java-types and I am admittedly very new at C++. Some people are the same way.

Also, keep in mind that programming takes a while to click for people. Once you get it though it's very difficult to forget. You're literally learning a new kind of language (you're "talking" to the computer using computer-understood syntax via a programming language, of course it's going to be hard to understand! =P ).

This article has been dead for over six months. Start a new discussion instead.