confusion on a nested loop program

This is a discussion on confusion on a nested loop program within the C++ Programming forums, part of the General Programming Boards category; I'm trying to build a 10 question quiz and I'm designing it to where if you get the write answer ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Location
    Huintsville, AL.
    Posts
    14

    confusion on a nested loop program

    I'm trying to build a 10 question quiz and I'm designing it to where if you get the write answer it will move to the next question.

    the problem I'm having is its saying (a,b,c,d,) is not initialized to anything when I clearly have initialized it as far as I need

    Why is it telling me this?

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    
    char Answer;
    char a, b, c, d;
    
    
    cout<<"you will be givin a short Exam of 10 questions \n"<<endl;
    cout<<"Press enter after each question is answered \n"<<endl;
    cout<<endl;
    cout<<endl;
        do
        {
    cout<<"Question 1"<<endl;
    cout<<"where was Jason Born? \n\n"<<endl;
    cout<<endl;
    cout<<"(a) - Chicago?"<<endl;
    cout<<"(b) - Atlanta?"<<endl;
    cout<<"(c) - Pheonix?"<<endl;
    cout<<"(d) - Tucson?" <<endl;
    cin>>Answer;
    cin.sync();
    cin.get();
    
            if (Answer==d)
                {
            cout<<"Thats Correct!"<<endl;
                 }
                 else
        cout<<"Try Again"<<endl;
        }while (Answer==a,b,c);
    
    
    
    return 0;
    }
    I plan to finish the whole thing and calculate the grade average at the end I just need to get this block right 4 now

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    a,b,c,d are variables. Initialising them would be writing something like
    Code:
    a = 3;
    b = 'm';
    etc.

    I think you just want character constants. These are enclosed in single quotes, and you just use them like you would any other constant (e.g. a number), no need for char keyword. You can remove the "char a,b,c,d".

    For example
    Code:
    if (Answer==d)
                          becomes
    if (Answer=='d')
    Also:
    Code:
        }while (Answer==a,b,c);
    is this meant to be "while Answer is 'a' or 'b' or 'c'"?

    What you've written is valid C++ but it's unlikely that's what you wanted! The comma operator Comma operator - Wikipedia, the free encyclopedia here will evaluate the comparison, throw it away, throw b away and evaluate to while(c). You'd probably see an infinite loop.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Location
    Huintsville, AL.
    Posts
    14

    Thankvyou smokeyAngel

    It works
    you were right I had to just use my constant with single quotes

    Now I need to learn how to calculate the grade average on the test.

    Code:
    #include <iostream>
    using namespace std;
     
    int main()
    {
     
    char Answer;
    
     
     
    cout<<"you will be givin a short Exam of 5 questions \n"<<endl;
    cout<<"Press enter after each question is answered \n"<<endl;
    cout<<endl;
    cout<<endl;
        do
        {
    cout<<"Question 1"<<endl;
    cout<<"where was Jason Born? \n\n"<<endl;
    cout<<endl;
    cout<<"(a) - Chicago?"<<endl;
    cout<<"(b) - Atlanta?"<<endl;
    cout<<"(c) - Pheonix?"<<endl;
    cout<<"(d) - Tucson?" <<endl;
    cin>>Answer;
    
    cin.get();
     
            if (Answer=='d')
                {
            cout<<"Thats Correct!"<<endl;
                 }
                 else
        cout<<"Try Again"<<endl;
        }while (Answer== 'a' || Answer=='b' || Answer=='c');
     
     do
    		{
    			cout<<"Question 2"<<endl;
    			cout<<"Where did Jason go to highschool"<<endl;
    			cout<<endl;
    			cout<<"(a) Sahuaro ?"<<endl;
    			cout<<"(b) Sabino ?"<<endl;
    			cout<<"(c) Huntsville ?"<<endl;
    			cout<<"(d) Santa Rita ?"<<endl;
    			cin>>Answer;
    			
    			cin.get();
    			
    			if (Answer=='a')
    			{
    				cout<<"Right again!"<<endl;
    			}else 
    				cout<<"try again"<<endl;
    		}while (Answer=='b' || Answer=='c' || Answer=='d');
    		 
     do
    		{
    			cout<<"Question 3"<<endl;
    			cout<<"Where does Jason go to College"<<endl;
    			cout<<endl;
    			cout<<"(a) UAH ?"<<endl;
    			cout<<"(b) UNA ?"<<endl;
    			cout<<"(c) Alabama A&M ?"<<endl;
    			cout<<"(d) Athens Sate ?"<<endl;
    			cin>>Answer;
    			
    			cin.get();
    			
    			if (Answer=='c')
    			{
    				cout<<"Your on a roll!"<<endl;
    			}else 
    				cout<<"try again"<<endl;
    		}while (Answer=='a' || Answer=='b' || Answer=='d');
    
     do
    		{
    			cout<<"Question 4"<<endl;
    			cout<<"what is Jason's Major"<<endl;
    			cout<<endl;
    			cout<<"(a) Electrical Engineer ?"<<endl;
    			cout<<"(b) Civil Engineer ?"<<endl;
    			cout<<"(c) Computer Science ?"<<endl;
    			cout<<"(d) Mechanical Engineer ?"<<endl;
    			cin>>Answer;
    			
    			cin.get();
    			
    			if (Answer=='c')
    			{
    				cout<<"Yes-4 in a role!"<<endl;
    			}else 
    				cout<<"try again"<<endl;
    		}while (Answer=='a' || Answer=='b' || Answer=='d');
    
                    do
    		{
    			cout<<"Question 5"<<endl;
    			cout<<"What floor is the computer science office on ?"<<endl;
    			cout<<endl;
    			cout<<"(a) 1st floor ?"<<endl;
    			cout<<"(b) 2nd floor ?"<<endl;
    			cout<<"(c) 3rd floor ?"<<endl;
    			cout<<"(d) Basement ?"<<endl;
    			cin>>Answer;
    			
    			cin.get();
    			
    			if (Answer=='c')
    			{
    				cout<<"Yes- you have completed this test!"<<endl;
    			}else 
    				cout<<"try again"<<endl;
    		}while (Answer=='a' || Answer=='b' || Answer=='d');
    return 0;
    }

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    You need to fix your indentation before you do anything else.
    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. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    Agreed. Perfectly formatting your code is not a luxury, it's the bare essentials. It should be learnt and strictly adhered to right from day 1.

    That said, replacing tabs with a fixed number of spaces prior to posting goes a long way in some cases too.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested while loop inside for loop
    By Sonny in forum C Programming
    Replies: 71
    Last Post: 07-31-2011, 08:38 PM
  2. nested for loop
    By aromash in forum C Programming
    Replies: 4
    Last Post: 10-25-2010, 02:41 AM
  3. nested for loop
    By altf4thc in forum C++ Programming
    Replies: 3
    Last Post: 02-18-2010, 11:43 AM
  4. Nested Loops: Confusion
    By chubbs1900 in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2007, 07:04 PM
  5. Nested Loop
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-07-2002, 12:40 AM

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