Problems with an If loop..

This is a discussion on Problems with an If loop.. within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by oogabooga (And don't mix tabs and spaces ... get rid of all tabs.) Who are you to ...

  1. #16
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,180
    Quote Originally Posted by oogabooga View Post
    (And don't mix tabs and spaces ... get rid of all tabs.)
    Who are you to judge? If you say get rid of all tabs, I say get rid of all spaces.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  2. #17
    Registered User
    Join Date
    Jan 2008
    Posts
    24

    I'm not even to the % wrong part..

    I'm more concerned with repetition of the code and it outputting whether or not it's right. I am able to make it repeat, but it won't output the outcome anymore.
    Code:
     {
        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;
    	}
    I must be missing something within these statements?

  3. #18
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,180
    Are you even listening or reading replies?
    Fix the tabs/spaces problem. Fix the brace problem and then THINK. Read the code back here.
    Why are we wasting time on you when you aren't listening? If you don't understand, then mention so!
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  4. #19
    Registered User
    Join Date
    Jan 2008
    Posts
    24

    :P I don't

    I'm pretty ignorant.

  5. #20
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,180
    What part do you not understand?
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

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

    Alright

    Well You guys are telling me to get rid of all my spaces/tabs, and I really don't understand how thats going to solve my problem. It's probably more of a style thing so that it's easier for you to look at. I don't know, i'm just messy. I really haven't taken any classes to speak of so I'm at a disadvantage. Still If you tell me how you think I should structure my program based on tabs/spaces I will do so. I just need a layman's explanation. I'm not pro.

  7. #22
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,180
    Yes, but we need to able to read your code, no?
    That's why we keep telling you to use only one of them. Either tabs or just spaces. It's called indentation and it's very important to make others be able to read your code as well to help you catch mistakes.
    Unfortunately, I can't tell you which one to use. You'll have to make up your own mind on that. Here is a good vs list:
    http://cpwiki.sourceforge.net/User:E..._Disadvantages
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  8. #23
    Registered User
    Join Date
    Jan 2008
    Posts
    24

    alright

    Seems a little irrelevant to me but then again i'm used to looking at my own code and I see differently than you might.

    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;
    }
    }
    }
    }
    }

  9. #24
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,180
    Huh? Now you destroyed it even more and fixed nothing.
    Read, read, read, read, read please.
    http://cpwiki.sf.net/User:Elyisa/Indentation
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

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

    if I'm tabbing then-

    does that mean I only hit it once, because everything just lines up and the eye won't see the difference in the statements...?

  11. #26
    Registered User
    Join Date
    Jan 2008
    Posts
    24

    only spaces this time.

    Okay so now I did spaces. Whatya think?
    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;
    }
    }
    }
    }
    }

  12. #27
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,180
    Yes, you're using only spaces, but the indentation is severely messed up (which is clearly your mistake and not due to unforeseen problems beyond your control such as mixing of spaces/tabs), which is why I wanted you to read the article. Is there something you did not understand or wasn't perfectly clear?
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

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

    you want me to

    You want me to indent with 4 or 8?

  14. #29
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Who are you to judge?
    I thought his code was mostly spaces with a few tabs messing it up. Now that I look back it seems the other way round (duh). I just meant to change them to the same thing. It's the _mixing_ I can't stand!

    To Sedvan: to say people's advice is "irrelavant" is pathetic. Programming is about communication, with the machine _and_ with people. Coding style is common courtesy, as well as a no-brainer for people with any experience. If you want people's advice (read "time"), then you have to meet them halfway. Personally, I can't believe how gracious the explicators here are sometimes. But for their example I would say "scrap it and take up knitting".

    (Note: He's posting this on multiple sites and seemlingly just accumulating code as he goes.)

  15. #30
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,180
    Quote Originally Posted by oogabooga View Post
    I thought his code was mostly spaces with a few tabs messing it up. Now that I look back it seems the other way round (duh). I just meant to change them to the same thing. It's the _mixing_ I can't stand!
    Then you should have mentioned so! It seemed to me like you were just a "tab hater" and wanted to the OP to use all spaces instead.
    I agree with you that the mixing is bad and messed up a lot.

    4 is fine. But the real problem is the indentation levels and bracket placement which is way wrong. Re-read the link I posted.
    Last edited by Elysia; 01-29-2008 at 09:41 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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-01-2008, 09: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, 07:26 PM
  4. simple collision detection problems
    By Mr_Jack in forum Game Programming
    Replies: 0
    Last Post: 03-31-2004, 03:59 PM
  5. problems with greatest to least sorting
    By Mr_Jack in forum C++ Programming
    Replies: 2
    Last Post: 03-11-2004, 09:12 PM

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