Mulitplication Table in C++ with Loops

  • Comp Sci
  • Thread starter Windowmaker
  • Start date
  • #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;
}
 

Answers and Replies

  • #2
197
1
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 dont even have my book, i left it at school and its locked :/
 
  • #4
197
1
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, im getting discouraged. Im going to bed now.
 
  • #6
197
1
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
764
71
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
197
1
Ahh! great catch! Thank you for pointing that out Mister X!
 

Related Threads on Mulitplication Table in C++ with Loops

  • Last Post
Replies
2
Views
2K
Replies
6
Views
3K
Replies
6
Views
5K
Replies
0
Views
3K
Replies
2
Views
1K
  • Last Post
Replies
5
Views
5K
  • Last Post
Replies
23
Views
3K
Replies
18
Views
9K
Replies
6
Views
2K
Top