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; Alright, Now the purpose of the program is to generate two random numbers both from the pool of 1-50 and ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    24

    Unhappy Problems with an If loop..

    Alright, Now the purpose of the program is to generate two random numbers both from the pool of 1-50 and have them come in the form of the question asking the user to input the sum. They get two tries and after that it says your wrong and prompts you to press return and the program ends. So far it seemed fine til i got the if loop. Something isn't right.

    Could someone tell me what I'm doing wrong? Thanks!

    Code:
    #include <iostream>
    #include <ctime> 
    #include <cstdlib>
    
    using namespace std;
    
    int main() 
    { 
        srand((unsigned)time(0)); 
        int answer;
        int realanswer;
        int random_integer1;
        int random_integer2;
        int lowest=0, highest=49;
        int range=(highest-lowest)+1; 
          for(int index=0; index<1; index++){
            random_integer1 = lowest+int(range*rand()/(RAND_MAX + 1.0));
            endl;
    }
    {
             for(int index=0; index<1; index++){
            random_integer2 = lowest+int(range*rand()/(RAND_MAX + 1.0));
            endl;
            cout << "If I add  " << random_integer1 << " and  " << random_integer2 <<
             " then what do I have? " << endl;
             cin >> answer;
             }
    
    
    {
    
        system("PAUSE");
    }
    
    
     
    {
     if (answer == realanswer)
           {
    
           cout << "You are Right! " << endl;
           }
           else
           {
    
           if (answer != realanswer)
           {
           cout << "Whoops Try again! :" << endl;
           }
           else
           {
           if (answer == realanswer)
           {
           cout << "You are Right :" << endl;
           }
           else
           {
           if (answer != realanswer)
           cout << "The answer was :" << realanswer << endl;
           }
        system("PAUSE");
    
      return 0;
    }
    }
    }
    }
    }

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    Indentation is horrible. I'm afraid you're going to have to fix that first:
    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.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    > for(int index=0; index<1; index++)
    index isn't going to remain <1 for very long.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    1

    Talking

    Hi,

    Where have you assigned the value to "realanswer"?

    Here is a tweaked code of the same....!
    You can use the "while" loop to make it much better in case of "for".


    Code:
    
    #include <iostream>
    #include <ctime> 
    #include <conio.h>
    #include <cstdlib>
    
    using namespace std;
    
    void 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;
    
    	{
    		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;		//This was missing in your program
    	}
    
    	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;
    			}
    		}
    	}
    }

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    24

    Lightbulb Thanks

    Wow...that clears things up a bit...It seems like I was putting to many if and else statements in there..it works now :P

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

    Question How could I repeat this loop 10 times?

    if i have this, could i put the for loop inside another loop and so that the student can take a little pop quiz and see the percent wrong?

    Code:
    #include <iostream>
    #include <ctime> 
    #include <conio.h>
    #include <cstdlib>
    
    using namespace std;
    
    void 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;
    
    	{
    		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(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;
    			}
    
    
    		}
    	}
    }

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    24

    Could I use a while loop?

    If the while loop repeats code until a condition is met then You can cause that loop that causes the answer to be correct or incorrect to repeat itself?

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

    Unhappy Okay So I've been tweaking it and

    It atleast runs the questions 10 times. But it doesn't grade them after each time. hrm..any ideas?
    Code:
     
    #include <iostream>
    #include <ctime> 
    #include <conio.h>
    #include <cstdlib>
    #include <string>
    
    using namespace std;
    
    void main() 
    { 
        srand((unsigned)time(0)); 
        float itiner[9];
        string num[10];
        int answer,i;
        int realanswer;
        int random_integer1;
        int random_integer2;
        int lowest=0, highest=49;
        int range=(highest-lowest)+1;
    	char ch;
    
    
    //Repeating The question
    	{   for(int i = 0;i <= 9;i++)
          {
            itiner[i] = i + 1;
            }
            for(int i = 0;i <= 9;i++)
            {
    		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;
    	}
    
    
    //Determining factor of correctness of question.
    	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. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    Learn to indent and use int main, not void main. It's undefined.
    THEN work on your problem. You are starting to have serious indentation problems, making the code difficult to read. And don't mix tabs and spaces. Stick to one.
    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. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    24

    Progress but it still stinks.

    Okay so I've been working on it, and I removed void like you wanted me to..hasn't really changed the program much..I really need a pointer on how to fix my loop..it works fine until i try to make it go 10 times with the " for (int j = 0; j < 10; ++j) " line.

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

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    Code doesn't make any sense.
    So you want it to generate two random numbers, ask what the sum of those becomes, and give the user two chances to answer right, and loop the whole thing 10 times, right?

    You need to understand how your logic in the program is bad.
    And you're still mixing tabs and spaces. Try removing one and it will do wonders to the code.
    Btw, you also seem to be confused with the first for loop. Everything inside the { and } belongs to the loop, but since you placed it AFTER the first {, it means only the first line after the loop belongs to the loop.
    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.

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    it works fine until i try to make it go 10 times
    Make what go 10 times? Do you want a series of 10 random addition problems with 2 tries at the answer for each?

    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;
    	}
    That first brace '{' is in the wrong place... currently the only thing being done 10 times in that bit of code is that you only calculate random_integer1 over and over throwing away intermediate values.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

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

    Unhappy Is this better?

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

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    No!
    Read your code and explain what it's doing.
    This is a logic error. The code is right, but it's incorrectly placed!
    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.

  15. #15
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You've have hardly changed anything since this code was _given_ to you a few posts back. You've just added the "for (int j = 0..." part there. Sweet. But it will not magically keep track of how many answers have been correct. You need (yet) another variable to accumulate that result, and another cout at the end to show it. Think!
    (And don't mix tabs and spaces ... get rid of all tabs.)

Page 1 of 4 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