You are viewing our Forum Archives. To view or take place in current topics click here.
I need help with my code, C++ ill +Rep
Posted:
I need help with my code, C++ ill +RepPosted:
Status: Offline
Joined: May 31, 201410Year Member
Posts: 8
Reputation Power: 0
Status: Offline
Joined: May 31, 201410Year Member
Posts: 8
Reputation Power: 0
File: OpinionDriver.cpp
// Chapter 3, Problem 46
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//dim variable
int main()
{
//Display info "Chapter 3, Problem 46 -- Opinion Poll"; This program will present the user with a question and 5 possible answers for a response. You may vote multiple times and the results will show the voting and percentage of votes for each answer.
string answer;
cout << "Chapter 3, Problem 46 -- Opinion Poll";
cout << "\n\nThis program will present the user with a question and 5 possible "
<< "\nanswers for a response. You may vote multiple times and the results "
<< "\nwill show the voting and percentage of votes for each answer.";
do
{
float fKeyLime=0.0,fChocCake=0.0,fChocPudding=0.0,fPieAlaMode=0.0,fDifferent=0.0;
float iKeyLime=0,iChocCake=0, iChocPudding=0, iPieAlaMode=0, iDifferent=0, iInvalid=0;
int iVchoice=0, iTotal=0; // vote choice, Total = total # of votes
bool bInvalid=false;
//Dim variables with values
do
{
//Display ------------- O P I N I O N P O L L -------------";
cout << "------------- O P I N I O N P O L L -------------\n";
//Display What is your favorite type of dessert? ";
cout << "\nWhat is your favorite type of dessert?\n ";
//Display 1. Key Lime Pie 2. Chocolate Cake 3. Chocolate Pudding 4. Apple Pie with Ice Cream 5. I have a different dessert 6. QuitVoting Enter your vote (1-5 OR Enter 6 to quit) ;
cout << "\n1. Key Lime Pie";
cout << "\n2. Chocolate Cake";
cout << "\n3. Chocolate Pudding";
cout << "\n4. Apple Pie with Ice Cream";
cout << "\n5. I have a different dessert";
cout << "\n6. Quit Voting";
cout << "\n\nEnter your vote (1-5 OR Enter 6 to quit)" ;
//Get input vote from user
cin >> answer
//use case structure to total votes
switch(iVchoice)
{
case 1:
++iKeyLime;
break;
case 2:
++iChocCake;
break;
case 3:
++iChocPudding;
break;
case 4:
++iPieAlaMode;
break;
case 5:
++iDifferent;
break;
//continue
default:
++iInvalid;
bInvalid = true;
//not invalid, count the vote
//it is invalid, already counted above, so set back to false
}
}while(iVchoice != 6);
// CALCULATE THE PERCENTAGES...
// OUTPUT TO THE SCREEN
cout << "\n\n---------- O P I N I O N P O L L R E S U L T S ----------";
cout << "\n\n\tITEM\t\t\tVOTES\t\tPERCENT";
cout << "\nKey Lime Pie\t\t\t" << setw(3)<<(int)iKeyLime << "\t\t"
<< setw(6)<<fKeyLime;
cout << "\nChocolate Cake\t\t\t" << setw(3)<< (int)iChocCake << "\t\t"
<<setw(6)<< fChocCake;
cout << "\nChocolate Pudding\t\t" << setw(3)<< (int)iChocPudding << "\t\t"
<< setw(6)<<fChocPudding;
cout << "\nApple Pie w/ice Cream\t\t" << setw(3)<< (int)iPieAlaMode << "\t\t"
<< setw(6)<<fPieAlaMode;
cout << "\nDifferent Choice\t\t" << setw(3)<< (int)iDifferent << "\t\t"
<<setw(6)<< fDifferent;
cout << "\n\nValid Votes -- " << (int)iTotal;
cout << "\nInvalid Votes -- " << (int)iInvalid;
cout << "\n Take the poll again? yes/no ";
cin >> answer;
}while(answer == "yes");
cout << "\n\n Remember, vote early, vote often! ; - ) \n\n";
return 0;
}
this is all I have so far, I don't understand vectors and I don't know how to write the math part on it either. Thanks!
Last edited by MeatMaster69 ; edited 1 time in total
#2. Posted:
Status: Offline
Joined: May 31, 201410Year Member
Posts: 8
Reputation Power: 0
Status: Offline
Joined: May 31, 201410Year Member
Posts: 8
Reputation Power: 0
Any help at all will be greatly appreciated, I was put into a class that is 3 chapters ahead of me. I feel like any time homework is assigned in the class
- 0useful
- 0not useful
#3. Posted:
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
I don't know whether I misunderstood this, but you are have "cin >> answer" and then use iVchoice in the switch statement.
Surely you would want to be using
switch(answer)?
Surely you would want to be using
switch(answer)?
- 0useful
- 0not useful
You are viewing our Forum Archives. To view or take place in current topics click here.