|
-
September 20th, 2008, 02:49 PM
#1
I need help with this simple calculation!!!
ok here is the code:
Code:
#include <iostream>
using namespace std;
int main()
{
int t1;
int t2;
int sum;
int div;
int div1;
int divided;
{
int t1;
cout<<" this will add 2 values of test grades you have entered" <<endl;
cout<<"enter the value of the first test " <<endl;
cin>> t1;
cin.ignore();
cout<<"You enetered " << t1 << endl;
int t2;
cout<<"enter the value of the second test " <<endl;
cin>> t2;
cin.ignore();
cout<<"you have entered " << t2 << endl;
// simple cacl by J-C
int sum;
cout<<"THIS IS THE SUM " << t1 + t2 << endl;
int div;
cout<<"ENter the number you want to be divided by " <<endl;
cin>> div;
cin.ignore();
cout<<" you have entered " << div << "press enter to see the answer" <<endl;
}
{ int div1;
div1 = ( sum / div );
cin.ignore();
}
{
int divided;
cout<<"the answer is " << divided << endl;
cin.ignore();
}
}
i just want it to divide SUM ( which is t1+t2 ) by the div1 which is the number the user inputs..... can anyone help me why does it keep saying is 0?
-
September 20th, 2008, 03:22 PM
#2
Re: I need help with this simple calculation!!!
Note that in integer division the fractional portion is truncated, so 1/2 is evaluated to 0.
-
September 20th, 2008, 03:34 PM
#3
Re: I need help with this simple calculation!!!
 Originally Posted by J-C
i just want it to divide SUM ( which is t1+t2 ) by the div1
In the code you posted, you do not divide by div1, you divide by div and store the result in div1. You then output the value of the uninitialized variable divided. Uninitialized variables may contain any value, which MIGHT happen to be zero, but could also be the phone number of your town comptroller. Get the drift?
The problem noted by laserlight is something you should definitely be aware of, though.
- Alon
-
September 20th, 2008, 03:35 PM
#4
Re: I need help with this simple calculation!!!
then how about I'm putting in 10 + 10 the sum is = 20 ok that's right but then i put divide by 2 and it says is 0 how so? what do i need to change then?
BTW thanks a lot for the quick reply
In the code you posted, you do not divide by div1, you divide by div and store the result in div1. You then output the value of the uninitialized variable divided. Uninitialized variables may contain any value, which MIGHT happen to be zero, but could also be the phone number of your town comptroller. Get the drift?
The problem noted by laserlight is something you should definitely be aware of, though.
thanks for the reply and yes my bad i mean divide by div not div1 since div is the number the user inputs and div1 is supposed to give the result of SUM divided by div, which is not doing
and yes i am aware of what laserlight said as i replied to him before quoting you.
Last edited by J-C; September 20th, 2008 at 03:39 PM.
-
September 21st, 2008, 04:41 AM
#5
Re: I need help with this simple calculation!!!
Division is computer is totally different with normal daily calculation with integer.
-
September 21st, 2008, 08:26 AM
#6
Re: I need help with this simple calculation!!!
the problem is simple, you need to have this sentence in your code:
sum = t1+t2;
I think you must forget this sentence.
------------------------------------------------
codeuu,source code
Last edited by yulin11; September 21st, 2008 at 08:29 AM.
-
September 21st, 2008, 10:19 AM
#7
Re: I need help with this simple calculation!!!
You never output the result of the division in that code.
-
September 21st, 2008, 12:17 PM
#8
Re: I need help with this simple calculation!!!
THANKS FOR THE HELP GUYS 
i have another issue this one is a little harder:
Code:
a1:
// TITTLE
{
cout<<" SC J 1.0 (BETA) " <<endl;
cout<<endl;
cout<<"( simple calc J )"<<endl;
cout<< endl;
}
// Enter the first value
cout<<"First Number: ";
cin>> a1;
cout<<endl;
cout<<"Press: + to add, - to subtract, * to multiply and / to divide and press enter ";
cin >> a;
cout<<endl;
//if user inputs + then add
if ( a == '+' ) {cout<<"Number to ADD to " << a1 <<" : "; }
cin>> b1;
cout<<endl;
cout<<" Answer " << a1 <<" + "<< b1 <<" = " << a1 + b1 <<endl;
cout<<endl;
system("pause");
cout<<endl;
cout<<endl;
{ goto a1; }
// if user inputs - then substract
if ( a == '-' ) { cout<<" Number to subtract from: " << a1 <<": "; }
cin>> b1;
cout<<endl;
cout<<" Answer: "<< a1 << " - " << b1 <<" = " << a1-b1<<endl;
cout<<endl;
system("pause");
cout<<endl;
cout<<endl;
{ goto a1; }
// if user inputs * then multiply
if ( a == '*' ) {
cout<<" Number to multiply by: "<< a1 <<" : "; }
cin>> b1;
cout<<endl;
cout<<" Answer " << a1 <<" * " << b1 <<" = " << a1*b1 <<endl;
cout<<endl;
system("pause");
cout<<endl;
cout<<endl;
{ goto a1; }
// if user inputs / then divide
if ( a == '/' ) {
cout<<" Nmber to divide " << a1 <<" by " <<" : ";
cin>> a1;
cout<<endl;
cout<<" Answer " << a1 <<" / " << b1 << " = " << a1 / b1 <<endl; }
cout<<endl;
system("pause");
cout<<endl;
cout<<endl;
{ goto a1; }
btw is a calculator ...... pretty obvious
you don't have to read all code, it works and everything but i want it to do 2 things:
first : i want to make so that if you choose any character but
Code:
'+', '-', '*', '/';
it will return you to label a1:
i tried this
Code:
if (a != '+', '-', '*', '/' )
goto a1;
isn't this supposed to work? cause it is not.......
SECOND: the adding and subtracting work like a charm, but then the multiplying and dividing don't work at all if i press * does not do what it is supposed to here it is
Code:
// if user inputs * then multiply
if ( a == '*' ) {
cout<<" Number to multiply by: "<< a1 <<" : "; }
cin>> b1;
cout<<endl;
cout<<" Answer " << a1 <<" * " << b1 <<" = " << a1*b1 <<endl;
cout<<endl;
system("pause");
cout<<endl;
cout<<endl;
{ goto a1; }
// if user inputs / then divide
if ( a == '/' ) {
cout<<" Nmber to divide " << a1 <<" by " <<" : ";
cin>> a1;
cout<<endl;
cout<<" Answer " << a1 <<" / " << b1 << " = " << a1 / b1 <<endl; }
cout<<endl;
system("pause");
cout<<endl;
cout<<endl;
{ goto a1; }
so why isn't working while this one is:
Code:
if ( a == '+' ) {cout<<"Number to ADD to " << a1 <<" : "; }
cin>> b1;
cout<<endl;
cout<<" Answer " << a1 <<" + "<< b1 <<" = " << a1 + b1 <<endl;
cout<<endl;
system("pause");
cout<<endl;
cout<<endl;
{ goto a1; }
-
September 21st, 2008, 12:32 PM
#9
Re: I need help with this simple calculation!!!
Don't use gotos. Never, ever, *ever* use gotos. (Unless you can demonstrate decisively that they simplify program logic over the alternatives. Which you almost never can really do.)
The correct way to write this would be:
Code:
do {
{
cout<<" SC J 1.0 (BETA) " <<endl;
cout<<endl;
cout<<"( simple calc J )"<<endl;
cout<< endl;
}
// Enter the first value
cout<<"First Number: ";
cin>> a1;
cout<<endl;
cout<<"Press: + to add, - to subtract, * to multiply and / to divide and press enter ";
cin >> a;
cout<<endl;
//if user inputs + then add
if ( a == '+' )
{
stuff here
}
else if ( a == '-' )
{
//stuff here
}
else if ( a == '*' )
{
// stuff here
}
else if ( a == '/' )
{
// stuff here
}
} while (1); // or whatever condition you want to terminate the loop, maybe a particular character.
Code:
if (a != '+', '-', '*', '/' )
This is incorrect use of the comma operator. It does not do what you think it does. In general the correct way to write this (unnecessary in the above example, BTW) would be:
Code:
if (a != '+' && a != '-' && a != '*' && a != '/')
Although, in the particular case where you're trying to match one of several chars you could instead just write
Code:
if (strchr("+-*/",a) == NULL)
Last edited by Lindley; September 21st, 2008 at 12:39 PM.
-
September 21st, 2008, 01:00 PM
#10
Re: I need help with this simple calculation!!!
:P :P :P :P
LOL i have heard and actually found out for my self that goto is something to be avoided AT ALL times, well it just got proved xD thanks so much man
oh and it does make more sense to use && ..... i just didn't think about it then thanks again so much man you are THE BEST 
EDIT: ok a little bit of blindness here, i really think i'm going blind...
Code:
while (a != something)
{
cout<<" YOU PRESSED A WRONG SYMBOL THAT IS NOT IN SC J 1.0 (BETA) "<<endl;
cout<<endl;
cout<<endl;
system("PAUSE");
}
i really i'm blind cause i can't see what makes it not show the message.....
if i put the semicolon after ) then ok it loops but before it loops i want it to show the message.
Last edited by J-C; September 21st, 2008 at 01:45 PM.
-
September 21st, 2008, 01:45 PM
#11
Re: I need help with this simple calculation!!!
Can't say without more context. In isolation, that looks fine. Well, except for being an infinite loop.
-
September 21st, 2008, 01:49 PM
#12
Re: I need help with this simple calculation!!!
thanks for helping Lindley i really appreciate
Code:
do
{ // begins do while loop
// TITTLE
{
cout<<" SC J 1.0 (BETA) " <<endl;
cout<<endl;
cout<<"( simple calc J )"<<endl;
cout<< endl;
}
if ( a == '+' )
{
cout<<"Number to ADD to " << a1 <<" : ";
cin>> b1;
cout<<endl;
cout<<" Answer " << a1 <<" + "<< b1 <<" = " << a1 + b1 <<endl;
cout<<endl;
system("pause");
cout<<endl;
cout<<endl;
}
} //end of do while loop
while (a != something)
{
cout<<" YOU PRESS A WRONG SYMBOL THAT IS NOT IN SC J 1.0 (BETA) "<<endl;
cout<<endl;
cout<<endl;
system("PAUSE");
}
that's not the whole code but i think that sums it up if you want to see the full code just tell me
Last edited by J-C; September 21st, 2008 at 01:52 PM.
-
September 21st, 2008, 02:19 PM
#13
Re: I need help with this simple calculation!!!
Code:
do
{ // begins do while loop
// TITTLE
{
cout<<" SC J 1.0 (BETA) " <<endl;
cout<<endl;
cout<<"( simple calc J )"<<endl;
cout<< endl;
}
if ( a == '+' )
{
cout<<"Number to ADD to " << a1 <<" : ";
cin>> b1;
cout<<endl;
cout<<" Answer " << a1 <<" + "<< b1 <<" = " << a1 + b1 <<endl;
cout<<endl;
system("pause");
cout<<endl;
cout<<endl;
}
else if (...)
else if (...)
else if (a != 'q')
{
cout<<" YOU PRESS A WRONG SYMBOL THAT IS NOT IN SC J 1.0 (BETA) "<<endl;
cout<<endl;
cout<<endl;
system("PAUSE");
}
} while (a != 'q'); // 'q' for quit
I think that's what you meant.
-
September 21st, 2008, 02:31 PM
#14
Re: I need help with this simple calculation!!!
LOL that is totally what i meant and that works just fine... i just thought that
Code:
while (a!=something)
{
//code to execute here
}
could still display a message...guess not?
an i have another question
what is the simplest way to say if a cin>> does not equal a number
like if the input is anything but a number to show a message i want
Last edited by J-C; September 21st, 2008 at 02:38 PM.
-
September 21st, 2008, 02:34 PM
#15
Re: I need help with this simple calculation!!!
 Originally Posted by J-C
LOL that is totally what i meant and that works just fine... i just thought that
Code:
while (a!=something)
{
//code to execute here
}
could still display a message...guess not?
It can; just don't get confused between the while which *opens* a while loop and the while which *closes* a do/while loop. The latter can't have a block after it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|