Mulitplication Table in C++ with Loops

  • Context: Comp Sci 
  • Thread starter Thread starter Windowmaker
  • Start date Start date
  • Tags Tags
    C++ Loops Table
Click For Summary

Discussion Overview

The discussion revolves around writing a C++ program to generate a multiplication table based on user input. Participants are addressing issues related to code syntax, logic, and structure, with a focus on loops and output formatting.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant shares an initial code attempt but has issues with undeclared variables and output formatting.
  • Another participant suggests using a for loop instead of a while loop for better control of iterations.
  • There is a need to display both the multiplication expression (e.g., "1 x 5") and the result, which some participants note is missing in the original code.
  • Corrections are made regarding the syntax of the main function and the use of semicolons versus commas in loop declarations.
  • Concerns are raised about potential infinite loops due to improper loop conditions and variable initialization.
  • One participant expresses frustration and discouragement about the coding process.
  • Another participant offers guidance and asks for clarification on specific problems to provide better assistance.

Areas of Agreement / Disagreement

Participants generally agree on the need for corrections in the code, but there are multiple competing views on how to implement the necessary changes effectively. The discussion remains unresolved regarding the final implementation of the program.

Contextual Notes

Limitations include undeclared variables, incorrect loop syntax, and potential infinite loops due to the lack of variable updates within loops. The discussion does not resolve these issues definitively.

Who May Find This Useful

Readers interested in C++ programming, particularly those learning about loops, variable declarations, and output formatting in console applications.

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

{

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


while ( i < n + 1);

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

count << "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 2 ·
Replies
2
Views
3K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 7 ·
Replies
7
Views
2K