Click to See Complete Forum and Search --> : c++ please help


feierstarter1
April 18th, 2006, 12:06 AM
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


/*
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]

humptydumpty
April 18th, 2006, 12:23 AM
what's the Problem.change your else part to else if and use following code which will check this for you.

if((va>0 || val<=100 ) && (val!=999))
{

}
else
break;


Thankyou.

sreehari
April 18th, 2006, 12:48 AM
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;
}

Graham
April 18th, 2006, 05:12 AM
Careful, guys, you've swapped the comparison operators round...

val > 0 || val <= 100

is always true.

NMTop40
April 18th, 2006, 05:35 AM
there's also far too much code in main

sreehari
April 18th, 2006, 06:12 AM
Careful, guys, you've swapped the comparison operators round...

val > 0 || val <= 100

is always true.
Well oops :wave:
Thanks Graham, guess it just slipped out :p

feierstarter1
April 18th, 2006, 07:54 AM
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

NMTop40
April 18th, 2006, 08:43 AM
that isn't what you have in the last version of your code posted above.

feierstarter1
April 19th, 2006, 04:33 AM
Hi
i just wrote what i changed and it doesnt work and i didnt understand the change if else..


/*
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;
}