Thread: Need help with a tiny program

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay, I just came back from a quick shower, and the idea came to me just as I started showering

    Basically, I was trying to find a way to loop 0 or 1 times, without having to use a control variable other than x and y. For example, this works:
    Code:
    while (x > y)
    {
        cout << "x is bigger than y";
        x = 0;
        y = 1;
    }
    Unfortunately, as soon as it hits the while (x < y) loop, it will print again, erroneously. So I tried to think of some swapping method that could avert this. However, there appears to be a simpler solution: do not reuse x and y. We could write something like:
    Code:
    void printGreaterThan(int x, int y)
    {
        while (x > y)
        {
            cout << "x is bigger than y";
            x = 0;
            y = 1;
        }
    }
    
    void printEqualTo(int x, int y)
    {
        // ...
    }
    
    void printLessThan(int x, int y)
    {
        // ...
    }
    
    // ...
    
    printGreaterThan(x, y);
    printEqualTo(x, y);
    printLessThan(x, y);
    Since x and y within each function is local to that functions, the assignment that ends the loop does not affect the x and y from the caller (e.g., the main() function). As such, only one of the functions when called actually prints something, and only once. This is equivalent to the if - else if - else structure that we would normally use.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I meant in terms of different ways of writing a conditional.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    How about
    Code:
    for (bool cond=(x > y); cond; cond=false) {
      // stuff
    }

  4. #19
    Registered User
    Join Date
    Jul 2007
    Posts
    15
    laserlight:
    Excellent touch breaking it up into functions and changing the values in the body. That solves my problem.

    Thanks everyone for the help. Since I'm getting more and more interested in C++ don't be surprised to see me in other threads in the near future. ;-)

    /Rahiiyja

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You're welcome, though I suspect that robatino's suggestion may be even simpler and easier to use.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User
    Join Date
    Jul 2007
    Posts
    15
    Here's an update of the situation, and some code I've written.

    Code:
    #include <iostream>
    using namespace std;
    
    int biggerThan(int, int);
    int smallerThan (int, int);
    int equal (int, int);
    
    int main()
    {
        cout << "Type in name of value one: ";
        string one;
        cin >> one;
        cout << endl << "Type in name of value two: ";
        string two;
        cin >> two;
        cout << endl << "Type in the value of " << one << ": ";
        int onevalue;
        cin >> onevalue;
        cout << endl << "Type in the value of " << two << ": ";
        int twovalue;
        cin >> twovalue;
        cout << endl << endl;
        biggerThan(onevalue, twovalue);
        smallerThan(onevalue, twovalue);
        equal(onevalue, twovalue);
        return 0;
    }
    
    int biggerThan(x,y)
    {
         while(x>y)
         {
                   cout << one << " is bigger than " << two << " with ";
                   cout << x-y << ".";
                   x=y;
         }
    }
    
    int smallerThan(x,y)
    {
         while(x<y)
         {
                   cout << two " is bigger than " << one << " with ";
                   cout << y-x << ".";
                   x=y;
         }
    }
    
    int equal(x, y)
    {
         while(x==y)
         {
                    cout << one << " & " << two;
                    cout << " are equal ";
                    x=y+1;
         }
    }
    I get a lot of messages from the compiler though. Can anyone tell me, or give me a hint as to what is wrong?

    Here are some examples of the messages I get:
    Code:
    29 int biggerThan' redeclared as different kind of symbol
    4  previous declaration of `int biggerThan(int, int)'  
    29 'int biggerThan'
    4 conflicts with previous declaration `int biggerThan(int, int)' 
    29 `x' was not declared in this scope 
    29 `y' was not declared in this scope
    30 initializer expression list treated as compound expression 
    30 expected `,' or `;' before '{' token 
    39 `int smallerThan' redeclared as different kind of symbol 
    5  previous declaration of `int smallerThan(int, int)' 
    and it goes on...
    I guess most of you experienced people already can see instantly what the problem is, but if you want I can post all the remaining compiler outputs.

    Thanks for any help

    /Rahiiyja

  7. #22
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > int biggerThan(x,y)
    You need some types in there as well, like
    int biggerThan(int x, int y)
    Specifically, they must match the prototype.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #23
    Registered User
    Join Date
    Jul 2007
    Posts
    15
    Thanks, that's something I should have tried, it solved most of it. The final problem I had was that the strings weren't declared globally, and therefore couldn't be used within the other functions, but I solved that.

    The problem I have now though, is that the program closes after I've typed in all the names and values. I'm using Dev-C++ 5 beta, and have included 'cin.get()' before the 'return0;' in 'int main().' Any clues?

    EDIT: When trying to run it again it suddenly worked. I must have forgotten to recompile it after using cin.get(). Thanks for your help.
    Last edited by Rahiiyja; 07-05-2007 at 02:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. tiny error with C Program
    By JohnMayer in forum C Programming
    Replies: 4
    Last Post: 07-11-2002, 03:09 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM