|
-
April 18th, 2006, 12:06 AM
#1
c++ please help
hi,
i have this program!
I enter numbers until 999(exit)
if i enter <0 and >100 it shows " number must be between 0 and 100.."
thats fine!!
but if i enter 999 for exit the program than i dont want that message
How can i fix it??
Second problem is calculate the factorial, it isnt working, Where is my mistake? Example i enter 4 it shows fac 6 but it should be 24
thanks
Code:
/*
sum of first and last numbers
average of first three numbers
product of last two numbers
largest of all numbers
greatest common divisor (gcd) of last two numbers
(gcd of two integers is the largest integer that evenly divides each of the numbers)
factorial of last number
(the factorial of a nonnegative integer number (n) is the product n*(n-1)*(n-2)..1;
for example factorial(4)=4*3*2*1=24)
grade for all numbers (H:85-100, D:75-84, C:65-74, P:50-64, F:0-49)
*/
#include<iostream>
using namespace std;
int main()
{
// variable declaration
long fac =0;
int count =0,data=0;
int first =0,second_last=0,last=0,total=0,largest=0;
int second=0,third=0,val=0;
int i;
// read value from key board
do
{
cout<<"\nPlease Enter a Number( 999 to exit): ";
cin>>val;
/*
Here is the problem. I tried if(val<0 || val>100 & val!=999)
but is not working, the program shows everytime the cout: The Number must be..."
if i enter 999 i would like the program doesnt show the sentence because is the exit number
How can i fix it??
*/
if(val<0 || val>100 )
{
cout<<"\nThe Number must be between 0 and 100, Try again!";
continue;
}
else
{
cout<<"The Grade from your number " << val <<" is: ";
if(val>=85)
cout<<"H";
else if(val>=75 && val<=84)
cout<<"D";
else if(val>=65 && val<=74)
cout<<"C";
else if(val>=50 && val<=64)
cout<<"P";
else
cout<<"F";
cout<<"\n";
}
// set count
count++;
// first value
if(count==1)
{
first=val;
total+=val;
largest =val;
}
// all other values
else if(val!=999){
// second last
second_last = last;
//last
last =val;
// sum of all values
total+=val;
// find largest values
if(largest<val)
largest = val;
// get second value
if(count==2)
second = val;
else if(count==3) //third value
third = val;
}
}while(val!=999);
// find factorial
fac =1;
for(int i=1;i<last;i++)
fac*=i;
int gcd = last;
// find gcd of last two numbers
if(gcd>second_last)
gcd = second_last;
for(i=gcd;gcd>1;i--)
{
gcd=i;
if( (last%gcd)==0 && (second_last%gcd)==0)
break;
}
cout<<"\n";
// display data
/*
the sum of first and last numbers,
average of first three numbers,
product of last two numbers,
largest of all numbers,
the greatest common divisor of last two numbers,
the factorial of last number and
the grade for all numbers preceding 999
*/
cout<<"\nThe sum of first and last numbers : "<<(first+last);
cout<<"\nThe average of first three numbers : "<<((first+second+third)/3);
cout<<"\nThe product of last two numbers : "<<(last*second_last);
cout<<"\nThe largest of all numbers : "<<largest;
cout<<"\nThe GCD of last two numbers : "<<gcd;
cout<<"\nThe factorial of last number : "<<fac;
//cout<<"\n\nThe grade for all numbers : ";
cout<<"\n";
// pause
getchar();
getchar();
// return
return 0;
}
[/QUOTE]
-
April 18th, 2006, 12:23 AM
#2
Re: c++ please help
what's the Problem.change your else part to else if and use following code which will check this for you.
Code:
if((va>0 || val<=100 ) && (val!=999))
{
}
else
break;
Thankyou.
Last edited by humptydumpty; April 18th, 2006 at 12:30 AM.
-
April 18th, 2006, 12:48 AM
#3
Re: c++ please help
Another way would be to add another if condition inide the if condition as follows
if((va>0 || val<=100 ) )
{
if(val==999)
{
// exit the program
}
cout<<"\nThe Number must be between 0 and 100, Try again!";
continue;
}
- Sreehari
"Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
" Everybody is sent to Earth on a purpose. I am so Lagging behind that i won't die." – Calvin
-
April 18th, 2006, 05:12 AM
#4
Re: c++ please help
Careful, guys, you've swapped the comparison operators round...
Code:
val > 0 || val <= 100
is always true.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
April 18th, 2006, 05:35 AM
#5
Re: c++ please help
there's also far too much code in main
-
April 18th, 2006, 06:12 AM
#6
Re: c++ please help
 Originally Posted by Graham
