Like Tree1Likes
  • 1 Post By laserlight

Problem with condition in IF ELSE + cmath lib error

This is a discussion on Problem with condition in IF ELSE + cmath lib error within the C++ Programming forums, part of the General Programming Boards category; Hi to everyone, im working some exsercise for c++ and i have this code right here : Code: #include <iostream> ...

  1. #1
    Registered User
    Join Date
    Nov 2013
    Location
    Croatia
    Posts
    15

    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. #2
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    465
    For the first problem you can handle this with a condition:

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

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Quote Originally Posted by jocdrew21 View Post
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    465
    Sorry I told you wrong...

  5. #5
    Registered User
    Join Date
    Nov 2013
    Location
    Croatia
    Posts
    15
    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. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    if(str<0)

    you should check content of variable a not content of variable str
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    465
    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. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    Quote 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;
    }
    Pantheon303 likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Nov 2013
    Location
    Croatia
    Posts
    15
    Quote Originally Posted by jocdrew21 View Post
    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. #10
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    465
    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?

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    your indentation is misleading. You need to use autoindent

    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;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Registered User
    Join Date
    Nov 2013
    Location
    Croatia
    Posts
    15
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. condition problem
    By cubanoemg in forum C Programming
    Replies: 10
    Last Post: 02-07-2011, 02:33 PM
  2. <math> and <cmath>
    By h3ro in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2007, 10:00 PM
  3. Condition error
    By nick048 in forum C Programming
    Replies: 1
    Last Post: 04-10-2007, 10:06 AM
  4. What's the difference between cmath and math.h?
    By thetinman in forum C++ Programming
    Replies: 11
    Last Post: 12-02-2005, 03:28 PM
  5. Replies: 15
    Last Post: 01-14-2003, 10:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21