Thread: Simple help with an assignment

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    2

    Simple help with an assignment

    Well, I'm very new to programming and I'm stuck on an assignment...


    I'm supposed to make a program that involves creating a while loop and if-else statements. Unfortunately, I'm stuck. This is what I have so far.


    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
        char response;
        
        cout <<"Animal Classifier" << endl;
        cout << endl;
        
        
        
        
        cout <<"Does the animal have backbones (Y/N)?" << endl;
        cin >> response;
        cout << endl;
        
        if (response == 'Y')
        {
        cout << "The animal is a Vertebrate. Let's continue..." << endl;
        }
        else if (response == 'N')
        {
        cout << "The animal is an Insect." << endl;
        }
        
        
        
        cout << "Does the animal's body feel cold (Y/N)?" << endl;
        cin >> response;
        cout << endl;
        
            if (response == 'N')
            {
            cout << "The animal is Warm-blooded. Let's continue..." << endl;
            }
            
            else if (response == 'Y')
            {
            cout << "The animal is a Reptile." << endl;
            cout << endl;
            }
                    
    
    
        cout << "Can it fly (Y/N)?" << endl;
        cout << endl;
        cin >> response;
        
            if (response == 'Y')
            {
            cout << "The animal is a Bird." << endl;
            cout << endl;
            }
            
            else if (response == 'N')
            {
            cout << "The animal is a Mammal." << endl;
            cout << endl;
            }
        
    }
         
    system("Pause")
    return 0;
    My question is....how do I make it so that when it DOES determine the type of animal it is, that it would loop around to the beginning. I will eventually cout<<"Would you like to start over?" so that it loops around, but I'm confused.


    Please don't exactly tell me how, but if you can give me an example, that would be great. I think these are what is called a Nested If-Else?

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    A loop?

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    33
    Google ‘c++ while loops’ and find this:

    Code:
    bool runProg = false;
    while (runProg == false) 
    {
    Your code here
    }
    cout << "Run program again (Y/N)?" << endl;
    cin >> response;
    if (response == 'N')
    {
    	runProg = true;
    }
    Your last 2 lines may be causing you a problem.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    15

    Wink

    It should be a Do... While loop because you want that the program runs at least, one time.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    2
    Finished my basic assignment lol.


    I just used a while loop only


    Thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 04:50 PM
  2. simple chat program questions
    By surefire in forum C Programming
    Replies: 5
    Last Post: 11-05-2009, 08:16 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple question about array of chars
    By Chewy in forum C Programming
    Replies: 9
    Last Post: 04-12-2004, 05:13 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM