Thread: New, making a survey program

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    Mount Sterling, Ohio
    Posts
    8

    New, making a survey program

    Hello everyone im new to the forums and im sure your all going to be seeing much more of me Im in school for C++ programming, Im making a survey program for an assignment and one problem im coming across is;

    Im asking 30 questions; and i want it to save the answer to a variable, is their anyway to save the answers without having to make 30 variables?
    ex:

    cout << "is this a good code: ";
    cin >> Ans1

    and so far thats how it is for the rest just with 30 different answer variables.. IF not ill do that i was just wanting a quick and easier way.. if anyone can make any suggestions id greatly appreciate it. Thank you

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >is their anyway to save the answers without having to make 30 variables?
    There are two ways, depending on how your program works with the answers. If you can process each answer as it comes then there's no need for more than one variable. If you have to save all of the answers before processing them, you can use an array or some other container. An array should work for now, but if you don't know how many items you'll have to store, a vector is a much better choice.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Location
    Mount Sterling, Ohio
    Posts
    8
    Allright; ill have to read up on Arrays cause thats the common answer im getting so thats the route ima go it looks like. thanks for the help

  4. #4
    Registered User
    Join Date
    Nov 2006
    Location
    Mount Sterling, Ohio
    Posts
    8
    I should probably be using while statements im sure of right? well anyways i need to make it where if the code == 1/2 it mutiplies sales * .12 - and if the code is 3 * .15 and if the code is 4 * .20 - and if its not any of them numbers i need an error message? would a while statement make my life easier?
    Code:
    //calculation
        if (code == 1)
    	else (code == 2)
    	  totalsales = sales * .12 
    	  cout << "the total sale is: " << totalsale << endl;
    	else (code == 3)
    		totalsales = sales * .15
    		cout << "the total sale is: " << totalsale << endl;
    	else (code == 4)
    		totalsales = sales * .20
    		cout << "the total sale is: " << totalsale << endl;
    	else (code != 4)
    		cout << "You have made an error!"

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >would a while statement make my life easier?
    No, a while statement creates a loop. It executes a certain part of the code multiple times based on the condition you give it. What you want is a decision, which is done with if or switch statements:
    Code:
    if (code == 1 || code == 2)
      totalsales = sales * .12;
    else if (code == 3)
      totalsales = sales * .15;
    else if (code == 4)
      totalsales = sales * .20;
    else {
      cout << "You have made an error!";
      // Some kind of exit
    }
    
    cout << "the total sale is: " << totalsale << endl;
    Keep in mind that when you have more than one statement in a block, you need to use braces:
    Code:
    if ( something )
      one statement okay;
    
    if ( something else ) {
      one statement okay;
      another statement requires braces;
    }
    My best code is written with the delete key.

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Like prelude said, I would try to steer clear of C style arrays ie:

    Code:
    int mem[11 = { 1,2,3,4,5,6,7,8,9,10};
    Vectors are a much better choice, although there learning curve is slightly more difficult than arrays, which are great container types and can be mixed with iterators
    Double Helix STL

  7. #7
    Registered User
    Join Date
    Nov 2006
    Location
    Mount Sterling, Ohio
    Posts
    8
    allright thanks guys worked fine appreciate it

    On my survey program;

    what kind of variables should i set for Answers

    cout << "Question 1: what ur name: ";
    cin >> Answer1;

    what kind of variable should answer be declared as? ATM i have them set as strings and im not to sure

    Im not really to familiar with Arrays or vectors iv read about them but never used them just yet, so if it would be easier atm and quicker to just make 30 variables then ill do that
    Last edited by shaffer; 11-20-2006 at 08:25 AM.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    It will be easier not to use abbreviation like ATM (if you really want to get quick answer - it took me a good two minutes just to decode your writings)

    You can start with struct where each member will contain answer to corresponding question.
    Its type and name will be fully determined by the question.
    This will make a code readable enough but not to flexible.

    On the other side using vector of strings you will get very flexible code...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Nov 2006
    Location
    Mount Sterling, Ohio
    Posts
    8
    Allright sorry about that, ill be much more understandable now.
    Code:
    //Enter input data
    	cout << "Enter your full name: ";
    	getline(cin, name);
    	cout << "This survey consists of 30 basic questions";
    	cout << "Ready to begin? Yes/No";
    	cin >> ready;
    What im wondering now is i want to make it where when they Enter Yes it just continues on with the program; Now i can handle that but when they enter No their not ready i want it to just restart the program and im not sure how to do that and what kind of statement i would be using? my guess is a IF statement but thats why im asking, so appreciate if you could give shine a little light on me thank you

    also i was wondering whats the trigger to print?

    Im wanting to print at the end of the program like
    Name: name
    Answer1: ans1
    answer2: ans2

    from questions 1-30 id like to print out whats saved in the variable.
    Last edited by shaffer; 11-20-2006 at 09:23 AM.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    2
    If you want to print all variable of the array the fastest way i know of would be to do somthing like.
    Code:
    int x;
    for (x; x < 30; )
    {
    cout << array[x]
    x++;
    }
    This way it prints all your variables out, you might need to edit the numbers but it should work.

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    but when they enter No
    Code:
    int main()
    {
       while(!quiz())
       {
       }
    }
    
    int quiz(void)
    {
       //ask user
       if(answer is NO)
       {
          return 0;
       }
       //process quiz
       return 1;
    }
    But obviously it will be a little bit annoying - to ask the same question againg and againg till you get the answer you want. User has to have an option exit program without using Ctrl+C if he doesn't want to continue
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Registered User
    Join Date
    Nov 2006
    Location
    Mount Sterling, Ohio
    Posts
    8
    Code:
    int main()
    {
       while(!ready())
       {
       }
    }
    
    int ready(void)
    {
       //ask user
       if(answer is NO)
       {
          return 0;
       }
       //process quiz
       return 1;
    }
    what would i need to change in this to acctually start or restart the program, do i need a quiz variable to process it? can someone just maybe explain it a little more into depth and ill get it, if it cant really be explained into more depth thats fine just having a little trouble understanding it

  13. #13
    Registered User
    Join Date
    Nov 2006
    Location
    Mount Sterling, Ohio
    Posts
    8
    Code:
    // survey.cpp - Christmas Survey, The program will ask, user will answer, program will save answers then print.
    // Created/Revised by <Jeff Shaffer> on <11-27-06>.
    
    #include "stdafx.h"
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main()
    {
    	//declare variables
    	string name = "";
    	string ans1 = "";
    	string ans2 = "";
    	string ans3 = "";
    	string ans4 = "";
    	string ans5 = "";
    	string ans6 = "";
    	string ans7 = "";
    	string ans8 = "";
    	string ans9 = "";
    	string ans10 = "";
    	string ans11 = "";
    	string ans12 = "";
    	string ans13 = "";
    	string ans15 = "";
    	string ans15 = "";
    	string ans16 = "";
    	string ans17 = "";
    	string ans18 = "";
    	string ans19 = "";
    	string ans20 = "";
        int ready = "";
    
    	//Enter input data
    	cout << "Enter your full name: ";
    	getline(cin, name);
    	cout << "This survey consists of 20 basic questions";
    	cout << "Ready to begin? Yes/No";
    	cin >> ready;
        
    	//darw
    	cout << "Hot chocolate or apple cider: ";
    	cin >> ans1;
    	cout << "Turkey or Ham: ";
    	cin >> ans2;
    	cout << "Do you get a fake or 'real cut' Xmas tree: ";
    	cin >> ans3;
    	cout << "Decorations on the outside of your house: ";
    	cin >> ans4;
    	cout << "Snowball fights or sledding: ";
    	cin >> ans5;
    	cout << "Do you like hanging around the fireplace because its warm: ";
    	cin >> ans6;
    	cout << "Do you enjoy xmas shopping: ";
    	cin >> ans7;
    	cout << "Favorite christmas song: ";
    	cin >> ans8;
    	cout << "Take a break, you at 9/20 ";
    	cout << "how do you feel about christmas movies: ";
    	cin >> ans10;
    	cout << "when is it to early to start listening to christmas music: ";
    	cin >> ans11;
    	cout << "Stockings before or after presents: ";
    	cin >> ans12;
    	cout << "do you like christmas carolers: ";
    	cin >> ans13;
    	cout << "go to someone elses house or they come to yours: ";
    	cin >> ans14;
    	cout << "do you read the christmas story the night before christmas: ";
    	cin >> ans15;
    	cout << "Question16: ";
    	cin >> ans16;
    	cout << "Question17: ";
    	cin >> ans17;
    	cout << "Question18: ";
    	cin >> ans18;
    	cout << "Question19: ";
    	cin >> ans19;
    	cout << "Question20: ";
    	cin >> ans20;
    	cout << "Thanks for taking our survey, Your all finished";
    
    
    	  return 0;
    	} // end of main function
    for some reason the program wont even begin to work, as soon as i start it it just says " press any key to continue " and no errors show up..

  14. #14
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Personally I would do it like this:
    Code:
    // survey.cpp - Christmas Survey, The program will ask, user will answer, program will save answers then print.
    // Created/Revised by <Jeff Shaffer> on <11-27-06>.
    
    #include "stdafx.h"
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	//declare variables
    	string name;
    	string ans1;
    	string ans2;
    	string ans3;
    	string ans4;
    	string ans5;
    	string ans6;
    	string ans7;
    	string ans8;
    	string ans9;
    	string ans10;
    	string ans11;
    	string ans12;
    	string ans13;
    	string ans14;
    	string ans15;
    	string ans16;
    	string ans17;
    	string ans18;
    	string ans19;
    	string ans20;
        int ready;
    
    	//Enter input data
    	cout << "Enter your full name: ";
    	getline(cin, name);
    	cout << "This survey consists of 20 basic questions\n";
    	cout << "Ready to begin? (1 if you are, 2 if you are not)";
    	cin >> ready;
            if ( ready == 1 ) {
    	  //darw
    	  cout << "\n\nHot chocolate or apple cider: ";
    	  getline ( cin, ans1 );
    	  cout << "\n\nTurkey or Ham: ";
    	  getline ( cin, ans2 );
    	  cout << "\n\nDo you get a fake or 'real cut' Xmas tree: ";
    	  getline ( cin, ans3 );
    	  cout << "\n\nDecorations on the outside of your house: ";
    	  getline ( cin, ans3 );
    	  cout << "\n\nSnowball fights or sledding: ";
    	  getline ( cin, ans3 );
    	  cout << "\n\nDo you like hanging around the fireplace because its warm: ";
    	  getline ( cin, ans3 );
    	  cout << "\n\nDo you enjoy xmas shopping: ";
    	  getline ( cin, ans3 );
    	  cout << "\n\nFavorite christmas song: ";
    	  getline ( cin, ans3 );
    	  cout << "\n\nhow do you feel about christmas movies: ";
    	  getline ( cin, ans3 );
    	  cout << "\n\nwhen is it to early to start listening to christmas music: ";
    	  getline ( cin, ans3 );
    	  cout << "\n\nStockings before or after presents: ";
    	  getline ( cin, ans3 );
    	  cout << "\n\ndo you like christmas carolers: ";
    	  getline ( cin, ans3 );
    	  cout << "\n\ngo to someone elses house or they come to yours: ";
    	  getline ( cin, ans3 );
    	  cout << "\n\ndo you read the christmas story the night before christmas: ";
    	  getline ( cin, ans3 );
    	  cout << "\n\nQuestion16: ";
    	  getline ( cin, ans3 );
    	  cout << "\n\nQuestion17: ";
    	  getline ( cin, ans3 );
    	  cout << "\n\nQuestion18: ";
    	  getline ( cin, ans3 );
    	  cout << "\n\nQuestion19: ";
    	  getline ( cin, ans3 );
    	  cout << "\n\nQuestion20: ";
    	  getline ( cin, ans3 );
    	  cout << "\n\nThanks for taking our survey, Your all finished";
              cin.get();
            }
            else if ( ready == 2 ) {
              cout<< "not ready";
              cin.get();
            }
    	 return 0;
    } // end of main function
    But, I'm a noob

  15. #15
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    You can't declare an integer with an empty string. Also you should set all those answers as an array of strings to simplify your code ad make it easier to read. You also don't need to decalre them to empty strings because unless you are appending something, it is not necessary.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making a program run on startup
    By Poincare in forum C Programming
    Replies: 10
    Last Post: 06-21-2009, 12:50 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Making interest rate program in C
    By canadas321 in forum C Programming
    Replies: 6
    Last Post: 06-23-2005, 11:59 AM
  5. I need help with making a program which....
    By Tonyukuk in forum C# Programming
    Replies: 1
    Last Post: 04-16-2003, 10:49 PM