Mulitplication Table in C++ with Loops

In summary: I'll fix it up now.In summary, Jevin Barnett's multiplcation table allows for entry of a number to multiply by and displays the results. There are problems with the code, including incorrect syntax for the main function and an infinite loop.
  • #1
Windowmaker
68
0
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;
}
 
Physics news on Phys.org
  • #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}
 
  • #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 don't even have my book, i left it at school and its locked :/
 
  • #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:
  • #5
Thank you for help, I am getting discouraged. I am going to bed now.
 
  • #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.
 
  • #7
Ivan92 said:
for (int i = 0, i<n, i++)
{CODE HERE}

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:
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:
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.
 
  • #8
Ahh! great catch! Thank you for pointing that out Mister X!
 

FAQ: Mulitplication Table in C++ with Loops

What is a multiplication table in C++?

A multiplication table in C++ is a table that displays the product of two numbers. It typically includes a row and column of numbers, with the product of each combination of numbers displayed in the intersecting cells.

How do you create a multiplication table in C++?

To create a multiplication table in C++, you can use nested for loops. The outer loop controls the rows of the table, while the inner loop controls the columns. Within the nested loops, you can use the multiplication operator (*) to calculate the product of the row and column numbers.

What are the benefits of using loops to create a multiplication table in C++?

Using loops to create a multiplication table in C++ is beneficial because it allows for a more efficient and concise code. Instead of manually calculating and printing each product, the loops can automatically handle this task for a specified range of numbers. This saves time and reduces the chance of errors.

Can you customize the multiplication table in C++?

Yes, you can customize the multiplication table in C++ by changing the range of numbers, the formatting of the table, and the output of the products. You can also add labels or headings to make the table more user-friendly.

Are there other methods for creating a multiplication table in C++?

Yes, there are other methods for creating a multiplication table in C++. You can use arrays, functions, or object-oriented programming to generate a multiplication table. However, using loops is the most common and efficient approach.

Similar threads

Replies
3
Views
970
Replies
2
Views
3K
Replies
3
Views
1K
Replies
3
Views
1K
Replies
6
Views
3K
Replies
7
Views
1K
Back
Top