Thread: Something's wrong with me Quiz logic..

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    23

    Something's wrong with my Quiz logic.. [SOLVED]

    Code:
                
        for(int Non=0; Non<(sizeof(Question)/sizeof(Question[0])); Non++)
        {
            Iterator2=true;
            while(Iterator2) 
            {
                No = random(0,(sizeof(Question)/sizeof(Question[0])-1));
                for(int Ni=0; Ni<Non; Ni++)
                {
                    if(Question[No] == QuestionsAlreadyAsked[Ni])
                        continue;
                    else 
                        break;
                }
                Iterator2=false;
            }
            
                     
            
            QuestionsAlreadyAsked[Non]= Question[No];
                    
            cout<<endl<<endl;
            cout<<"\t";
            DelayType(Question[No], 15, false, 11);
            cout<<endl;
            
            cout<<"\t\t    ";
            DelayType("a)", 250, false, 15);
            cout<<"  ";
            DelayType(OptionA[No], 50, false, 7);
            cout <<endl;
            
     and so on..
            
            
    ///////////////////////// Question Output /////////////////////////
    
    
     Code for Answer logic here

    So focus on the

    Code:
     Iterator2=true;
            while(Iterator2) 
            {
                No = random(0,(sizeof(Question)/sizeof(Question[0])-1));
                for(int Ni=0; Ni<Non; Ni++)
                {
                    if(Question[No] == QuestionsAlreadyAsked[Ni])
                        continue;
                    else 
                        break;
                }
                Iterator2=false;
            }
    This is going to make it so that A question that was asked by the program previously would not be asked again. But this is not working! I have been breaking my head on this for hours I just don't get what is wrong.

    The "No" variable holds the question number to be outputted, and I have to make sure No does not repeat itself and hence the While Loop, but the while loop does not seem to work as anticipated

    because the same question is being printed out..
    Last edited by Nwb; 1 Week Ago at 03:11 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    36,559
    Your while(Iterator2) loop only ever executes once.

    The continue restarts the for loop, not the while loop.

    It might be easier if you took this approach.
    - Start with an array of questions.
    - Randomise the order just once.
    - Then simply loop from 0 to n asking the questions.
    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. #3
    Registered User
    Join Date
    Sep 2018
    Posts
    23
    Quote Originally Posted by Salem View Post
    Your while(Iterator2) loop only ever executes once.

    The continue restarts the for loop, not the while loop.

    It might be easier if you took this approach.
    - Start with an array of questions.
    - Randomise the order just once.
    - Then simply loop from 0 to n asking the questions.
    I definitely want to try randomizing the order itself. Thanks.

    Anyways for those of you interested, my logic was flawed in itself. However, I used another variable (Eject) and edited the logic, and now the approach would work.
    Code:
        for(int Non=0; Non<(sizeof(Question)/sizeof(Question[0])); Non++)
        {
            Iterator2=true;
            while(Iterator2) 
            {
                No = random(0,(sizeof(Question)/sizeof(Question[0])-1));
                for(int Ni=0; Ni<(sizeof(Question)/sizeof(Question[0]))+10; Ni++)
                {
                    if(Question[No] == QuestionsAlreadyAsked[Ni])
                        Eject=true;
                }
                if(!(Eject))
                    Iterator2=false;
                    
                Eject=false;
            
    
            }
            
        cout<<Question[No]<<endl;
        QuestionsAlreadyAsked[Non]= Question[No];
    
        }
    It was fun debugging though

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Coding why wrong/logic cant understand
    By raihan004 in forum C Programming
    Replies: 6
    Last Post: 10-02-2012, 04:16 AM
  2. is there anything wrong in sudoku logic ?
    By suryak in forum C Programming
    Replies: 5
    Last Post: 08-09-2011, 01:19 AM
  3. what is wrong with the logic in my loop?
    By tranman1 in forum C Programming
    Replies: 4
    Last Post: 03-10-2010, 08:48 PM
  4. Question 26 Answer seems wrong on site quiz
    By Kleid-0 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2005, 03:03 PM
  5. Logic all wrong
    By nizbit in forum C Programming
    Replies: 4
    Last Post: 10-17-2004, 05:15 PM

Tags for this Thread