Comp Sci Mulitplication Table in C++ with Loops

  • Thread starter Thread starter Windowmaker
  • Start date Start date
  • Tags Tags
    C++ Loops Table
AI Thread Summary
The discussion focuses on creating a C++ program to generate a multiplication table based on user input. The initial code provided has several issues, including undeclared variables and incorrect loop syntax. Participants suggest using a for loop instead of a while loop for better functionality and emphasize the need to display both the multiplication equation and the result. Corrections are made to the syntax of the main function and variable declarations. Overall, the conversation highlights the importance of proper coding practices and syntax in C++.
Windowmaker
Messages
68
Reaction score
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
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}
 
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 :/
 
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:
Thank you for help, I am getting discouraged. I am going to bed now.
 
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.
 
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.
 
Ahh! great catch! Thank you for pointing that out Mister X!
 

Similar threads

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