Thread: revisiting my quiz

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    38

    revisiting my quiz

    hey so i posted my quiz up here for help a while ago and it was working perfectly when i clicked run without debugging... untill i tried to run it with debugging. when i do that it crashes right before I get my answer. please help.
    Code:
    #include <iostream>
    #include <algorithm>
    #include <string>
    
    using namespace std;
    
    int main ()
    {
    	int think;
    	int bro = 0;
    	int jock = 0;
    	int art = 0;
    	int rock = 0;
    	int stoner = 0;
    	int nerd = 0;
    	int rich = 0;
    	int ba = 0;
    	int a;
    	int b;
    	int c;
    	int d;
    	int e;
    	int f;
    	int g;
    
    	cout<<"THE HIGHSCHOOL STERIOTYPE QUIZ\n";
    	cout<<"\n";
    	cout<<"How To Play:\n";
    	cout<<"You will be asked a series of questions to determine your personality, \n"
    	      "answer truthfully by typing the number next to the response that fits you best \n"
    	      "and then hitting the enter key. In the event that multiple responses fit you, \n"
    	      "DO NOT type both numbers, choose the one that fits you best.\n";
    	cout<<"\n";
    	cout<<"Are you ready to find out what highschool steriotype you are? \nPress enter to find out!\n";
    	cin.get();
    
    	cout<<"What would you classify yourself as? (does not count towards final result)\n";
    	cout<<"\n";
    	cout<<"1. Lax Bro\n";
    	cout<<"2. Jock\n";
    	cout<<"3. Artist\n";
    	cout<<"4. Rocker\n";
    	cout<<"5. Stoner\n";
    	cout<<"6. Nerd\n";
    	cout<<"7. Rich Kid\n";
    	cout<<"or are you...\n";
    	cout<<"8. BEYOND AUTHORITY!!!\n";
    	cout<<"\n";
    	cin>> think;
    	cout<<"\n";
    
    	cout<<"QUESTION 1\n";
    	cout<<"\n";
    	cout<<"What's your favorite sport?\n";
    	cout<<"\n";
    	cout<<"1. Football\n";
    	cout<<"2. Do gigs count as sports?\n";
    	cout<<"3. Baseball\n";
    	cout<<"4. Lacrosse\n";
    	cout<<"5. Sports?\n";
    	cout<<"6. I'm too good for sports\n";
    	cout<<"\n";
    	cin>> a;
    	cout<<"\n";
    
    	switch (a) {
    		case 1:
    			jock = jock + 1;
    			break;
    		case 2:
    			rock = rock + 1;
    			break;
    		case 3:
    			rich = rich + 1;
    			break;
    		case 4:
    			bro = bro + 1;
    			break;
    		case 5:
    			art = art + 1;
    			nerd = nerd + 1;
    			stoner = stoner + 1;
    			break;
    		case 6:
    			ba = ba + 1;
    			break;
    		default:
    			cout<<"Wow... Is it really that hard to put in the right number? GET OUT!\n";
    			return 0;
    	}
    
    	cout<<"QUESTION 2\n";
    	cout<<"\n";
    	cout<<"Paarrrtaaayyy! Where are you?\n";
    	cout<<"\n";
    	cout<<"1. Hosting it\n";
    	cout<<"2. Mackin mad biddies\n";
    	cout<<"3. At home...\n";
    	cout<<"4. Attempting to dance with some hot girl I don't know\n";
    	cout<<"5. In the corner providing people with some extra \"happyness\"\n";
    	cout<<"6. On stage making the music happen\n";
    	cout<<"7. Beating up the jock that's attempting to dance with my girlfriend\n";
    	cout<<"8. Playing games with some of my close friends\n";
    	cout<<"\n";
    	cin>> b;
    	cout<<"\n";
    
    	switch (b) {
    		case 1:
    			rich = rich + 1;
    			break;
    		case 2:
    			bro = bro + 1;
    			break;
    		case 3:
    			nerd = nerd + 1;
    			stoner = stoner + 1;
    			break;
    		case 4:
    			jock = jock + 1;
    			break;
    		case 5:
    			stoner = stoner + 1;
    			break;
    		case 6:
    			rock = rock + 1;
    			break;
    		case 7:
    			ba = ba + 1;
    			break;
    		case 8:
    			art = art + 1;
    			break;
    		default:
    			cout<<"Wow... Is it really that hard to put in the right number? GET OUT!\n";
    			return 0;
    	}
    
    	cout<<"QUESTION 3\n";
    	cout<<"\n";
    	cout<<"What's your favorite kind of art?\n";
    	cout<<"\n";
    	cout<<"1. Paintings of things BURNING!\n";
    	cout<<"2. Modern art, stuff that makes me think\n";
    	cout<<"3. Dark impressionism, art with emotion\n";
    	cout<<"4. Anything with a political statement\n";
    	cout<<"5. The platinum and diamond eagle above my bed\n";
    	cout<<"6. Roman sculptures of the physical body\n";
    	cout<<"\n";
    	cin>> c;
    	cout<<"\n";
    
    	switch (c) {
    		case 1:
    			ba = ba + 1;
    			break;
    		case 2:
    			nerd = nerd + 1;
    			art = art + 1;
    			break;
    		case 3:
    			stoner = stoner + 1;
    			art = art + 1;
    			break;
    		case 4:
    			rock = rock + 1;
    			break;
    		case 5:
    			rich = rich + 1;
    			break;
    		case 6:
    			jock = jock + 1;
    			bro = bro + 1;
    			break;
    		default:
    			cout<<"Wow... Is it really that hard to put in the right number? GET OUT!\n";
    			return 0;
    	}
    
    	cout<<"QUESTION 4\n";
    	cout<<"\n";
    	cout<<"What's your favorite food?\n";
    	cout<<"\n";
    	cout<<"1. Crack\n";
    	cout<<"2. Filet mignon\n";
    	cout<<"3. Protein shakes\n";
    	cout<<"4. Astronaut food packets\n";
    	cout<<"5. Anything prepared fancy\n";
    	cout<<"6. I don't eat\n";
    	cout<<"7. Burgers and fries\n";
    	cout<<"\n";
    	cin>> d;
    	cout<<"\n";
    
    	switch (d) {
    		case 1:
    			stoner = stoner + 1;
    			break;
    		case 2:
    			rich = rich + 1;
    			break;
    		case 3:
    			jock = jock + 1;
    			break;
    		case 4:
    			nerd = nerd + 1;
    			break;
    		case 5:
    			art = art + 1;
    			break;
    		case 6:
    			ba = ba + 1;
    			break;
    		case 7:
    			bro = bro + 1;
    			rock = rock + 1;
    			break;
    		default:
    			cout<<"Wow... Is it really that hard to put in the right number? GET OUT!\n";
    			return 0;
    	}
    
    	cout<<"QUESTION 5\n";
    	cout<<"\n";
    	cout<<"What's your favorite music?\n";
    	cout<<"\n";
    	cout<<"1. Metal\n";
    	cout<<"2. Rap\n";
    	cout<<"3. Reggae\n";
    	cout<<"4. Hard rock\n";
    	cout<<"5. Classical\n";
    	cout<<"6. Alternative\n";
    	cout<<"7. Mainstream Pop\n";
    	cout<<"\n";
    	cin>> e;
    	cout<<"\n";
    
    	switch (e) {
    		case 1:
    			stoner = stoner + 1;
    			break;
    		case 2:
    			jock = jock + 1;
    			break;
    		case 3:
    			bro = bro + 1;
    			break;
    		case 4:
    			rock = rock + 1;
    			ba = ba + 1;
    			break;
    		case 5:
    			art = art + 1;
    			nerd = nerd + 1;
    			break;
    		case 6:
    			art = art + 1;
    			ba = ba + 1;
    			break;
    		case 7:
    			cout<<"Wow... you have horrible taste in music. GET OUT!\n";
    			return 0;
    		default:
    			cout<<"Wow... Is it really that hard to put in the right number? GET OUT!\n";
    			return 0;
    	}
    
    	cout<<"QUESTION 6\n";
    	cout<<"\n";
    	cout<<"Where do you want to go to college?\n";
    	cout<<"\n";
    	cout<<"1. MIT\n";
    	cout<<"2. Party school\n";
    	cout<<"3. Design school\n";
    	cout<<"4. Ivy league school\n";
    	cout<<"5. Community college\n";
    	cout<<"6. Duke or North Carolina\n";
    	cout<<"7. Berkeley school of music\n";
    	cout<<"\n";
    	cin>> f;
    	cout<<"\n";
    
    	switch (f) {
    		case 1:
    			nerd = nerd + 1;
    			break;
    		case 2:
    			rich = rich + 1;
    			ba = ba + 1;
    			break;
    		case 3:
    			art = art + 1;
    			break;
    		case 4:
    			bro = bro + 1;
    			break;
    		case 5:
    			stoner = stoner + 1;
    			break;
    		case 6:
    			art = art + 1;
    			ba = ba + 1;
    			break;
    		case 7:
    			rock = rock + 1;
    			break;
    		default:
    			cout<<"Wow... Is it really that hard to put in the right number? GET OUT!\n";
    			return 0;
    	}
    
    	cout<<"FINAL QUESTION\n";
    	cout<<"QUESTION 7\n";
    	cout<<"\n";
    	cout<<"Who's your best friend(s)\n";
    	cout<<"\n";
    	cout<<"1. My band-mates\n";
    	cout<<"2. My drug dealer\n";
    	cout<<"3. My friends since kindergarden\n";
    	cout<<"4. I set them all on FIRE!!!\n";
    	cout<<"5. My team-mates\n";
    	cout<<"6. My butler\n";
    	cout<<"7. The kids in math team\n";
    	cout<<"8. Chill bros\n";
    	cout<<"\n";
    	cin>> g;
    	cout<<"\n";
    
    	switch (g) {
    		case 1:
    			rock = rock + 1;
    			break;
    		case 2:
    			stoner = stoner + 1;
    			break;
    		case 3:
    			art = art + 1;
    			break;
    		case 4:
    			ba = ba + 1;
    			break;
    		case 5:
    			jock = jock + 1;
    			break;
    		case 6:
    			rich = rich + 1;
    			break;
    		case 7:
    			nerd = nerd + 1;
    			break;
    		case 8:
    			bro = bro + 1;
    			break;
    		default:
    			cout<<"Wow... Is it really that hard to put in the right number? GET OUT!\n";
    			return 0;
    	}
    
    	cout<<"\n\nProcessing results... Press enter to continue\n\n";
    	cin.get();
    
    		switch (think) {
    		case 1:
    			cout<<"You thought you were a lax bro\n\n";
    			break;
    		case 2:
    			cout<<"You thought you were a jock\n\n";
    			break;
    		case 3:
    			cout<<"You thought you were an artist\n\n";
    			break;
    		case 4:
    			cout<<"You thought you were a rocker\n\n";
    			break;
    		case 5:
    			cout<<"You thought you were a stoner\n\n";
    			break;
    		case 6:
    			cout<<"You thought you were a nerd\n\n";
    			break;
    		case 7:
    			cout<<"You thought you were a rich kid\n\n";
    			break;
    		case 8:
    			cout<<"You thought you were BEYOND AUTHORITY!!!\n\n";
    			break;
    		default:
    			break;
    	}
    	
    		int stereotypes[] = { bro, jock, art, rock, stoner, nerd, rich, ba };
    		size_t num_stereotypes = sizeof(stereotypes) / sizeof(stereotypes[0]);
    		int* player_stereotype = max_element(stereotypes, stereotypes + num_stereotypes);
    		string stereotype_names[] = {"lax bro", "jock", "artist", "rocker",
                                 "stoner", "nerd", "rich kid", "BEYOND AUTHORITY!!!"};
    		cout << "You are really a " << stereotype_names[player_stereotype - stereotypes] << "!\n";
    }
    Last edited by CornedBee; 06-30-2010 at 01:48 AM.

  2. #2
    Registered User
    Join Date
    Oct 2009
    Posts
    38
    Please help

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    Why don't you post the error also?

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    38
    its not an error. it compiles perfectly and runs perfectly if i dont debug but if i try to debug it quits out early right before the end and also it quits out in the default on the switch cases without displaying the text. I tried putting cin.get(); for it to wat for you to press enter before quitting but it didnt do anything.

    edit: the end is working fine now. now its just crashing on the defaults in the switch cases.
    Last edited by dyelax; 06-28-2010 at 09:08 PM.

  5. #5
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Code:
    		int* player_stereotype = max_element(stereotypes, stereotypes + num_stereotypes);
    This one seems wrong. On the second parameter, you're adding the value of a pointer with an integer. So consider this possible case:
    On the line above it there's this code:

    Code:
    		size_t num_stereotypes = sizeof(stereotypes) / sizeof(stereotypes[0]);
    So it will give you the result of the sizeof of an array divided by the sizeof each item in the value. So it's actually sizeof_int * num_stereotypes / sizeof_int = num_stereotypes. This is the best case scenario because arrays often have undefined behaviour like losing reference or such (been there T_T ). I prefer std::vector over an array any time. Anyway, you've got the number of items in your array. The next statement is the first faulty one. Your parameters are pointers and would prolly look like this: eg. stereotypes = 0xabcd, so stereotypes + num_stereotypes (in this case 8) = 0xabcd + 8 = 0xabd5, so your code will look like this:

    Code:
    		int* player_stereotype = max_element(0xabcd, 0xabd5);
    Is that what you're looking for?

    Code:
    cout << "You are really a " << stereotype_names[player_stereotype - stereotypes] << "!\n";
    This one is also wrong Why did you substract 2 pointers? It will prolly get your stereotype_names array out of bound because it will give you a particular address instead of the value of the variable. I don't know what the max_element function will return, but consider the same example, it will prolly look like this:

    Code:
    cout << "You are really a " << stereotype_names[0xcdcd - 0xabcd] << "!\n";
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by g4j31a5 View Post
    Code:
    		int* player_stereotype = max_element(stereotypes, stereotypes + num_stereotypes);

    This one seems wrong. On the second parameter, you're adding the value of a pointer with an integer. So consider this possible case:
    As far as I can see, it's right.
    It's an iterator to the beginning of an array and an iterator to one past the end of the array, which is what the function wants.

    Quote Originally Posted by g4j31a5 View Post
    On the line above it there's this code:

    Code:
    		size_t num_stereotypes = sizeof(stereotypes) / sizeof(stereotypes[0]);
    So it will give you the result of the sizeof of an array divided by the sizeof each item in the value. So it's actually sizeof_int * num_stereotypes / sizeof_int = num_stereotypes. This is the best case scenario because arrays often have undefined behaviour like losing reference or such (been there T_T ).
    What?
    This is perfectly defined behavior. It gives the number of elements in the array.

    Quote Originally Posted by g4j31a5 View Post
    I prefer std::vector over an array any time. Anyway, you've got the number of items in your array. The next statement is the first faulty one. Your parameters are pointers and would prolly look like this: eg. stereotypes = 0xabcd, so stereotypes + num_stereotypes (in this case 8) = 0xabcd + 8 = 0xabd5, so your code will look like this:

    Code:
    		int* player_stereotype = max_element(0xabcd, 0xabd5);
    Is that what you're looking for?
    Yes, this should be right.

    Quote Originally Posted by g4j31a5 View Post
    Code:
    cout << "You are really a " << stereotype_names[player_stereotype - stereotypes] << "!\n";
    This one is also wrong Why did you substract 2 pointers? It will prolly get your stereotype_names array out of bound because it will give you a particular address instead of the value of the variable. I don't know what the max_element function will return, but consider the same example, it will prolly look like this:

    Code:
    cout << "You are really a " << stereotype_names[0xcdcd - 0xabcd] << "!\n";
    No, this one also seems to be right.
    player_sterotype is a pointer to an element in the array. So subtract that with the beginning of the array and you get the offset, in T, to where the pointer points. That same index should be passed into the string array to get the string equivalent of the integer array.
    Last edited by Elysia; 06-29-2010 at 05:07 AM.
    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.

  7. #7
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by Elysia View Post
    As far as I can see, it's right.
    It's an iterator to the beginning of an array and an iterator to one past the end of the array, which is what the function wants.
    Well, after I do a search on max_element, I finally know that it was a part of the C++ standart. My bad, to tell you the truth I didn't know about it at all. I haven't used it at all in my projects.

    Quote Originally Posted by Elysia View Post
    What?
    This is perfectly defined behavior. It gives the number of elements in the array.
    Well, if you look closely, I actually agree with this line of code. However what I do point out is that I don't really encourage using an unbounded array because its not memory safe. That's why I usually use std::vector.

    Quote Originally Posted by Elysia View Post
    Yes, this should be right.
    After I saw the method's definition, I agree with you. It is correct.

    Quote Originally Posted by Elysia View Post
    No, this one also seems to be right.
    player_sterotype is a pointer to an element in the array. So subtract that with the beginning of the array and you get the offset, in T, to where the pointer points. That same index should be passed into the string array to get the string equivalent of the integer array.
    Again, I confess. This was my mistake.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    38
    alright so theres nothing wrong with the end part but what could be the reason that it crashes in the default of the switch cases before it displays the text? as i said before i tried putting in cin.get(); so it waits before returning 0 but it didn't change it.

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    I don't really know what you mean by 'when I try to debug.' Do you mean when you run it in debug mode? Can you explain what you are doing a little better? If it 'crashes' during runtime there *are* errors, whatever they may be. If you are using a debugger, then it should be giving you some kind of explanation.

  10. #10
    Registered User
    Join Date
    Oct 2009
    Posts
    38
    im using microsoft visual c++ 2008 and when i build it it says there are no errors and when i want to run it there are 2 options: start debugging and start without debugging. if i start without debugging it runs perfectly but if i start with debugging it doesnt.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Place a breakpoint on the first line, start with debugging, and step through the program line by line. Observe the line where it crashes, place the breakpoint there, remove the first one, start the program over. When it stops, examine the variables to find the one that doesn't make sense. It's basic debugging.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Use jock++ instead of jock = jock+1. I find your quiz quite amusing. "You thought you were a rocker. You are really a stoner!" Cool.

  13. #13
    Registered User
    Join Date
    Oct 2009
    Posts
    38
    thank you babkockdood. Is that the result you got from it? I think I figured it out though. if i put:
    Code:
     system ("PAUSE");
    before the return 0; in the default would it wait to display the code?

  14. #14
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    Yes, you can do that. I guess you don't really have an error. cin.get() should work also. It may be that you have a character (like the newline character) leftover in the input buffer so when you try to cin.get() wait for input it just reads that in. I don't know why it would differ between release and debug.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Tsk. Code to pause the console is evil IMO.
    Anyway, there are alternative ways of doing it: SourceForge.net: Pause console - cpwiki
    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. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 04:50 PM
  2. Hi, Quiz C program
    By Eman in forum C Programming
    Replies: 0
    Last Post: 11-11-2009, 04:12 PM
  3. Website tutorials (Pointers quiz)
    By TheUnknownFacto in forum C++ Programming
    Replies: 6
    Last Post: 04-18-2007, 10:58 AM
  4. Try this quiz (just made it with C)
    By voltson4 in forum C Programming
    Replies: 1
    Last Post: 07-11-2003, 06:28 PM
  5. C programming language quiz
    By Trancongan in forum C Programming
    Replies: 7
    Last Post: 04-21-2002, 01:04 PM