This code is meant to keep prompting for user input (integers), and when 0 is pressed it is meant to view the product of all the entered numbers apart from 0.
It works fine, but when I hit 0 to view the product, it shows 0.
Here's the code: - if (a == 10)
-
{
-
int l;
-
int m;
-
cout<< "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \n \n";
-
cout << "Activated multiplying function... \n \n";
-
while (1){
-
cout << "Enter one number to multiply ( 0 to find total ): \n";
-
cin >> l;
-
m = m*l;
-
if (l == 0)
-
{
-
cout << "PRODUCT:" << endl;
-
cout << m << endl;
-
cout << endl;
-
cout<< "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \n \n";
-
break;
-
}
-
}
-
}
Can anyone see the problem here, and how I can fix it?
Thanks
:P
14 1529
This code is meant to keep prompting for user input (integers), and when 0 is pressed it is meant to view the product of all the entered numbers apart from 0.
It works fine, but when I hit 0 to view the product, it shows 0.
Here's the code: - if (a == 10)
-
{
-
int l;
-
int m;
-
cout<< "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \n \n";
-
cout << "Activated multiplying function... \n \n";
-
while (1){
-
cout << "Enter one number to multiply ( 0 to find total ): \n";
-
cin >> l;
-
m = m*l;
-
if (l == 0)
-
{
-
cout << "PRODUCT:" << endl;
-
cout << m << endl;
-
cout << endl;
-
cout<< "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \n \n";
-
break;
-
}
-
}
-
}
Can anyone see the problem here, and how I can fix it?
Thanks
:P
I see your problem here. You are multiplying the breaking 0 with the total. Anything multiplied by 0 is 0. But anything multiplied by 1 , is the same thing. so try doing a bit of manipulation to the code : )
- cin >> l;
-
m = m*l;
-
if (l == 0)
-
{
-
// display m ...
-
}
Can anyone see the problem here, and how I can fix it?
Can't you see the problem? Suppose I enter 0, so zero is assigned to l ('ell')
and m is multiplied by ell (i.e. zero (0)) ... and then ell is tested for zero.
Somone already warned you for this logic mistake w.r.t. sentinel values but I
bet you didn't even read it.
kind regards,
Jos
so what i need to do is make it ignore the value 0 in the multiplication formula.
How could I do that?
so what i need to do is make it ignore the value 0 in the multiplication formula.
How could I do that?
Don't multiply by ell when ell equals zero.
kind regards,
Jos
and it is because I am using a previous code from a program that does the same thing but with addition.
Now ive realized in addition, adding value 0 would give the same output
anyway back to how to make it ignore value 0, see post above
Yeah I was just about to point out
Killer42's Avatar
Killer42
Moderator
5,108 Posts
Yesterday
10:09 PM
#41
Re: The addition problem
Quote:
Originally Posted by Hunderpanzer
...
Code: ( cpp )
1.
while (1){
2.
cout << "Enter numbers to add ( 0 to total )";
3.
cin >> n;
4.
sum = sum + n;
5.
if (n == 0){
6.
cout << sum ;
7.
break;
8.
}
Just as a general point, CyBerFirEZ. In this sort of situation it will usually make more sense to do your "if blah then break" processing before you add to your total. I'm aware that it doesn't make any practical difference in this case, since you're adding zero. It's just something I would recommend keep in mind for future loops. It's generally preferable to avoid any unnecessary processing, for reasons of efficiency among others.
so then how do i make the forumla ignore the value 0?????
so then how do i make the forumla ignore the value 0?????
C and C++ do have an if statement and an optional else clause you know ...
kind regards,
Jos
Expanbd your if statement.
If ell is 0, display the total else do the calculation.
BTW, you realize, I hope that in the Courier font, the number one and the letter ell are the same symbol making your code imposssible to read.
Yeah I was just about to point out
Yep that was the tip; credits to Killer42 for the useful tip and you, Hunderpanzer
for retrieving it. thanks.
kind regards,
Jos
edit: after re-reading that tip text I'm not sure anymore *who* actually supplied
the tip ;-)
Jos
edit: after re-reading that tip text I'm not sure anymore *who* actually supplied
the tip ;-)
Haha?
Crazyfire the answer you need is already been said here. Just read slower, and think harder : )
Haha?
No, no 'haha'; your post #7 doesn't clearly show who actually supplied the tip
and that's why I added my 'edit' paragraph, that's all.
kind regards,
Jos
No, no 'haha'; your post #7 doesn't clearly show who actually supplied the tip
and that's why I added my 'edit' paragraph, that's all.
kind regards,
Jos
I may be a bit confused. It said killer42 at the top ?
I'll just let it go :P
I may be a bit confused. It said killer42 at the top ?
I'll just let it go :P
Yep, it said Killer42 at the top but it doesn't clearly tell where the (nested)
quote ends; and yep, I'll let go of it as well; thanks both of you anyway also
on behalf of the OP (I guess?)
kind regards,
Jos
ps. man this is a chaotic thread ...
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Andy |
last post by:
Hi,
I don't know if this is the correct group to post this, but
when I multiply a huge floating point value by a really
small (non-zero) floating point value, I get 0 (zero) for the result.
This...
|
by: Ralf Hildebrandt |
last post by:
Hi all!
First of all: I am a C-newbie.
I have noticed a "strange" behavior with the standart integer
multiplication. The code is:
void main(void)
|
by: Vijay Kumar R Zanvar |
last post by:
Hi,
Why multiplication of pointers is not allowed?
Till now I only know this, but not the reason why!
PS: As a rule, I searched the FAQ, but could not
find an answer.
--
Vijay Kumar R...
|
by: Christopher Dyken |
last post by:
Hi group,
I'm trying to implement two routines to handle 32x32-bits and
64x64-bits signed integer multiplication on a 32 bits machine in C. It
easy to find descriptions of non-signed...
|
by: TJ Doherty |
last post by:
Need help understanding the following please:
When I am creating a project and code my connection using Dim connectString
As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data...
|
by: amitnanda |
last post by:
Hi Guys,
I have a matrix multiplication program in C that multiplies two
matrices.
When their size is 3*3 or 800*800, the program runs fine. But above
that size, I get a "segmentation fault"....
|
by: VijaKhara |
last post by:
Hi all,
Is there any method which can implememt the matrix multiplication
faster than using the formula as we often do by hand?
I am writing the following code and my matrice: one is 3x40000 and...
|
by: helPlease |
last post by:
hello !! Can anybody provide me an algorithm or C code for long
integer multiplication.It's urgent.
|
by: vbwire |
last post by:
hi.. im new to programming world.im a first year student in computer graphics. i just curious how to multiply 3 matrices in a coding. i just can do the 2 matrices multiplication.but it is not a good...
|
by: Sozos |
last post by:
Hi guys. I have a problem with writing the base case for the following matrix multiplication function I have implemented. Please help.
#define index(i,j,power) (((i)<<(power))+(j))
void...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |