Thread: Nested loops!

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    36

    Nested loops!

    hi im trying to do the Alex's ebook question that is;
    Write a program that provides the option of tallying up the results of a poll with 3 possible values. The first input to the program is the poll question; the next three inputs are the possible answers. The first answer is indicated by 1, the second by 2, the third by 3. The answers are tallied until a 0 is entered. The program should then show the results of the poll—try making a bar graph that shows the results properly scaled to fit on your screen no matter how many
    results were entered.

    I have done this correctly however when it comes to the bar chart I think I have to use a nested loop which I haven't (I have used three seperate for loops) which therefore prints my results out in a list rather than a "bar" as required please could someone help! I'm still a bit unsure on nested loops I have read through but still uncertain.

    thanks

    Code:
    #include <iostream> //include header files
    #include <string>
    
    using namespace std;
    
    int main () //main body fcn
    
    {
        int answer;//delcaring integers and initializing them
        int a = 0;
        int b = 0;
        int c = 0;
        int i = 0;
        int j = 0;
        int k = 0;
    
        while( answer!=0) //start of while loop condition to execute unless equal to 0
        {
            cout << "How do you like your eggs in the morning? " << endl; //writes to std o/p
            cout << "Enter 1 for scrambled, 2 for fried or 3 for poached, 0 to exit " << endl; //prompts user for i/p
            cin >> answer; //stores i/p from user
    
            if (answer == 1 ) //if 1 is selected increments a by 1
            {
                a+=1;
            }
            else if (answer == 2) //if 2 is selected increments b by 1
            {
                b+=1;
            }
            else if (answer == 3) //if 3 is selected increment c by 1
            {
                c+=1;
            }
    
        }
    
            for (i = 0; i < a; i++) //initialises i at 0, loop commences compares it with a, increments i by 1
            {
                cout << "Scrambled eggs" <<  "*" << endl; //prints out tally result
            }
            for (j = 0; j < b; j++) //initialises j at 0, loop to commence if b is greater than j, increments j by 1
            {
                cout << "Fried Eggs " << "*" << endl; //prints out tally result
            }
    
            for (k = 0; k < c; k++) //initialises j at 0, loop commences if c is greater than a, increments j by 1
            {
                    cout << "Poached Eggs" << "*" << endl; //prints out tally result
            }
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    your logic is a little off. if a is 3, the following code:

    Code:
    for (i = 0; i < a; i++) //initialises i at 0, loop commences compares it with a, increments i by 1
    {
        cout << "Scrambled eggs" <<  "*" << endl; //prints out tally result
    }
    will print

    Code:
    Scrambled eggs*
    Scrambled eggs*
    Scrambled eggs*
    you don't need nested loops to do this job, unless you want to show the results after every vote added to the survey, in which case the three for loops need to be inside the while loop.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    36
    Thanks for the help Elkvis,
    I knew this was printing out these results. I ran the file first to see what it was doing but did not know what was going on. In response to what you have said about the nested loops I experienced that as well as I tried to do this the first time round. However I have taken note and have not used a nested loop instead I have altered the for loop scenario however there is still an error, the code that i have changed the for loops to is;
    Code:
            for (i = 0; i < a; i++) //initialises i at 0, loop commences compares it with a, increments i by 1
            {
                cout << "Scrambled eggs " << "* "; //prints out tally result
            }
            cout << endl;
            for (j = 0; j < b; j++) //initialises j at 0, loop to commence if b is greater than j, increments j by 1
            {
                cout << "Fried Eggs " << "* " ; //prints out tally result
            }
            cout << endl;
            for (k = 0; k < c; k++) //initialises j at 0, loop commences if c is greater than a, increments j by 1
            {
                cout << "Poached Eggs " << "* " ; //prints out tally result
            }
            cout << endl;
    and it is printing out result such as this
    http://cboard.cprogramming.com/image...AAAElFTkSuQmCC

    This is better however im not sure how i get it to stop printing the name in front of the the asterix *.
    Thanks again for the help

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    36
    sorry silly question, got it now here is the revised for loop scenario;
    Code:
            cout << "Scrambled eggs " ;
            for (i = 0; i < a; i++) //initialises i at 0, loop commences compares it with a, increments i by 1
            {
            cout << "* ";//prints out tally result
            }
            cout << endl;
    
            cout << "Fried Eggs " ;
            for (j = 0; j < b; j++) //initialises j at 0, loop to commence if b is greater than j, increments j by 1
            {
            cout << "* " ; //prints out tally result
            }
            cout << endl;
    
            cout << "Poached Eggs ";
            for (k = 0; k < c; k++) //initialises j at 0, loop commences if c is greater than a, increments j by 1
            {
            cout << "* " ; //prints out tally result
            }
            cout << endl;
    thanks again for the help!

  5. #5
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128
    I tried your program but the conditions are never met to run the first loop with the code posted originally.

    Code:
    int answer;//delcaring integers and initializing them
    int a = 0;
    int b = 0;
    int c = 0;
    int i = 0;
    int j = 0;
    int k = 0;
    The variable answer is not initialized before the loop (or initialized by user input either) :

    Code:
    while( answer!=0) //start of while loop condition to execute unless equal to 0
    Thanks for sharing, it's nice to see other people like myself still working their way through Alex's book

    I'm quite interested to see how "try making a bar graph that shows the results properly scaled to fit on your screen no matter how many results were entered." is achieved.
    Last edited by samwillc; 03-28-2013 at 02:45 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loops
    By flysurfer in forum C Programming
    Replies: 5
    Last Post: 10-19-2012, 05:26 AM
  2. nested while loops possible?
    By prepare in forum C Programming
    Replies: 15
    Last Post: 10-18-2004, 02:10 AM
  3. nested loops PLEASE HELP!!
    By scuba22 in forum C++ Programming
    Replies: 6
    Last Post: 10-08-2002, 09:31 AM
  4. Nested for loops
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2002, 10:25 AM
  5. nested for loops
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 11-17-2001, 11:44 AM

Tags for this Thread