# Thread: Problem with condition in IF ELSE + cmath lib error

1. ## Problem with condition in IF ELSE + cmath lib error

Hi to everyone,
im working some exsercise for c++ and i have this code right here :
Code:
```#include <iostream>
#include<cstdlib>
using namespace std;
int main() {
int x,sum=0,a;
int num=(5*5*5);
cout<<"Imput number N : ";
cin>>x;
cout<<"\nThis program will compute the sum of the cubes from 5^3 to N^3.\n ";
cout<<endl;
system("PAUSE");
if(x>=5){
for(;x>=5;){
sum+=(x*x*x);
x--;
}
cout<<"Total sum is : "<<sum<<".";
}
else if(x<=5){
for(;x<=5;){
sum+=(x*x*x);
x++;
}
cout<<"Total sum is : "<<sum<<".";
}
else cout<<"Incorrect imput!";
return 0;
}```
it works fine UNTIL i enter a letter or word or anything else that isnt int type of number. In that case it pops out this same result :
"Total sum is :225" . How to handle this problem?
And aditional question is when i use this :

Code:
```#include <iostream>
#include<cmath>
using namespace std;
int main(){
int a=pow(5,3);
cout<<a<<endl;
}```
it shows 124 and im pretty sure its 125.
Im using software Dec-C++ version 5.5.1

2. For the first problem you can handle this with a condition:

Code:
```
if(isalpha(x))
{
cout<<"ERROR, only enter a number please"<<"\n";
}```

3. Originally Posted by jocdrew21
For the first problem you can handle this with a condition:

Code:
```
if(isalpha(x))
{
cout<<"ERROR, only enter a number please"<<"\n";
}```
this will not work for x declared as int
cin >> x will fail and x will left with old value instead of putting there ASCII code of first non-number character which could be tested with isalpha

4. Sorry I told you wrong...

5. jocdrew21 i tried your solution and it gives same outcome as before.
So i tried with a class stringstream (The c++ language tutorial recommend this method for all programs that are intensive in user imput) i tested it like this :

Code:
```#include <iostream>
#include <sstream>
using namespace std;
int main(){
int a;
string str;
cout<<"Enter a number : ";
getline (cin,str);
stringstream(str)>>a;
if(str<0)cout<<"You entered number lower than 0 .";
else if (str>=0)cout<<"You entered 0 or higher number .";
else cout<<"Isnt a number .";
return 0;
}```
and when i put anything else than a number it acts like i entered 0.
Is there a code that have the ability to see the difference between a number and other characters?

6. if(str<0)

you should check content of variable a not content of variable str

7. Code:
```#include <iostream>
#include <cmath>
#include<cstdlib>
using namespace std;
int main() {

int sum=0,x=0;
int num=0;

cout<<"Imput number N : ";
cin>>x;
if (cin.fail())
{
cerr << "\n\nSorry enter a number please";
}
else if (x<0)
{
cerr<<"\n\nSorry please only enter positive numbers";
}
else

cout<<"\nThis program will compute the sum of the cubes from 5^3 to N^3.\n ";
cout<<endl;

sum=pow(5, 3); cout<<"Total sum of 5^3 is : "<<sum<<".\n";

num=pow(5, x); cout<<"Total sum of 5^"<<x<<" is: "<<num<<".\n";

return 0;
}```
I looked at your code a little more and this is how I would have wrote it. Is this what you were trying to do?

8. Originally Posted by Pantheon303
So i tried with a class stringstream
(...)
and when i put anything else than a number it acts like i entered 0.
Is there a code that have the ability to see the difference between a number and other characters?
The problem is that you did not check to see if the conversion succeeded. For example:
Code:
```#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
cout << "Enter a number : ";
string str;
if (getline(cin, str))
{
stringstream ss(str);
int a;
if (ss >> a && ss.eof())
{
cout << "You entered: " << a << endl;
}
else
{
cout << "Isnt a number .";
}
}
return 0;
}```

9. Originally Posted by jocdrew21
Code:
```#include <iostream>
#include <cmath>
#include<cstdlib>
using namespace std;
int main() {

int sum=0,x=0;
int num=0;

cout<<"Imput number N : ";
cin>>x;
if (cin.fail())
{
cerr << "\n\nSorry enter a number please";
}
else if (x<0)
{
cerr<<"\n\nSorry please only enter positive numbers";
}
else

cout<<"\nThis program will compute the sum of the cubes from 5^3 to N^3.\n ";
cout<<endl;

sum=pow(5, 3); cout<<"Total sum of 5^3 is : "<<sum<<".\n";

num=pow(5, x); cout<<"Total sum of 5^"<<x<<" is: "<<num<<".\n";

return 0;
}```
I looked at your code a little more and this is how I would have wrote it. Is this what you were trying to do?
The exercise goes like this : Write a program that asks the user to type an integer N and computes the sum of the cubes from 53 to N3.
Its computes 5^3,N^3 and all between so thats not it, but thanks anyway

10. Code:
```#include <iostream>
#include <cmath>
#include<cstdlib>
using namespace std;
int main() {

int sum=0,x=0;
int num=0;

cout<<"Imput number N : ";
cin>>x;
if (cin.fail())
{
cerr << "\n\nSorry enter a number please";

}
else if (x<0)
{
cerr<<"\n\nSorry please only enter positive numbers";

}
else

cout<<"\nThis program will compute the sum of the cubes from 5^3 to N^3.\n ";
cout<<endl;

sum=pow(5, 3); cout<<"Total sum of 5^3 is : "<<sum<<".\n";
for (int i=6; i<=x; ++i) {
num=pow(i, 3);
cout<<"Total sum of "<<i<<"^"<<"3"<<" is: "<<num<<".\n";

}

return 0;
}```
This?

Code:
```#include <iostream>
#include <cmath>
#include<cstdlib>
using namespace std;
int main() {

int sum=0,x=0;
int num=0;

cout<<"Imput number N : ";
cin>>x;
if (cin.fail())
{
cerr << "\n\nSorry enter a number please";

}
else if (x<0)
{
cerr<<"\n\nSorry please only enter positive numbers";

}
else
cout<<"\nThis program will compute the sum of the cubes from 5^3 to N^3.\n ";

cout<<endl;
sum=pow(5, 3);
cout<<"Total sum of 5^3 is : "<<sum<<".\n";

for (int i=6; i<=x; ++i) {
num=pow(i, 3);
cout<<"Total sum of "<<i<<"^"<<"3"<<" is: "<<num<<".\n";

}

return 0;
}```

12. Something like that, but your code works only if entered number is higher than 5 and it dont give sum of the cubes it prints every cube separately, but that is minor task to do now. Thanks for help, much love