PDA

View Full Version : Help with survey program


jeffshaffer
11-30-2006, 03:21 PM
can anyone tell me why my program wont even begin to run? it just says press any key to continue as soon as i start it and their is no errors..
// survey.cpp - This is a survey of 30 basic questions, 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

Aradon
11-30-2006, 04:45 PM
While I am no c++ expert, my first instinct is to ask which compiler you're using and what exactly is in stdafx.h? I know some people include string in their stdafx files but in others they don't.

I'm sure someone else has more insight, this is just my first impressions :)

jeffshaffer
11-30-2006, 04:49 PM
Im using Visual C++ 2005 express edition, and as for whats in my stdafx.h file im not quite sure lol

brad211987
11-30-2006, 06:17 PM
Are you sure "stdafx.h" is the right library? I haven't done c++ since my junior year of high school, but I thought it was something else that was needed. And it just exits the program when you "press any key" correct?

Fatmumuhomer
11-30-2006, 08:36 PM
There were no errors at all? Hmm. Somethings seems wrong with that.

For instance, you declared string ans15 = ""; twice in a row. That should have caused some kind of an error.

Plus I had to declare that I was using the std namespace to create any strings.
i.e. using namespace std;

Also, stdafx is just a precompiled header file used in Visual Studio to enable quicker compiling. It shouldn't have anything to do with him outputting to a console window. I'll have to play around with this some more. I'm running Visual Studio .NET 2003 and the code doesn't compile for me as is.

mentalhorse
11-30-2006, 09:41 PM
Try declaring your variables outside the main function (as in before it). And...
string ans1 = ""; <-- somewhat bad
do this
string as1;
int ready = ""; <-- that is bad
just do this
int ready;

rpgfan3233
11-30-2006, 09:51 PM
I recommend the using namespace std; as Fatmumuhomer did. The reason why is because things don't seem to be working quite right as is:
std::cout
std::cin
std::endl
std::getline
std::string

You seem to be missing the last two. Remember that there is a difference between std::cin.getline() (iostream) and std::getline() (string).

jeffshaffer
12-01-2006, 03:33 PM
Allright I played around with everything fixed my small errors; and I got it running but the problem im experiencing now well heres the code
// survey.cpp - This is a survey of 20 Christmas questions, 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>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

int main()
{
//Declare string 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 = "";


//Enter input data
cout << "Enter your full name: ";
getline(cin, name);
cout << "This survey consists of 20 basic questions"<<endl;


//Program asks the user the question, user types in answer and program saves the answer
cout << "Hot chocolate or apple cider: ";
getline(cin, ans1);
cout << "Turkey or Ham: ";
getline(cin, ans2);
cout << "Do you get a fake or 'real cut' Xmas tree: ";
getline(cin, ans3);
cout << "Decorations on the outside of your house: ";
getline(cin, ans4);
cout << "Snowball fights or sledding: ";
getline(cin, ans5);
cout << "Do you like hanging around the fireplace because its warm: ";
getline(cin, ans6);
cout << "Do you enjoy xmas shopping: ";
getline(cin, ans7);
cout << "Favorite christmas song: ";
getline(cin, ans8);
cout << "Take a break, you at 9/20 "<<endl;
cout << "how do you feel about christmas movies: ";
getline(cin, ans9);
cout << "when is it to early to start listening to christmas music: ";
getline(cin, ans10);
cout << "Stockings before or after presents: ";
getline(cin, ans11);
cout << "do you like christmas carolers: ";
getline(cin, ans12);
cout << "go to someone elses house or they come to yours: ";
getline(cin, ans13);
cout << "do you read the christmas story the night before christmas: ";
getline(cin, ans14);
cout << "Do you enjoy christmas?: ";
getline(cin, ans15);
cout << "Did you get someone a gift: ";
getline(cin, ans16);
cout << ": Do you think you'll get any presents: ";
getline(cin, ans17);
cout << "what do you eat for christmas dinner: ";
getline(cin, ans18);
cout << "Do you beleive in Santa Clause: ";
getline(cin, ans19);
cout << "Thanks for taking our survey, Your all finished" << endl;

//Displaying the answers for the survey instructor to copy down
cout << "Please do not close the program, The instructor will now begin to copy down your answers" >> endl;
cout << "Answer #1:" << ans1 << endl;
cout << "Answer #2:" << ans2 << endl;
cout << "Answer #3:" << ans3 << endl;
cout << "Answer #4:" << ans4 << endl;
cout << "Answer #5:" << ans5 << endl;
cout << "Answer #6:" << ans6 << endl;
cout << "Answer #7:" << ans7 << endl;
cout << "Answer #8:" << ans1 << endl;
cout << "Answer #9:" << ans8 << endl;
cout << "Answer #10:" << ans10 << endl;
cout << "Answer #11:" << ans11 << endl;
cout << "Answer #12:" << ans12 << endl;
cout << "Answer #13:" << ans13 << endl;
cout << "Answer #14:" << ans14 << endl;
cout << "Answer #15:" << ans15 << endl;
cout << "Answer #16:" << ans16 << endl;
cout << "Answer #17:" << ans17 << endl;
cout << "Answer #18:" << ans18 << endl;
cout << "Answer #19:" << ans19 << endl;
cout << "Answer #20:" << ans20 << endl;

} // end of main function
I get like 45 error when i compile that, Their random errors that talk about stuff that does involve my code at all, IT worked fine until I added

//Displaying the answers for the survey instructor to copy down
cout << "Please do not close the program, The instructor will now begin to copy down your answers" >> endl;
cout << "Answer #1:" << ans1 << endl;
cout << "Answer #2:" << ans2 << endl;
cout << "Answer #3:" << ans3 << endl;
cout << "Answer #4:" << ans4 << endl;
cout << "Answer #5:" << ans5 << endl;
cout << "Answer #6:" << ans6 << endl;
cout << "Answer #7:" << ans7 << endl;
cout << "Answer #8:" << ans1 << endl;
cout << "Answer #9:" << ans8 << endl;
cout << "Answer #10:" << ans10 << endl;
cout << "Answer #11:" << ans11 << endl;
cout << "Answer #12:" << ans12 << endl;
cout << "Answer #13:" << ans13 << endl;
cout << "Answer #14:" << ans14 << endl;
cout << "Answer #15:" << ans15 << endl;
cout << "Answer #16:" << ans16 << endl;
cout << "Answer #17:" << ans17 << endl;
cout << "Answer #18:" << ans18 << endl;
cout << "Answer #19:" << ans19 << endl;
cout << "Answer #20:" << ans20 << endl;

Someone shine some light on me? ALSO my original plans were to PRINT the answers but i wasnt sure the print syntax.. if anyone could maybe tell me that then I would rather print then just say the answers back. Thank you

mentalhorse
12-02-2006, 02:55 PM
cout << "Please do not close the program, The instructor will now begin to copy down your answers" >> endl;
That should be a '<<' at the end their.