Thread: [Help] Mysterious Numbers

  1. #1
    Registered User
    Join Date
    May 2013
    Location
    Santo Domingo, Dominican Republic, Dominican Republic
    Posts
    1

    [Help] Mysterious Numbers

    Hello!

    I am new here, and created an account to ask for some help. Long story short: haven't touched C++ since High School, and need some help with a problem. I stupidly offered a girl some help and am a little stuck in making this work.

    A number is "mysterious" if it can be properly divided by the number of factors it has. For ex.: 9 has 3 factors, and is divisible by 3. 12 has 6 factors and is divisible by 6.

    I want to input a range and have it output the number of mysterious numbers within it. As soon as I enter the second number, my program closes though. Am I anywhere near my goal?

    Code:
    #include <iostream>using namespace std;
    
    
    bool esMisterioso(int n)
    {
        bool esMisterioso = false;
        int c = 0;
        for ( int i = 1; i <= n; i++)
        {
            if ( n % i == 0 ) c++;
        }
        
        if ( n % c == 0 ) esMisterioso = true;
        
        return esMisterioso;
    }
    
    
    int main()
    {
        int a, b, num = 0;
        cin >> a >> b;
        for ( int i = a; i <= b; i++)
        {
            if ( esMisterioso(i) ) num++;
        }
        cout << num;
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Maybe this? FAQ > Stop my Windows Console from disappearing everytime I run my program? - Cprogramming.com

    You also may want to flush the output buffer and print a newline after your output.

    Code:
    cout << num << endl;

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    as a bit of fun i added some little changes, nothing to do with the useful advice given by rags_to_riches though.

    Code:
    #include <iostream>
    
    using namespace std;
    
    
    bool esMisterioso(int n)
    {
        bool esMisterioso = false;
        int c = 0;
        for ( int i = 1; i <= n; i++)
        {
            if ( n % i == 0 ) c++;
        }
        if ( n % c == 0 )
        {
            esMisterioso = true;
            cout << n << " si hombre!\n";
        }
    
        return esMisterioso;
    }
    
    
    int main()
    {
        int a, b, num = 0;
        cin >> a >> b;
    
        cout << "\nprocesando...\n\n";
        for ( int i = a; i <= b; i++)
        {
            if ( esMisterioso(i) ) num++;
        }
        cout << "\n";
        cout << "hay "<< num << " numeros misteriosos entre " << a << " y " << b << " inclusivo.\n\n";
        return 0;
    }
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    Your code is correct. You might just want to add "cin.ignore();" so you can empty the buffer (after cin >> a >>b) and "cin.get();" by the end so it won't shutdown instantly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mysterious error
    By deeisenberg in forum C Programming
    Replies: 8
    Last Post: 04-30-2012, 03:51 PM
  2. mysterious seg fault!
    By MK27 in forum C++ Programming
    Replies: 12
    Last Post: 03-12-2010, 02:29 AM
  3. Mysterious Seg Fault
    By Akalic in forum C Programming
    Replies: 6
    Last Post: 10-26-2009, 04:16 PM
  4. mysterious numbers in a float
    By withoutn in forum C Programming
    Replies: 3
    Last Post: 07-02-2007, 10:05 AM
  5. mysterious code.......(??)
    By deltabird in forum C Programming
    Replies: 7
    Last Post: 04-11-2003, 03:58 PM