Like Tree1Likes

Help! Looping problem.

This is a discussion on Help! Looping problem. within the C++ Programming forums, part of the General Programming Boards category; Why didn't I think about it like this earlier? Now I need to think on how to make a bar ...

  1. #16
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    Why didn't I think about it like this earlier?

    Now I need to think on how to make a bar graph.

    Code:
    //User input poll of "Do you like _______ ?"
    
    
    //3 possible poll responds; r1, r2, r3.
    
    
    #include <iostream>;
    #include <string>;
    
    
    using namespace std;
    
    
    int main()
    {   //question
    	string poll;
    	//answers
    	
    
    
    	string respond;
    	
    
    
        cout << "NOTE: There are only 3 possibles answers to this poll:\n";
    	cout << "1.yes.\n";
    	cout << "2.no. \n";
    	cout << "3.no idea \n";
    
    
    	cout << "Press the answer's number and then ENTER to add poll.\n";
    	cout << "Example: press 1 to answer yes.\n";
    
    
    	cout << "To end poll, press 0 until the output to asks for vote ends.\n";
    
    
    	cout << "What would you like to ask? (Type below) \n\n";
    	getline(cin, poll);
    
    
    	cout << "\n\n\n";
    
    
    	cout << "Now each person press either 1,2, or 3 to vote.\n";
    	cout << "Once vote has complete, press 0 to exit poll and look at resulting bar graph.\n";
    	cout << "\n\n";
    	
    	while ( respond != "0" )
    
    
    	{
    		cin >> respond;
    
    
    		if ( respond != "1" && respond != "2" && respond != "3" && respond != "0" )
    		{ cout << "Invalid choice. Choices: 1 (yes), 2: (no), 3 (no idea) and 0 (exit poll).\n"; }
    
    
    	}
    
    
    	
    
    
    
    
    
    
    	  
    
    
    
    
    	 
    
    
    	system("pause");
    	return 0;
    }

  2. #17
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    On searching, I found out that I need array (some kind of memory?) to hold back the numbers to be presented on the graph. I haven't learn this.

    Can I just "draw" a graph using loops?
    If so, how do I collect the numbers of votes??

    For example the input is like;

    1
    1
    2
    2

    This indicates that 2 vote for yes and another 2 vote for no. But how do I collect the number of yes and no to produce an output of bar graph?

  3. #18
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,185
    If you need to remember something between "iterations", you need some storage. This storage is provided by variables, such as those you've already used.
    If you have many variables with the same name or you need to use an index to select a particular variable (say, name1, name2, etc), then you need an array. Arrays are provided in the form of std::array.
    Arrays are static, however. That is, its size need be determined at compile time (when you compile your program). If the size of your array must change during execution or if the size is not known at compile time, you need a dynamic array, which is provided in the form of std::vector.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  4. #19
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    Code:
    //User input poll of "Do you like _______ ?"
    
    
    //3 possible poll responds; r1, r2, r3.
    
    
    #include <iostream>;
    #include <string>;
    
    
    using namespace std;
      
       string respond;
    
    
    int main()
    {   //question
        string poll;
        //answers
        
    
    
        string respond;
        
    
    
        cout << "NOTE: There are only 3 possibles answers to this poll:\n";
        cout << "1.yes.\n";
        cout << "2.no. \n";
        cout << "3.no idea \n";
    
    
        cout << "Press the answer's number and then ENTER to add poll.\n";
        cout << "Example: press 1 to answer yes.\n";
    
    
        cout << "To end poll, press 0 until the output to asks for vote ends.\n";
    
    
        cout << "What would you like to ask? (Type below) \n\n";
        getline(cin, poll);
    
    
        cout << "\n\n\n";
    
    
        cout << "Now each person press either 1,2, or 3 to vote.\n";
        cout << "Once vote has complete, press 0 to exit poll and look at resulting bar graph.\n";
        cout << "\n\n";
        
        
        //people press 1 2 or 3 to vote.
        
        while ( respond != "0" )
    
    
        {
            cin >> respond;
    
    
            if ( respond != "1" && respond != "2" && respond != "3" && respond != "0" )
            { cout << "Invalid choice. Choices: 1 (yes), 2: (no), 3 (no idea) and 0 (exit poll).\n"; }
    
    
        
    
    
        }
    
    
        //add vertical "bar" for 'yes'.
        if  ( respond == "1" )
        {
            for ( int y = 1; y <2 ; y++)
            { cout << '\t' << y <<'\n'; }
    
    
        }
    
    
        
            
        
        
    
    
        
    
    
    
    
          
    
    
    
    
         
    
    
        system("pause");
        return 0;
    }
    I can't seem to make a vertical "bar". Why?

    After the while loop finishes (by pressing 0), the program just says 'press any key to continue', instead of creating a vertical "bar" like I intended to do.

  5. #20
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    Sorry this code is wrong, but still, it isn't printed out after the while loop.
    Code:
     //add vertical "bar" for 'yes'.
        if  ( respond == "1" )
        {
            for ( int y = 1; y <2 ; y++)
            { cout << '\t' << y <<'\n'; }
    
    
        }
    
    




  6. #21
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    Finally!! After long hours of thoughts and search! I think I grasped the idea of loops. Here's the code, AND I do hope I'm not over-confident about setting it right this time.

    Code:
    //User input poll of "Do you like _______ ?"
    
    
    //3 possible poll responds; r1, r2, r3.
    
    
    #include <iostream>;
    #include <string>;
    #include <sstream>;
    
    
    using namespace std;
      
       string respond;
       int a = 0; //count yes
       int b = 0; //count no
       int c = 0; //count no idea
    
    
    int main()
    {   //question
    	string poll;
    	//answers
    	
    	
    	
    	string choice;
    
    
        cout << "NOTE: There are only 3 possibles answers to this poll:\n";
    	cout << "1.yes.\n";
    	cout << "2.no. \n";
    	cout << "3.no idea \n";
    
    
    	cout << "Press the answer's number and then ENTER to add poll.\n";
    	cout << "Example: press 1 to answer yes.\n";
    
    
    	cout << "To end poll, press 0 until the output to asks for vote ends.\n";
    
    
    	cout << "What would you like to ask? (Type below) \n\n";
    	getline(cin, poll);
    
    
    	cout << "\n\n\n";
    
    
    	cout << "Now each person press either 1,2, or 3 to vote.\n";
    	cout << "Once vote has complete, press 0 to exit poll and look at resulting bar graph.\n";
    	cout << "\n\n";
    	
    	
    	//people press 1 2 or 3 to vote.
    	
    	 cout << "Choice ( 1, 2 or 3) : \n";
    
    
    
    
    	 while ( choice != "0" ) 
    
    
    	 {
    		 getline (cin,choice); 
    		 cout << '\n';
    
    
    		
    			  //vote yes
    			 for (; choice == "1"; ++a ) //++a is the count of votes for yes
    			
    			 { 
    			   cout << "voted yes.\n";
    			   a = ++a;
    			   break;
    			 }
    
    
    			 //vote  no.
    			 for (; choice == "2"; ++b ) //++b is the count of votes for no
    			 
    			 { 
    			   cout << "voted no. \n";
    			   b = ++b;
    			   break;
    			 }
    
    
    			 //vote no idea.
    			 for (; choice == "3"; ++c)  // ++c is the count of votes for no idea
    			
    			 { 
    			   cout << "voted no idea. \n";
    			   c = ++c;
    			   break;
    			 }
    			 
    			  
    
    
    
    
    	 }
    
    
    	 for (int i = 0; i < a; ++i)
    	                
    	 { cout << "*" << '\n'; }   
    
    
    	 for (int j = 0; j < b; ++j)
    
    
    	 { cout << '\t' << "*" << '\n'; }
    
    
    	 for (int k = 0; k < c; ++k)
    
    
    	 { cout << '\t' << '\t' << "*" << '\n';  }
    
    
    
    
    	
    
    
    	
        system("pause");
    	return 0;
    }
    1 2 3
    One thing though, how do you align the asteriks like this.. * * *?
    * * *
    * *
    *

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looping problem
    By vutek0328 in forum C Programming
    Replies: 8
    Last Post: 09-22-2006, 01:45 AM
  2. looping problem
    By chris285 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2005, 11:03 AM
  3. problem with looping
    By bajanstar in forum C Programming
    Replies: 7
    Last Post: 03-09-2005, 12:40 PM
  4. looping problem
    By swagatob in forum C Programming
    Replies: 7
    Last Post: 10-12-2004, 11:50 PM
  5. Looping problem
    By romeoz in forum C++ Programming
    Replies: 8
    Last Post: 09-01-2003, 08:38 PM

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