-
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;
}
-
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?
-
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.
-
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.
-
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'; }
}
-
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.. * * *?
* * *
* *
*