Thread: Simple survey

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    7

    Simple survey

    I have an assignment for class that I can't figure out how to start. I'm supposed to create a survey that records a number of peoples drink choices and tells the user how many times each drink was selected out of the menu at the end. I tried to start it but i hit a wall.

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        //Variables defined for drink choices
        const int COFFEE = 1,
                  TEA = 2,
                  COKE= 3,
                  OJ = 4;
                int exit;
                int people;
                int num;
                int choice;
    
                //Menu for Choices
                cout<<"How many people will be taking this survey?\n";
                cin>>people;
    
                for (num =0; num<people;num++)
                cout<<"Favorite Beverage Choices\n1.Coffee\n2.Tea\n3.Coke\n4.OJ\n-1 to exit the program\n\n";
    
    
                  cout<<"Please input favorite beverage of person #"<<num<<endl;
                  cout<< "Press 1,2,3, or 4 from the above menu. Press -1 to exit.\n";
                  cin>>choice;
    
    
    
    
        return 0;
    }

    any ideas?

  2. #2
    Registered User
    Join Date
    Feb 2014
    Posts
    7
    i forgot to add the sample output that was provided

    Please input the favorite beverage of person #1: Ch
    oose 1, 2, 3, or 4 from the
    above menu or -1 to exit the program
    4
    Please input the favorite beverage of person #2: Ch
    oose 1, 2, 3, or 4 from the
    above menu or -1 to exit the program
    1
    Please input the favorite beverage of person #3: Ch
    oose 1, 2, 3, or 4 from the
    above menu or -1 to exit the program
    3

    Please input the favorite beverage of person #4: Ch
    oose 1, 2, 3, or 4 from the
    above menu or -1 to exit the program
    1
    Please input the favorite beverage of person #5: Ch
    oose 1, 2, 3, or 4 from the
    above menu or -1 to exit the program
    1
    Please input the favorite beverage of person #6: Ch
    oose 1, 2, 3, or 4 from the
    above menu or -1 to exit the program
    -1
    The total number of people surveyed is 5. The resul
    ts are as follows:
    Beverage Number of Votes
    ********************************
    Coffee 3
    Tea 0
    Coke 1
    Orange Juice 1

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Start with a flowchart or pseudo code. Post that and let's look at translating it to real code.
    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.

  4. #4
    Registered User
    Join Date
    Feb 2014
    Posts
    7
    I came up with a pseudo code but i couldn't figure out how to start i dove in. This is my first programming class. I came up with a rough draft but it won't keep track of the answers given

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        //Variables defined for drink choices
         int COFFEE = 0,
                  TEA = 0,
                  COKE= 0,
                  OJ = 0;
                int exit;
                int people;
                int num=1;
                int choice;
                int totalC=0,
                    totalT=0,
                    totalCk=0,
                    totalO=0;
    
                //finding the number of time the questions will be asked
                cout<<"How many people will be taking this survey?\n";
                cin>>people;
                //menu for beverage choices 
                while(num<=people)
                {
                cout<<"Favorite Beverage Choices\n1.Coffee\n2.Tea\n3.Coke\n4.OJ\n-1 to exit the program\n\n";
    
                  cout<<"Please input favorite beverage of person #"<<num<<endl;
                  cout<< "Press 1,2,3, or 4 from the above menu. Press -1 to exit.\n\n";
                  cin>>choice;
                  num++;
                }
    //switch to tally up the choices to be displayed at the end
    switch(choice)
    {
        case '1': (totalC++);
            break;
        case'2': (totalT++);
            break;
        case'3': (totalCk++);
            break;
        case '4': (totalO++);
            break;
        default: cout<< "Invalid selection\n";
            break;
    }
    //display of total picks
    cout<<"The number of people surveyed is "<<people<<".\n\n";
    cout<<"Beverage Vote List\n";
    cout<<"*********************************\n";
    cout<<"Coffee:"<<totalC<<endl;
    cout<<"Tea:"<<totalT<<endl;
    cout<<"Coke:"<<totalCk<<endl;
    cout<<"Orange Juice:"<<totalO<<endl;
    
    
        return 0;
    }

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by PHDpaul View Post
    I came up with a pseudo code but i couldn't figure out how to start i dove in. This is my first programming class. I came up with a rough draft but it won't keep track of the answers given
    That's why you need pseudo code.
    Ignore the programming language for now.
    Think about, if you were given this task in real life, how you would accomplish it in small logical steps. Then write those down.
    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.

  6. #6
    Registered User
    Join Date
    Feb 2014
    Posts
    7
    ok i guess i'd first find out how many people need to be surveyed. I'd then show my question to someone and record their response, and repeat that until i had asked everyone and gotten their answers. Then i'd tally them up and display them

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Right. That's a good start. But also consider that judging from the output above, it doesn't say that you know how many will be surveyed, so keep that in mind.
    Now, the next step after amending your approach to the fact above, is to break it down into more logical steps. Computers are stupid, and we need to guide them.
    How do you show a question to someone?
    How do you record their response?
    How do you tally up the answers?
    How do you display them?
    We're not talking about just one person here, we're talking about many. So try to break it up into smaller steps by taking that into account.

    For example, given a task to count the number of sheep, I might

    For every sheep
    Increment the number of sheeps by one
    Loop
    Print the total number of sheeps
    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.

  8. #8
    Registered User
    Join Date
    Feb 2014
    Posts
    7
    hmmmmm ok i see what you're saying now. So i'd ask for the number of people, input that number, set the question to loop 'x' number of times, everytime a beverage is selected it would increment by one until the loop is completed, and i'd cout the variables after they'd been incremented.

  9. #9
    Registered User
    Join Date
    Feb 2014
    Posts
    7
    wow i just got it to work. definitely just had to take the steps through one by one. Thanks man, i had been working on this for hours

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Good work. If you get stuck, remember these basic exercises!
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Survey -- All Programmers
    By kakayoma in forum General Discussions
    Replies: 9
    Last Post: 12-11-2009, 09:43 AM
  2. Survey ProJect Need Help
    By tHePrOfEsSor_14 in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2005, 09:07 PM
  3. Computer programmer (yet another survey)
    By jaylc185 in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 05-17-2005, 10:27 PM
  4. Public Survey! Please Take!
    By DeanDemon in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 12-09-2002, 03:29 AM

Tags for this Thread