Beginner C++ Beverage Survey Tally -


i working on assignment supposed perform survey tally on people's favorite beverages. ideally continue asking user submit favorite beverage each individual wish include. when user done casting of votes, or enters -1 , count should displayed. problem noticing when results printed out, arbitrarily large numbers each beverage. still new coding , newer c++, have scope of switch statement? doesn't appear adding count of beverages @ :/.

#include <iostream> #include <stdlib.h>  using namespace std;  int main() {     int input, vcoff, vtea, vcoke, voj, personnumber = 1;       cout << "welcome favorite beverage survey. our beverage options are: "<< endl;      cout << "1.coffee   2.tea   3.coke   4.orange juice" << endl;      cout << endl;      cout << "please input favorite beverage of person # " << personnumber << ": choose 1, 2, 3, or 4 the"<< endl << "above menu or -1 exit program."<< endl;      cin >> input;    while (input < -1)   {     cout << "that not option. please reenter valid number: " << endl;     cin >> input;   }     while (input > 0)   {     switch(input)     {     case 1: vcoff++;     break;     case 2: vtea++;     break;     case 3: vcoke++;     break;     case 4: voj++;     break;     default: cout << "sorry, that's not menu item. please try again: " << endl;     cin >> input;     }     personnumber++;      cout << "please input favorite beverage of person # " << personnumber << ": choose 1, 2, 3, or 4 the"<< endl << "above menu or -1 exit program."<< endl;      cin >> input;   }    if (input = -1)  {    cout << "the total number of people surveyed " << personnumber << ". results follows:" << endl;    cout << "beverages     votes" << endl << "**********************" << endl;    cout << "coffee:    " << vcoff << endl;    cout << "tea:    " << vtea << endl;    cout << "coke:    " << vcoke << endl;    cout << "orange juice:    " << voj << endl;  }  return 0; } 

you missed initialisation of variables. local variables not initialised 0 if don't tell so.

this means starting value can unpredicatable value, explains find weird numbers.

try:

int input=0, vcoff=0, vtea=0, vcoke=0, voj=0, personnumber = 1; 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -