Thread: Problems with an If loop..

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks inconsistent with too few spaces per indent.

    However, it also looks like you have been trying though, so here is an example of your code in well indented form:
    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
        srand((unsigned)time(0));
        int answer;
        int realanswer;
        int lowest = 0, highest = 49;
        int range = (highest - lowest) + 1;
    
        for (int j = 0; j < 10; ++j)
        {
            int random_integer1 = lowest + static_cast<int>(range * rand() / (RAND_MAX + 1.0));
            int random_integer2 = lowest + static_cast<int>(range * rand() / (RAND_MAX + 1.0));
            cout << "If I add  " << random_integer1 << " and  " << random_integer2
                 << " then what do I have? " << endl;
            cin >> answer;
    
            realanswer = random_integer1 + random_integer2;
        }
    
        for (int j = 0; j < 10; ++j)
        {
            for (int i = 0; i < 2; ++i)
            {
                if (answer == realanswer)
                {
                    cout << "You are Right" << "\n";
                    break;
                }
                else
                {
                    if (i == 1)
                    {
                        cout << "You are wrong. The right answer is " << realanswer << "\n";
                    }
                    else
                    {
                        cout << "Oops! You are wrong. Try again...\n";
                        cin >> answer;
                    }
                }
            }
        }
    }
    Aside from fixing indentation and spacing, I took the liberty of removing an unused variable (do not declare what you do not use), changing the scope of i, random_integer1 and random_integer2 (declare variables near first use), and also changing the C-style type casts to C++ style static_cast (more descriptive and easier to find).

    Note however that the casting of time(0) to unsigned int is not guaranteed to work (though it probably will). Read Prelude's article on using rand() for a portable alternative, namely hashing the bytes returned by time(0).
    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. #32
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Note: laserlight did not take the liberty of making the code correct. That's still up to you. You just have to think about what the code does and where it's supposed to be.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #33
    Registered User
    Join Date
    Jan 2008
    Posts
    24

    hows this?

    I think this is neater than before.
    Code:
     #include <iostream>
    #include <ctime>
    #include <cstdlib>
     using namespace std;
     int main()
     {
     srand((unsigned)time(0));
     int answer,i;
     int realanswer;
     int random_integer1;
     int random_integer2;
     int lowest=0, highest=49;
     int range=(highest-lowest)+1;
     char ch;
    
        for (int j=0;j<10;++j)
        {
         random_integer1 = lowest+int(range*rand()/(RAND_MAX + 1.0));
         random_integer2 = lowest+int(range*rand()/(RAND_MAX + 1.0));
         cout << "If I add  " << random_integer1 << " and  " << random_integer2 <<
         " then what do I have? " << endl;
         cin >> answer;
    
         realanswer=random_integer1+random_integer2;
         }
         for (int j=0;j<10;++j)
         {
           for(i=0;i<2;i++)
           {
            if (answer == realanswer)
    		                 {
            cout<<"You are Right"<<"\n";
            break;
            }
              else
              {
               if(i==1)
               {
               cout<<"You are wrong. The right answer is "<<realanswer<<"\n";
               }
               else
               {
               cout<<"Oops! You are wrong. Try again...\n";
               cin>>answer;
               }
               }
               }
               }
               }

  4. #34
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's not.
    If you're not willing to try anything, then I'm not going to help you, and I do encourage everyone else to stop too. There's no point.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #35
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, it should look like what laserlight posted [or similar].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #36
    Registered User
    Join Date
    Jan 2008
    Posts
    24

    k..

    Quote Originally Posted by Elysia View Post
    Note: laserlight did not take the liberty of making the code correct. That's still up to you. You just have to think about what the code does and where it's supposed to be.
    Well thanks anyway. I just wish you'd realize that I don't have the knowledge you do so I can't know what you mean by, "That's poorly indented". I can only take stabs in the dark and read the link you gave me.


    Thank you laserlight, for the visual.

  7. #37
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Sedvan View Post
    Well thanks anyway. I just wish you'd realize that I don't have the knowledge you do so I can't know what you mean by, "That's poorly indented". I can only take stabs in the dark and read the link you gave me.
    Whether or not you are a newbie doesn't matter, but if you don't listen, then there's no point in helping.
    I know you don't have much knowledge, which is exactly why I gave you the list because it is very informative on how to indent properly. But still you somehow didn't seem to do it properly. You never did mention you didn't understand the article or there was something you were confused over and then you post another poorly indented code sample after laserlight posted a properly indented sample, and you also mention "it looks better." It doesn't look very good in your favor.

    There is a reason I didn't give you a properly indented sample myself. That's because I wanted you to read the article and grasp what it means to indent so you could do it yourself. Just indenting something properly for you won't learn you anything.
    If you didn't understand anything, I could easily clarify for you to make sure you understood correctly.
    Last edited by Elysia; 01-29-2008 at 11:15 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #38
    Registered User
    Join Date
    Jan 2008
    Posts
    24
    Being confused and not listening are too very different things. You're wiki is helpful. But I can't just read some instructions and be expected to grasp the concept of how to neatly organize something I have never seen done correctly. I've been programming for maybe a week or so.

  9. #39
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'm going to take that as you are willing to learn. I only expected a little progress between each sample of code you posted, but it really seemed as if nothing had been learned at all.
    If you are you willing to try again, I can give you a sample you can indent properly while you work on your own code.

    Code:
    int main()
    {
    srand( (unsigned int)time(NULL) );
    int num;
    for (int i = 0; i < 1000; i++)
    {
    num = rand();
    if (i &#37; 10 == 0)
    {
    cout << "This is a specially "
    "genereated number just "
    "for you! "
    << "The number is: "
    << num;
    }
    else if (i % 15 == 0)
    {
    cout << "This is a specially "
    "genereated number just "
    "for me! "
    << "The number is: "
    << num;
    }
    else
    {
    cout << "Some randomly generated " 
    "is: " 
    << num;
    }
    }
    }
    If you can indent that properly, you pass. Try if you want.
    (On a side note: casting time(NULL) is bad, but I don't remember the link to Prelude's article about rand. Anyone got a link?)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #40
    Registered User
    Join Date
    Jan 2008
    Posts
    24

    Okay I gave it a shot.

    Code:
    int main()
    {
    
        srand( (unsigned int)time(NULL) );
        int num;
    
        for (int i = 0; i < 1000; i++)
        {
            num = rand();
            if (i % 10 == 0)
            {
            cout << "This is a specially genereated number just "
            "for you! " << "The number is: "<< num;
            }
                else if (i % 15 == 0)
                {
                cout << "This is a specially "
                "genereated number just "
                "for me! "<< "The number is: "<< num;
                }
                    else
                    {
                    cout << "Some randomly generated is: "<< num;
                    }
                }
            }

  11. #41
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    (On a side note: casting time(NULL) is bad, but I don't remember the link to Prelude's article about rand. Anyone got a link?)
    I already linked to it.
    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

  12. #42
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Normally, if - else braches are written like this:
    Code:
    if (condition)
    {
        //code
    }
    else if (condition)
    {
        //code
    }
    else
    {
        //code
    }
    There is not much point in indenting every single else statement, so practically nobody does it. Example:
    Code:
    int main()
    {
    	srand( (unsigned int)time(NULL) );
    	int num;
    	for (int i = 0; i < 1000; i++)
    	{
    		num = rand();
    		if (i % 10 == 0)
    		{
    			cout << "This is a specially "
    			"genereated number just "
    			"for you! "
    			<< "The number is: "
    			<< num;
    		}
    		else if (i % 15 == 0)
    		{
    			cout << "This is a specially "
    			"genereated number just "
    			"for me! "
    			<< "The number is: "
    			<< num;
    		}
    		else
    		{
    			cout << "Some randomly generated " 
    			"is: " 
    			<< num;
    		}
    	}
    }
    Last edited by robwhit; 01-29-2008 at 12:13 PM.

  13. #43
    Registered User
    Join Date
    Jan 2008
    Posts
    24

    Now I'm just wondering

    What exactly is making this program run without outputting if the answer is correct or incorrect after every input.

  14. #44
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What exactly is making this program run without outputting if the answer is correct or incorrect after every input.
    Your flawed program logic. Ask yourself: why do you have two outer loops?
    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

  15. #45
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by laserlight View Post
    I already linked to it.
    I know you have, but I can't remember where >_<

    Sedvan:
    You indented well, but not perfectly.
    I presume you remember indentation levels? Here's a recap if you don't remember: http://cpwiki.sourceforge.net/User:E...ntation_Levels
    And when we look through some indentation rules, there's one rules called "Indentation Level" (recap here: http://cpwiki.sourceforge.net/User:E...entation_Level) that says all blocks should receive +1 indentation. It should have +1 indentation level that the previous block.
    And if you don't remember what a block is, this covers that as well: http://cpwiki.sourceforge.net/User:E...entation_Rules
    There's also a multi-line indentation rule to use (it is preferable): http://cpwiki.sourceforge.net/User:E...ne_indentation

    With that information, you should be able to indent the example I posted a little better.

    And again, for your code, explain to us what you code does. You should be able to spot why.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  2. "for" loop problems....
    By hayai32 in forum C Programming
    Replies: 4
    Last Post: 05-04-2005, 01:20 AM
  3. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  4. simple collision detection problems
    By Mr_Jack in forum Game Programming
    Replies: 0
    Last Post: 03-31-2004, 04:59 PM
  5. problems with greatest to least sorting
    By Mr_Jack in forum C++ Programming
    Replies: 2
    Last Post: 03-11-2004, 10:12 PM