I'm a new member to the board and a fairly new member to programming. I picked up a book on beginning programming and dabbled for QBasic for a while, but I've become really interested in C++ and started to teach it to myself. I haven't gotten very far, only enough to make simple programs that ask for input and store variables to display later...things of that sort.
Anyways, I'm trying to make a little game that quizzes the user. It asks three multiple choice questions and waits for a response in between each (the subject matter of the questions is irrelevant at this point, I just want to get the thing running). I've gotten far enough where the program will ask the questions and take the input, but I'm running into variable problems.
At the end of the program, I put in code to tell the user how many questions they got right out of three and then calculate and display the percentage as well. But no matter how I try storing variables for input, the program always displays the same ridiculous number (~4,000,000) out of three and an even larger one for the percent. Here is the code I've written:
Could anyone offer any advice? I am looking for the right answer to my problem, but I would also appreciate some reasoning on where I went wrong or why it should be different. Thanks in advance to anyone who can help out!Code:#include <iostream> using namespace std; // So the program can see cout and endl int main() { int x; int y; int z; char letter; int A = 1; int B = 2; int C = 3; int D = 4; int total; int average; int percent; //Question 1 code cout<<"1. Who was the first president of the United States of America?\n A. Washington B. Lincoln\n C. Jefferson D. Roosevelt\n"; cin>>letter; //cin.ignore(); if(letter == 1){ x == 1; //endl; cin.ignore(); } else { x == 0; cin.ignore(); } //Question 2 code cout<<"2. In what year were the North American colonies liberated?\n A. 1776 B. 1781\n C. 1783 D. 1795\n"; cin>>letter; //cin.ignore(); if(letter == 3){ y == 1; //endl; might not be necessary cin.ignore(); } else { y == 0; cin.ignore(); } //Question 3 code cout<<"3. When were the slaves officially freed?\n A. 1861 B. 1863\n C. 1870 D. 1875\n"; cin>>letter; //cin.ignore(); if(letter == 2){ z == 1; cin.ignore(); } else { z == 0; cin.ignore(); } total == x + y + z; cout<<"\nGuess what?\n"; cin.ignore(); cout<<"You got " <<total <<" out of 3 questions correct!\n"; average == total / 3; percent == average * 100; cout<<"That's "<<percent <<"% !!!"; cin.get(); }