Thread: Trying to do a simple thing...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    33

    Trying to do a simple thing...

    I'm asking the user if they are ready to begin.
    Code:
    #include <iostream.h>
    
    int main()
    {
    	int a;				// start;
    	int answer_one;		// answer to number one
    	int answer;
    
    	cout << "Welcome to  History Quiz!. This qui will test your knowledge on various history topics." << endl;
    	cout << "Ready to start? Y for yes, N for no." << endl;
    	cin >> a;
    	
    	if ( a == 'Y')
    	{
    	cout << "What was the name of the treaty that ended World War I?" << endl;
    	cout << "A - Treaty of Paris" << endl;
    	cout << "B - Treaty of Versailles" << endl;
    	cout << "C - Adams-On&#237;s Treaty" << endl;
    	cout << "D - Treaty of Mutual Cooperation and Security between the United States and Japan" << endl;
    	cin >> answer_one;
    	
    	switch (answer_one)
    	{
    	case 1:
    	answer = 'A';
    	break;
    	case 2:
    	answer = 'B';
    	break;
    	case 3:
    	answer = 'C';
    	break;
    	case '4':
    	answer = 'D';
    	break;
    	}
    	}
    	else
    	{
    		cout << "See you later!" << endl;
    		return 0;
    	}
    Even when I press N, it runs it.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    int a;				// start;
    int answer_one;		// answer to number one
    int answer;
    
    cout << "Welcome to  History Quiz!. This qui will test your knowledge on various history topics." << endl;
    cout << "Ready to start? Y for yes, N for no." << endl;
    cin >> a;
    	
    if ( a == 'Y')
    a is an int and unless you happened to type in 89 (ASCII for 'Y') then the if test is not going to work. Seems like you want a to be type char instead. Same thing with the answer_one variable.

    Code:
    #include <iostream.h>
    That is an old/non-standard header, you should be using <iostream> (without the .h part) instead. If you do that you'll also need a using namespace std; line after it.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    33
    Gah! Knew it would be something stupid. Thanks.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    33
    How would I write a code that shows the user how many correct answers they got?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Sembhi View Post
    How would I write a code that shows the user how many correct answers they got?
    You try to solve that problem, post your code, and we will help you sort out the trouble you've got.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    >how do I give the correct number of answers?

    -start with something to keep a count (an integer)

    -every time the user answers correctly, increment the counter.

    -at the end, display the final count.

    -now you do the coding.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    33
    For some reason it won't let me find out a final score out of 100.

    I'm dividing the total number they got correct out of the total number of questions then multiplying by 100 and it's not coming out right.


    Code:
    	g = ts/5 * 100;
    	cout << "You scored a " << g  << "&#37;."<< endl;
    I've tried using modulus and it won't come out right.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    At least use floating point division. A percentage is a part of a whole, so most divisions using integer math will result with zero and many disgruntled students.
    Code:
    std::cout << "You scored a " << ts / 5.0 * 100 << "&#37;.\n";

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM
  2. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 01:30 PM
  3. Help me with these simple programs
    By Help me in forum C Programming
    Replies: 4
    Last Post: 11-08-2001, 10:38 AM
  4. Need help with simple data types
    By partnole in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2001, 08:36 AM
  5. Linker errors with simple static library
    By Dang in forum Windows Programming
    Replies: 5
    Last Post: 09-08-2001, 09:38 AM