Thread: Factors and Primes

  1. #1
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79

    Factors and Primes

    Hey, here's the latest project I thought up - any suggested improvements?

    Code:
    #include <iostream>
    
    int x;
    int y;
    int factors = 0;
    void factor();
    
    int remainder;
    
    int main()
    {
        std::cout<<"Type a number and I'll report its factors, if any: ";
        std::cin>>x;
                for (y = 1; y <= x; y++)
            {
                remainder = x%y;
                if (remainder == 0 && y != 1 && y != x)
                factor(); 
            }
                  
        if (factors == 0)
        std::cout<<x<<" is a prime number."<<std::endl;
        
                         
        system("PAUSE");
        return 0;
    }
    
    void factor()
    {
         std::cout<<y<<" is a factor of "<<x<<std::endl;
         ++factors;
         return;
    }
    For some reason cin.get() doesn't work so I fell back on system("PAUSE").

    FYI I got stuck partway through Accelerated C++ so now I'm reading The C++ Primer by Skinner and Standard C++ Bible by Stevens and Walnum. I still have more ideas than skill!

    Thanks for your input -

    -JM

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    cin.get() not working because you need cin.ignore(); after you use cin >>

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    FYI I got stuck partway through Accelerated C++ so now I'm reading The C++ Primer by Skinner and Standard C++ Bible by Stevens and Walnum. I still have more ideas than skill!
    So which one of those books was the one that told you using globals for everything was a good idea?


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79
    Yeah, be rude to the n00b, thanks a lot for the help!

    Was there a suggestion in there somewhere?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How was that rude? You asked for suggestions. I pointed out that using globals for everything was less than a good idea. You're working on three books. Surely you got far enough in one of them to see how to pass variables around to functions.

    Here, let's quote you:
    any suggested improvements?

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79
    So I should pass variables to the function - nope, didn't quite learn that yet.

    Here, let's quote you:
    So which one of those books was the one that told you using globals for everything was a good idea?
    That's known as sarcasm Quzah.

    cin.get() not working because you need cin.ignore(); after you use cin >>
    THAT is helpful, thank you ILoveVectors!

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What's your point? You understood from my reply that global variables weren't the ideal. You had just asked for suggestions. Now you read my reply and either:
    a) See that I'm hinting that globals are a bad idea.
    b) Don't see what I'm talking about.

    It's one or the other. Either you understood my reply, in which case, it was helpful, because you now know that globals are to not be used. Or, you didn't understand my reply, and you'd ask for clarification. In either case, you need to stop being such a whiner. I didn't outright flame you to ash, which I should at this point, simply because you're such a thin skinned pansy.

    If you can't take the mildest form of sarcasm, which wasn't at all even remotely an attack on you, then you need to go find some place safe and quiet to hang out. The big scary world of internet forums is too much for your delicate persona.

    You damn sure shouldn't be posting code and asking for people's oppinion of it if you can't handle anything but soft pats on the hand saying what a good job you're doing. But if that's what you need:

    "Good job junior. Here's a shiny gold star! Now be sure to print out your source code, and we'll hang it on the fridge!"


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79
    Good job dude, be proud of yourself now.


    [edit] I guess you were trying to help, so thanks, but I'd work on those 'people' skills if I were you.
    Last edited by -JM; 07-20-2005 at 10:05 PM.

  9. #9
    Banned
    Join Date
    Jun 2005
    Posts
    594
    not meaning to hijack the thread, but i notice alot
    of people post with alot of stuff that is nto helpful
    to the problem at hand, i have done so several time
    but i try not to, there are several here that do
    it on a regular basis, but im not trying to hate on
    them, ive just been wanting to let these feelings
    out for a while, and this seemed like a good
    oppurtunity.

  10. #10
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79
    But you were helpful, thank you for that.

    I'm just trying to learn this stuff, it's not easy!

    -JM

  11. #11
    *this
    Join Date
    Mar 2005
    Posts
    498
    The reason you must use cin.ignore() is because you need to clear the stream first before waiting for the keyboard with cin.get().

    Because...
    When you use "cin >>" the enter key is not used in your data so it is left in what is called the stream. So in turn the next cin or your cin.get(); catches the enter key and uses it (which is probably not what you intended). So we must take it out of the stream with cin.ignore();

    You can call cin.ignore(); either right before cin.get(); or right after the "cin >>"

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by -JM
    [edit] I guess you were trying to help, so thanks, but I'd work on those 'people' skills if I were you.
    Well, you're not me. If you were, you wouldn't be so easily offended.

    Your question had already been answered, at least as far as 'cin.get' goes. However, your topical question was 'any suggestions'. I gave you one. You didn't like it. Either way, it has no long term effect on me. Take it or leave it.

    not meaning to hijack the thread, but i notice alot
    of people post with alot of stuff that is nto helpful
    to the problem at hand,
    But it was helpful. He just chose to get miffed about it, rather than to take it for what it was. He wanted suggestions, I gave him one in a round about way. The point was made. He got the point. He just didn't like the me making the point, or the way I made it.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    *this
    Join Date
    Mar 2005
    Posts
    498
    On a side note which isnt really necessary to post...
    Code:
    void factor()
    {
         std::cout<<y<<" is a factor of "<<x<<std::endl;
         ++factors;
         return;
    }
    You dont need to return here, its void so it will finish executing without the need of a return.

  14. #14
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Quote Originally Posted by quzah
    But it was helpful. He just chose to get miffed about it, rather than to take it for what it was. He wanted suggestions, I gave him one in a round about way. The point was made. He got the point. He just didn't like the me making the point, or the way I made it.

    Dont take it to hard, i was talking about a wide band
    of individuals, not pointing you out or just picking
    on you. I dont mean to offend by it we all do it,
    everyonce in a while, it just think that the board
    would be alot friendlier, and so many of are
    new people would continue to visit, if it wasnt
    for some of the mosts we make.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    On a side note, you don't even need the function call at all. It's pointless since you're not even passing anything to it.
    Code:
    #include <iostream>
    int main()
    {
        int x;
        int y;
        int factors = 0;
        int remainder;
    
        std::cout<<"Type a number and I'll report its factors, if any: ";
        std::cin>>x;
    
        for (y = 1; y <= x; y++)
        {
            remainder = x%y;
            if (remainder == 0 && y != 1 && y != x)
            {
                std::cout<<y<<" is a factor of "<<x<<std::endl;
                ++factors;
            }
        }
              
        if (factors == 0)
            std::cout<<x<<" is a prime number."<<std::endl;
        
        return 0;
    }
    On another aside, since you are using global variables, there's no need to even initialize them (the one you do) to zero. Globals are already initialized to zero by default.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 11-25-2007, 03:47 PM
  2. Replies: 6
    Last Post: 11-04-2002, 05:11 PM