Score component in beginner C++ program - c++

I am doing an assignment where I am supposed to write a program to test the user's math skills. Here's the code i have right now:
using namespace std;
void addition()
{
int Value, Value2, Answer;
bool gotAnswer;
for (int i=1; i<=10; i++)
{
Value = 1 + (rand()%10);
Value2 = 1 + (rand()%10);
gotAnswer = false;
cout << Value << " + " << Value2 << " = ";
for (int a=1; (a<=3 && gotAnswer==false); a++)
{
cin >> Answer;
if (Answer==(Value+Value2))
{
cout << "CORRECT" << endl << endl;
gotAnswer = true;
}
else
{
cout << "WRONG Try again." << endl;
if (a==3)
{
cout << "You have missed 3 times. The answer is " << Value+Value2 << "." << endl << endl;
}
}
}
}
}
void substraction()
{
int Value, Value2, Answer;
bool gotAnswer;
for (int i=1; i<=10; i++)
{
Value = 1 + (rand()%10);
Value2 = 1 + (rand()%10);
gotAnswer = false;
cout << Value << " - " << Value2 << " = ";
for (int a=1; (a<=3 && gotAnswer==false); a++)
{
cin >> Answer;
if (Answer==(Value-Value2))
{
cout << "CORRECT" << endl << endl;
gotAnswer = true;
}
else
{
cout << "WRONG Try again." << endl;
if (a==3)
{
cout << "You have missed 3 times. The answer is " << Value-Value2 << "." << endl << endl;
}
}
}
}
}
void Multiplication()
{
int Value, Value2, Answer;
bool gotAnswer;
for (int i=1; i<=10; i++)
{
Value = 1 + (rand()%10);
Value2 = 1 + (rand()%10);
gotAnswer = false;
cout << Value << " x " << Value2 << " = ";
for (int a=1; (a<=3 && gotAnswer==false); a++)
{
cin >> Answer;
if (Answer==(Value*Value2))
{
cout << "CORRECT" << endl << endl;
gotAnswer = true;
}
else
{
cout << "WRONG Try again." << endl;
if (a==3)
{
cout << "You have missed 3 times. The answer is " << Value*Value2 << "." << endl << endl;
}
}
}
}
}
int main()
{
int number;
cout << "Enter the number for the problem type desired:"<<endl;
cout << " 1. Addition"<<endl;
cout << " 2. Subtraction"<<endl;
cout << " 3. Multiplication"<<endl;
cin >> number;
if (number == 1)
{
addition();
}
else if(number == 2)
{
substraction();
}
else if (number ==3)
{
Multiplication();
}
}
The program runs fine. However, there should be a score component where the user gets 10 points on the first try, 5 point on second try, and 0 on third try/wrong. I have no idea how to blend the score component in and the display at the end of 10 questions. Hints please?
All thanks in advance.

You should keep a score variable in each of your functions, add to the score variable as necessary and then return the score variable.
So those function are no longer going to be voids, they'll be ints. You can then get the score at the end and print it out.
I'm not going to write any code for you, since it is for an assignment :P

Related

Why is failed input validation returning me to the start of my program?

Really sorry if this is a dumb question. I know it must have a super easy solution but I've been staring at this for so long I can't see it. It doesn't help that I'm really new at this either.
Long story short for some reason entering an invalid input past the first time returns me back to my menu, and sometimes also asks me to enter weight immediately after instead of allowing me to enter a menu choice. It's just all around broken and I don't know why. Thanks.
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
bool loopFlag = true;
bool loopFlagTwo = true;
int choice = 0;
int time = 0;
float weightPounds = 0;
float weight = 0;
const int BIKING = 8;
const int RUNNING = 10;
const int LIFTING = 3;
const float YOGA = 2.5;
int main()
{
cout << "Welcome to my Fitness Center" << endl;
do
{
cout << "\n\t____________________________________________________________" << endl;
cout << "\n\t\t\tMy Fitness Center" << endl;
cout << "\t\t\tActivity System" << endl;
cout << "\t____________________________________________________________" << endl;
cout << "\t\t\t Main Menu\n" << endl;
cout << "\t\t\t1) Stationary Bike" << endl;
cout << "\t\t\t2) Treadmill" << endl;
cout << "\t\t\t3) Weight Lifting" << endl;
cout << "\t\t\t4) Hatha Yoga" << endl;
cout << "\t\t\t5) End" << endl;
cout << "\t____________________________________________________________" << endl;
cout << "\n\nEnter the workout that you wish to track, or end to exit:" << endl;
do
{
cin >> choice;
if (cin.fail() || choice > 5 || choice < 1)
{
cout << "Invalid choice. Please choose from option 1 through 5." << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}
else if (choice == 5)
{
return 0;
}
else
{
loopFlag = false;
}
}
while (loopFlag);
do
{
cout << "\nPlease enter your weight in pounds: " << endl;
cin >> weightPounds;
if (cin.fail() || weightPounds <= 0)
{
cout << "Invalid weight entry!" << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}
else
{
loopFlag = false;
}
}
while (loopFlag);
weight = weightPounds / 2.2;
cout << "\nYour weight is: \n" << fixed << setprecision(1) << weight << " kilograms." << endl;
if (choice == 1)
{
do
{
cout << "For how many minutes did you do this activity? " << endl;
cin >> time;
if (cin.fail() || time <= 0)
{
cout << "Invalid time entry!" << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}
else
{
loopFlag = false;
}
}
while (loopFlag);
}
}
while (choice != 5);
return 0;
}
You need to set loopFlag to true before every do...while() you have, or use another flag, because after the first do...while(), loopFlag is always false.

calculating scores and total questions asked

I'm trying to output the score for each addition, subtraction, multiplication, division in the end test function. and the total of the question asked.
**Any help would be appreciated
BTW this is only using local functions and is passing the scores by reference. IT's supposed to output the number of answers that you answer correctly.
Also is there a way where you can prevent the program from letting you select the choices more than once?
here's what I got so far:
#include < iostream >
#include < iomanip >
#include < stdlib.h >
#include < time.h >
#include < stdlib.h >
using namespace std;
int addition(int addscore);
int subtraction(int subscore);
int multiplication(int multiscore);
int division(int divscore);
int endtest(int & addscore, int & subscore, int & multiscore, int & divscore);
main() {
int end_final = 0;
do {
int addscore, subscore, multiscore, divscore;
char choice;
cout << "A- " << "Addition\n";
cout << "B- " << "Subtraction\n";
cout << "C- " << "Multiplication\n";
cout << "D- " << "Division\n";
cout << "E- " << "End of Test\n";
cin >> choice;
cout << endl << endl;
switch (choice) {
case 'A':
case 'a':
addition(addscore);
break;
case 'B':
case 'b':
subtraction(subscore);
break;
case 'C':
case 'c':
multiplication(multiscore);
break;
case 'D':
case 'd':
division(divscore);
break;
case 'E':
case 'e':
endtest(addscore, subscore, multiscore, divscore);
break;
}
}
while (end_final != 1);
return 0;
}
int addition(int addscore) {
int iRandom;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen addition\n";
int randnum1, randnum2;
int total = 0;
for (int i = 1; i <= 5; i++) {
randnum1 = rand() % 15 + 1;
randnum2 = rand() % 15 + 1;
cout << randnum1 << " " << "+" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total++;
if (answer == randnum1 + randnum2) {
cout << "Correct! \n";
addscore++;
} else {
cout << "Incorrect \n";
}
}
}
int subtraction(int subscore) {
int iRandom;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen subtraction\n";
int randnum1, randnum2;
int total = 0;
while (total != 5) {
randnum1 = rand() % 20 + 1;
randnum2 = rand() % 20 + 1;
if (randnum1 >= randnum2) {
cout << randnum1 << " " << "-" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total++;
if (answer == randnum1 - randnum2) {
cout << endl;
cout << "Correct!\n ";
subscore++;
} else {
cout << "Incorrect\n ";
}
}
}
}
int multiplication(int multiscore) {
int iRandom;
int total = 0;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen Multiplication\n";
int randnum1, randnum2;
for (int i = 1; i <= 5; i++) {
randnum1 = rand() % 20 + 1;
randnum2 = rand() % 20 + 1;
cout << randnum1 << " " << "x" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total++;
if (answer == randnum1 * randnum2) {
cout << endl;
cout << "Correct! \n";
multiscore++;
} else {
cout << "Incorrect\n ";
}
}
}
int division(int divscore) {
int iRandom;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen Division\n";
int randnum1, randnum2;
int total = 0;
while (total != 5) {
randnum1 = rand() % 13 + 1;
randnum2 = rand() % 13 + 1;
if (randnum1 % randnum2 == 0) {
cout << randnum1 << " " << "/" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total++;
if (answer == randnum1 / randnum2) {
cout << endl;
cout << "Correct! \n";
divscore++;
} else {
cout << "Incorrect\n ";
}
}
}
}
int endtest(int & addscore, int & subscore, int & multiscore, int & divscore) {
int total = 0;
cout << endl << endl;
cout << "Addition" << " " << addscore++ << endl;
cout << "Subtraction" << " " << subscore << endl;
cout << "Multiplication" << " " << multiscore << endl;
cout << "Division" << " " << divscore << endl;
cout << "Total" << " " << total << endl;
}
To clarify the code, declare an enum
enum { ADDITION=0, SUBTRACTION, MULTIPLICATION, DIVISION };
You could have global arrays
int used[] = { 0,0,0,0 }; // not used
int total[] = { 0,0,0,0 }; // total questions per operation
int score[] = { 0,0,0,0 }; // score per operation
Then, doing it for 'addition', in the do {
if ( ! used[ADDITION]) cout << "A- " << "Addition\n";
then in the switch
case 'A':
case 'a':
used[ADDITION] = 1; // tells addition was used
addition(addscore);
break;
The addition code becomes
int addition(int addscore) {
int iRandom;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen addition\n";
int randnum1, randnum2;
total[ADDITION] = 0; // should not be
score[ADDITION] = 0; // ...necessary (but in case you call it again)
for (int i = 0; i < 5 ; i++) {
randnum1 = rand() % 15 + 1;
randnum2 = rand() % 15 + 1;
cout << randnum1 << " " << "+" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total[ADDITION]++; // total is incremented for Addition
if (answer == randnum1 + randnum2) {
cout << "Correct! \n";
score[ADDITION]++; // score is incremented
} else {
cout << "Incorrect \n";
}
}
}
endtest becomes
int endtest() {
cout << endl << endl;
if (used[ADDITION]) cout << "Addition" << " " << score[ADDITION] << " / " << total[ADDITION] << endl;
// same for SUBTRACTION ...
int grandtotal = total[ADDITION] + ....;
cout << "Grand Total" << " " << grandtotal << endl;
}
endtest is declared, and called like that
int endtest();
...
endtest();
Since this is C++, you could also make a class Operation, then one subclass per operation, holding the name of the operation and overriding a 'execute' method that does the specific operation, then declare an array of the parent class that holds an instance of each operation ...
Based on OP's comments, after the do {
// initialize variables to zero
int addscore=0, subscore=0, multiscore=0, divscore=0;
in the switch
case 'A':
case 'a':
used[ADDITION] = 1; // tells addition was used
addition( &addscore ); // <== gives the pointer to that variable
break;
then in addition code
int addition(int *addscore) { // <== gets a pointer
int iRandom;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen addition\n";
int randnum1, randnum2;
total[ADDITION] = 0; //
score[ADDITION] = 0; // using your way, this is not necessary anymore
for (int i = 0; i < 5 ; i++) {
randnum1 = rand() % 15 + 1;
randnum2 = rand() % 15 + 1;
cout << randnum1 << " " << "+" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total[ADDITION]++; // total is incremented for Addition
if (answer == randnum1 + randnum2) {
cout << "Correct! \n";
// score[ADDITION]++; // score is incremented (see above)
*addscore++; // <== increment variable pointed to by pointer
} else {
cout << "Incorrect \n";
}
}
}

How can I Terminate Vector Input Using if statement in C++

Does anybody have a simple solution to terminate a for loop that inputs values for a vector when a specific value is placed into the vector, but does not store that specified value into the vector?
I have created a random number generator that creates "n" unique values for a vector of size "n". However, when I run my code, the word "done" pops up and the program crashes.
Here is the code:
int main()
{
int i = 0;
vector<int> playerIndex;
vector<int> randGenNum;
vector<string> players;
vector<string> team1;
vector<string> team2;
vector<string> team3;
vector<string> team4;
cout << "Welcome to the Team Selector. \n" << endl;
cout << "A random team of two will be generated for you all." << endl;
cout << "When you are finished entering player names, enter the word \"done\". " << endl;
cout << "Enter the name of the players: \n" << endl;
for(string name; cin >> name;){
players.push_back(name);
if(name == "done"){
players.erase(players.end() - 1);
break;
}else{
playerIndex.push_back(i);
i++;
}
}
cout << "\n";
cout << "Player names: \n";
for(int a=0; a < players.size(); a++){
cout << a + 1 << " - " << players[a] << endl;
}
srand(time(0));
int b =0;
int randNum;
while(b < players.size()){
randNum = 1 +(rand()%players.size());
if(find(randGenNum.begin(), randGenNum.end(), randNum)!= randGenNum.end()){
while(find(randGenNum.begin(), randGenNum.end(), randNum)!= randGenNum.end()){
randNum = 1 +(rand()%players.size());
}
randGenNum.push_back(randNum);
b++;
}else{
randGenNum.push_back(randNum);
b++;
}
}
cout << "\n" <<"This is a test" << endl;
for (int c=0; c < players.size(); c++){
cout << "Generated #: " << randGenNum[c] << endl;
}
int totalPlayers = players.size();
int firstPlayer = randGenNum[0];
int secondPlayer = randGenNum[1];
int thirdPlayer = randGenNum[2];
int fourthPlayer = randGenNum[3];
int fifthPlayer = randGenNum[4];
cout << "\n";
cout << "Team 1 is: " << players[firstPlayer] << " and "<< players[secondPlayer] << "\n" << endl;
cout << "Team 2 is: " << players[thirdPlayer] << " and " << players[fourthPlayer] << "\n" << endl;
return 0;
}
Problem
players.end() - 1 is not a valid iterator.
Solutions 1:
Check the item before adding it to the vector.
for(string name; cin >> name;) {
if(name != "done")
{
players.push_back(name);
playerIndex.push_back(i);
i++;
}
}
Solutions 2:
Use std::vector::pop_back().
for(string name; cin >> name;){
players.push_back(name);
if(name == "done"){
players.pop_back();
break;
}else{
playerIndex.push_back(i);
i++;
}
}

Random Guessing Game in C++

My assignment is to create a guessing game where the computer guesses my number and one where I guess the computer's number. I have my code written out but It's incomplete. I want to create a menu so the user can choose which game he wants to play, and also i am having trouble joining the two programs into one so they both will run. Can someone please assist me. The first game works fine but from there I really don't know what I'm doing.
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
const int min = 1;
const int max = 100;
int num = 2 && 3 && 4;
int prevMin = 0;
int prevMax = 0;
int tries = 0;
int guess = 0;
int userNum = 0;
int userGuess = 0;
int systemNum = 0;
int systemGuess = 0;
cout << "Welcome to the Guessing Game!" << endl;
cout << "Enter a 2 to play Game 1 or a 3 to play Game 2. Or enter 4 to quit." << endl;
cin >> num;
if (num == 2)
{
cout << "Welcome to Game 1!" << endl;
cout << "User, enter a random number between " << min << " and " << max << " : "; cin >> userNum;
}
srand(time(0));
systemGuess = rand() % 100 + 1;
do
{
cout << "System, guess the user's number between " << min << " and " << max << ": " << systemGuess << endl;
cin.get();
++tries;
if (systemGuess > max || systemGuess<min)
{
cout << "I said guess a number between " << min << " and " << max << " stupid." << endl;
}
if (systemGuess > userNum)
{
cout << "Too high. Guess lower." << endl;
prevMax = systemGuess;
systemGuess = rand() % (prevMax - prevMin) + prevMin;
if (systemGuess == prevMin)
systemGuess++;
}
else if (systemGuess < userNum)
{
cout << "Too low. Guess higher." << endl;
prevMin = systemGuess;
systemGuess = rand() % (prevMax - prevMin) + prevMin;
if (systemGuess == prevMin)
systemGuess++;
}
} while (systemGuess != userNum);
cout << systemGuess << endl;
cout << " I guessed it right! It took me " << tries << " guess(es). " << endl;
srand(time(0));
systemNum = rand()% 100 + 1;
//Beginning of second game
do
{
if (num == 3)
{
cout << "Welcome to Game 2!" << endl;
cout << "User, try to guess the computer's number that's between " << min << " and " << max << " . " << endl;
}
cout << "Enter your guess." << endl;
cin >> userGuess;
tries++;
if (userGuess > systemNum)
{
cout << "Too high. Guess lower." << endl;
cin >> userGuess;
}
else if (userGuess < systemNum)
{
cout << "Too low. Guess higher." << endl;
cin >> userGuess;
}
else
{
cout << "Correct! It took you long enough! Lol... " << endl;
cin >> userGuess;
}
while (userGuess != systemNum);
}
system("pause");
return 0;
}
Make the two games two distinct functions (each with its own local variables), create a third one that is a loop that prompts for the games (and break when "quit" is selected), and call the corresponding function, then call the third one from main.
You can use if...else :
cout << "Welcome to the Guessing Game!" << endl;
cout << "Enter a 2 to play Game 1 or a 3 to play Game 2. Or enter 4 to quit." << endl;
cin >> num;
if (num == 2)
{//game1
}
else if(num==3)
{//game2
}
else if(num==4)
{return 0;
}else
{
cout<<"Invalid choice!";
system("pause");
return 1;
}
//print highscores
system("pause");
return 0;
Your whole code:
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
const int min = 1;
const int max = 100;
int num = 2 && 3 && 4;
int prevMin = 0;
int prevMax = 0;
int tries = 0;
int guess = 0;
int userNum = 0;
int userGuess = 0;
int systemNum = 0;
int systemGuess = 0;
srand(time(0));
cout << "Welcome to the Guessing Game!" << endl;
cout << "Enter a 2 to play Game 1 or a 3 to play Game 2. Or enter 4 to quit." << endl;
cin >> num;
if (num == 2)
{
cout << "Welcome to Game 1!" << endl;
cout << "User, enter a random number between " << min << " and " << max << " : "; cin >> userNum;
systemGuess = rand() % 100 + 1;
do
{
cout << "System, guess the user's number between " << min << " and " << max << ": " << systemGuess << endl;
cin.get();
++tries;
if (systemGuess > max || systemGuess<min)
{
cout << "I said guess a number between " << min << " and " << max << " stupid." << endl;
}
if (systemGuess > userNum)
{
cout << "Too high. Guess lower." << endl;
prevMax = systemGuess;
systemGuess = rand() % (prevMax - prevMin) + prevMin;
if (systemGuess == prevMin)
systemGuess++;
}
else if (systemGuess < userNum)
{
cout << "Too low. Guess higher." << endl;
prevMin = systemGuess;
systemGuess = rand() % (prevMax - prevMin) + prevMin;
if (systemGuess == prevMin)
systemGuess++;
}
} while (systemGuess != userNum);
cout << systemGuess << endl;
cout << " I guessed it right! It took me " << tries << " guess(es). " << endl;
}
systemNum = rand()% 100 + 1;
//Beginning of second game
if (num == 3)
{
do
{
cout << "Welcome to Game 2!" << endl;
cout << "User, try to guess the computer's number that's between " << min << " and " << max << " . " << endl;
cout << "Enter your guess." << endl;
cin >> userGuess;
tries++;
if (userGuess > systemNum)
{
cout << "Too high. Guess lower." << endl;
cin >> userGuess;
}
else if (userGuess < systemNum)
{
cout << "Too low. Guess higher." << endl;
cin >> userGuess;
}
else
{
cout << "Correct! It took you long enough! Lol... " << endl;
cin >> userGuess;
}}
while (userGuess != systemNum);
}else if(num==4)return 0;
else{
cout<<"Invalid choice!";
system("pause");
return 1;
}
//print highscores
system("pause");
return 0;
}

C++ program to display votes in percentage not showing correct result

I'm solving some C++ problems from ebooks. I made this C++ program but it isn't working properly. I've 2 problems:
Even after applying the forumla (votePercentage = firstAnswer/totalVotes*100;) it isn't showing the output, but only 0.
The program should display the bar chart, how am I suppose to do that? Any hints, reference or solution will be appreciated.
Here is my code:
/*
* 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.
*/
#include <iostream>
#include <string>
void startPoll (void);
void showPoll (void);
void pollCheck (void);
std::string pollQuestion, answer1, answer2, answer3;
int pollChoice, firstAnswer, secondAnswer, thirdAnswer;
int main (void)
{
int totalVotes = 1;
float votePercentage;
startPoll();
showPoll();
for(;;totalVotes++)
{
if (pollChoice == 1)
{
firstAnswer = firstAnswer + 1;
}
else if (pollChoice == 2)
{
secondAnswer++;
}
else if (pollChoice == 3)
{
thirdAnswer++;
}
else
{
std::cout << "==============*======*======*==============\n"
<< " RESULT \n"
<< "==============*======*======*==============\n"
<< "Question: " << pollQuestion << "\n"
<< "Total Votes: " << totalVotes << "\n";
votePercentage = (firstAnswer/totalVotes)*100;
std::cout << answer1 << ": " << firstAnswer << " votes. | " << votePercentage << "\n";
votePercentage = secondAnswer/totalVotes*100;
std::cout << answer2 << ": " << secondAnswer << " votes. | " << votePercentage << "\n";
votePercentage = thirdAnswer/totalVotes*100;
std::cout << answer3 << ": " << thirdAnswer << " votes. | " << votePercentage << "\n";
return 0;
}
std::cout << "\nEnter your vote again\nOR\nuse 0 to show the results.\n";
std::cin >> pollChoice;
}
std::cout << "Error: Something went wrong!\n";
}
void startPoll (void)
{
std::cout << "Enter your poll question:\n";
getline (std::cin, pollQuestion, '\n');
std::cout << "Enter answer 1:\n";
getline (std::cin, answer1, '\n');
std::cout << "Enter answer 2:\n";
getline (std::cin, answer2, '\n');
std::cout << "Enter answer 3:\n";
getline (std::cin, answer3, '\n');
}
void showPoll (void)
{
std::cout << "==============|======|======|==============\n"
<< " POLL \n"
<< "==============|======|======|==============\n"
<< pollQuestion << "\n"
<< "1. " << answer1 << "\n"
<< "2. " << answer2 << "\n"
<< "3. " << answer3 << "\n\n"
<< "Enter 1,2 or 3:\n\n";
std::cin >> pollChoice;
pollCheck();
}
void pollCheck (void)
{
if (pollChoice != 1 && pollChoice != 2 && pollChoice != 3)
{
std::cout << "Wrong choice entered! Please try again.\n\n";
return showPoll();
}
}
You need to take care that integer/integer = integer. In your case, changing
(firstAnswer/totalVotes)*100
to
(1.0*firstAnswer/totalVotes)*100
or
(firstAnswer*100.0/totalVotes)
should work. They all give a floating point result.
Well, the solution for the Bar Chart could be the following:(Not written by me) I think thats very self explaining because its really basic
void line (int n, char c)
{
// this is the loop for n
for (int i = 0; i < n; i++)
cout << c << endl;
}
Here is my solution, you can see how I made the bars work by reading the comments.
#include <iostream>
using namespace std;
int main()
{
int a = 0;
int b = 0;
int c = 0;
cout << "What is your favorite animal? 1 Cat, ";
cout <<"2 Dog, 3 Fish, 0 Count votes" << endl;
//Choice counter
while (true)
{
int choice;
cout << "Choice: ";
cin >> choice;
if(choice == 1)
a++;
else if(choice == 2)
b++;
else if(choice == 3)
c++;
else if(choice == 0)
break;
else
continue;
}
cout << endl << " 1: " << a << endl;
cout << " 2: " << b << endl;
cout << " 3: " << c << endl;
cout << endl << "1\t" << "2\t" << "3\t" << endl;
//Finds the max voted option
int max = 0;
if(a > b && a > c)
max = a;
else if(b > c && b > a)
max = b;
else if(c > a && c > b)
max = c;
/* If the max voted option is bigger than 10, find by how much
we have to divide to scale the graph, also making 10 bar
units the max a bar can reach before scaling the others too */
int div =2;
if(max > 10)
{
do
{
max = max/div;
if(max < 10)
break;
div++;
}while(true);
}else
div = 1;
//Sets the final number for the bars
a=a/div;
b=b/div;
c=c/div;
if(a==0)
a++;
if(b==0)
b++;
if(c==0)
c++;
//Creates the bars
while(true)
{
if(a>0)
{
cout << "[]" << "\t";
a--;
}else
cout << " ";
if(b>0)
{
cout << "[]" << "\t";
b--;
}else
cout << " ";
if(c>0)
{
cout << "[]" << "\t";
c--;
}else
cout << " ";
cout << endl;
if(a==0 && b==0 && c==0)
break;
}
}