1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Mulitplication Table in C++ with Loops

  1. Oct 5, 2011 #1
    I have to write a program in c++ that can be repeated as many times as needed. It should ask for a number to multply like

    Enter a number to multply: 5

    output is this

    1 x 5=5
    2 x 5=10
    3 x 5=15
    4 x 5 =20
    5 x 5 =25

    Enter a number to multiply : 8

    1 x 5=5
    2 x 5=10
    3 x 5=15
    4 x 5 =20
    5 x 5 =25
    6 x 5=30
    7 x 5=35
    8 x 5=40

    Run again : N




    My feebile attempt:

    // mommy.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    int main

    // Author: Jevin Barnett
    // Date Written: 05 Oct 2011
    // Program Description: Multiplcation Table



    // variable declarations

    int i, num, total;
    char choice = 'Y';
    do

    {

    cout << "Pick a Number to multiply \n. ";
    cin >> num;


    while ( i < n + 1);

    { total = i * n;
    cout << total;
    }

    cout << "Run again? /n.";

    cin >> choice:

    while (choice == 'Y' || choice == 'y')

    {
    return 0;
    }
     
  2. jcsd
  3. Oct 5, 2011 #2
    I don't see n declared anywhere in your program. Instead of running a while loop, your best bet would be to run a for loop instead. Also you are not displaying the 1 x 5 part. You are only displaying the results.

    Also, be sure to fix int main. It is int main(){CODE HERE}
     
  4. Oct 5, 2011 #3
    My professor wants the 1 x 5 part displayed lol. I fix the undeclared "n" to num. I'm not following the loop at all. I dont even have my book, i left it at school and its locked :/
     
  5. Oct 5, 2011 #4
    What I meant was that you need to fix your code to display the 1x5 part. With the code you have displayed you are not displaying 1x5, instead you are just displaying the product. As for for-loops.

    for(intial value, condition, then increment)
    {CODE HERE}

    For example:

    [STRIKE]for (int i = 0, i<n, i++)
    {CODE HERE}[/STRIKE]

    Corrected version (Thanks MisterX for pointing it out)

    for (int i = 0; i<n; i++)
    {CODE HERE}
     
    Last edited: Oct 6, 2011
  6. Oct 5, 2011 #5
    Thank you for help, im getting discouraged. Im going to bed now.
     
  7. Oct 5, 2011 #6
    I am trying to guide you in the right direction. If you tell me what exactly is your problem, I could be of more assistance.
     
  8. Oct 5, 2011 #7
    This is incorrect. Those should be semicolons and not commas.

    Jevin, there are many problems with the code you posted. You did not use the correct syntax for the main function, as has already be posted.

    You need to give values to variables before they are used.

    Code (Text):
    while ( i < n + 1);
    The statement that would get executed with this loop is ';' - the statement that does nothing. If you want the loop to be the code in brackets after this, then you should remove the semicolon.

    Even if you did this, the code

    Code (Text):

    while ( i < n + 1)
    { total = i * n;
    cout << total;
    }
    has no change to i or n so if i < n + 1 was true, there would be an infinite loop.
     
  9. Oct 6, 2011 #8
    Ahh! great catch! Thank you for pointing that out Mister X!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Mulitplication Table in C++ with Loops
  1. C++- while-loop (Replies: 7)

Loading...