Careful, guys, you've swapped the comparison operators round...
Code:
val > 0 || val <= 100
is always true.
Well oops
Thanks Graham, guess it just slipped out
- Sreehari
"Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
" Everybody is sent to Earth on a purpose. I am so Lagging behind that i won't die." – Calvin
-
April 18th, 2006, 07:54 AM
#7
Re: c++ please help
HI people,
thanks for your help
but if i would know the "What is the problem" i wouldnt ask here.
im a beginner and not a senior in c++
think about you all started once!!
ok, i still dont understand the change else if??
could you give me an example please?!
i just wrote
if((va>0 || val<=100 ) && (val!=999))
{
output
}
else
and it doesnt do what i want, it does still show the sentence "...try again"
thanks for your help
-
April 18th, 2006, 08:43 AM
#8
Re: c++ please help
that isn't what you have in the last version of your code posted above.
-
April 19th, 2006, 04:33 AM
#9
Re: c++ please help
Hi
i just wrote what i changed and it doesnt work and i didnt understand the change if else..
Code:
/*
sum of first and last numbers
average of first three numbers
product of last two numbers
largest of all numbers
greatest common divisor (gcd) of last two numbers
(gcd of two integers is the largest integer that evenly divides each of the numbers)
factorial of last number
(the factorial of a nonnegative integer number (n) is the product n*(n-1)*(n-2)..1;
for example factorial(4)=4*3*2*1=24)
grade for all numbers (H:85-100, D:75-84, C:65-74, P:50-64, F:0-49)
*/
#include<iostream>
using namespace std;
int main()
{
// variable declaration
long fac =0;
int count =0,data=0;
int first =0,second_last=0,last=0,total=0,largest=0;
int second=0,third=0,val=0;
int i;
// read value from key board
do
{
cout<<"\nPlease Enter a Number( 999 to exit): ";
cin>>val;
/*
Here is the problem. I tried if(val<0 || val>100 & val!=999)
but is not working, the program shows everytime the cout: The Number must be..."
if i enter 999 i would like the program doesnt show the sentence because is the exit number
How can i fix it??
*/
//if(val<0 || val>100
if((val<0 || val>100) && (val!=999))
{
cout<<"\nThe Number must be between 0 and 100, Try again!";
continue;
}
else
{
cout<<"The Grade from your number " << val <<" is: ";
if(val>=85)
cout<<"H";
else if(val>=75 && val<=84)
cout<<"D";
else if(val>=65 && val<=74)
cout<<"C";
else if(val>=50 && val<=64)
cout<<"P";
else
cout<<"F";
cout<<"\n";
}
// set count
count++;
// first value
if(count==1)
{
first=val;
total+=val;
largest =val;
}
// all other values
else if(val!=999){
// second last
second_last = last;
//last
last =val;
// sum of all values
total+=val;
// find largest values
if(largest<val)
largest = val;
// get second value
if(count==2)
second = val;
else if(count==3) //third value
third = val;
}
}while(val!=999);
// find factorial
fac =1;
for(int i=1;i<last;i++)
fac*=i;
int gcd = last;
// find gcd of last two numbers
if(gcd>second_last)
gcd = second_last;
for(i=gcd;gcd>1;i--)
{
gcd=i;
if( (last%gcd)==0 && (second_last%gcd)==0)
break;
}
cout<<"\n";
// display data
/*
the sum of first and last numbers,
average of first three numbers,
product of last two numbers,
largest of all numbers,
the greatest common divisor of last two numbers,
the factorial of last number and
the grade for all numbers preceding 999
*/
cout<<"\nThe sum of first and last numbers : "<<(first+last);
cout<<"\nThe average of first three numbers : "<<((first+second+third)/3);
cout<<"\nThe product of last two numbers : "<<(last*second_last);
cout<<"\nThe largest of all numbers : "<<largest;
cout<<"\nThe GCD of last two numbers : "<<gcd;
cout<<"\nThe factorial of last number : "<<fac;
//cout<<"\n\nThe grade for all numbers : ";
cout<<"\n";
// pause
getchar();
getchar();
// return
return 0;
}
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
|