Please support our C++ advertiser: Programming Forums
Views: 3548 | Replies: 15
![]() |
•
•
Join Date: Jul 2005
Posts: 47
Reputation:
Rep Power: 4
Solved Threads: 0
OK, I gave it one more try, And this is my last solution, I have no idea how else to do this
I tried to compile this and it worked out pretty well except for the totaling up the correct answers part. Can you guys please check it?
OMG. I think I'm fainting!
//Driver's License Exam;
//This program first enter the answer key with 20 answers
//then it will ask the student to enter his/her answer
//The program then totals up the number of questions answered correctly and incorrectly
//The student must answer at least 15 questions right in order to pass the exam.
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
char StudentAns(int );
int main()
{
int i, correct = 0, incorrect =0, total1, total2;
char *key = "BDAACABACDBCDADCBBDA"; //THE ANSWER KEY
char answer[20]; //DECLARE THE STUDENT ANSWER ARRAY
//STUDENT ENTER ANSWERS.
for(i = 0; i < 20; i++)
{
answer[i] = StudentAns(i);
}
//SHOW RESULT;
cout << "THIS IS YOUR RESULT.\n";
for(i = 0; i < 20; i++)
{
if(answer[i] == key[i])
{
cout << "this answer is correct.\n";
correct++; //INTITIALIZE COUNTER FOR CORRECT ANSWER
}
else
{
cout << answer [i] << " is an incorrect choice.\n";
incorrect++; //INNITIALIZE COUNTER FOR INCORRECT ANSWER
}
}
total1 += correct++; //TOTAL UP THE CORRECT ANSWERS
total2 += incorrect++; //TOTAL UP THE INCORRECT ANSWERS
cout << "you have answered " << total1 << " questions correctly.\n";
cout << "You have answered " << total2 << " questions incorrectly.\n";
if (total1 >= 15)
{
cout << "\nCONGRATUATIONS! YOU HAVE PASSED THE DRIVER'S LICENSE TEST.\n";
}
else
{
cout << "\nSorry. You have failed the Driver's License Exam.\n";
cout << "\nYou must answer at least 15 questions right in order to pass the exam.\n";
}
return 0;
}
char StudentAns(int i)
{
char choice;
cout << "Please enter your choice for Question " << i+1 << endl;
cin >> choice;
choice = toupper(choice);
while (choice != 'A' && choice != 'B'&& choice != 'C' && choice != 'D')
{
cout << "You must enter A, B, C, or D.\n";
cin >> choice;
}
return choice;
}I tried to compile this and it worked out pretty well except for the totaling up the correct answers part. Can you guys please check it?
OMG. I think I'm fainting!
•
•
Join Date: Jul 2005
Posts: 244
Reputation:
Rep Power: 4
Solved Threads: 4
•
•
•
•
Originally Posted by karen_CSE
//Driver's License Exam; //This program first enter the answer key with 20 answers //then it will ask the student to enter his/her answer //The program then totals up the number of questions answered correctly and incorrectly //The student must answer at least 15 questions right in order to pass the exam. #include <iostream> #include <stdio.h> //#include <string.h> using namespace std; char StudentAns(int ); int main() { int i, correct = 0, incorrect =0; //, total1, total2; char *key = "BDAACABACDBCDADCBBDA"; //THE ANSWER KEY char answer[20]; //DECLARE THE STUDENT ANSWER ARRAY //STUDENT ENTER ANSWERS. for(i = 0; i < 20; i++) { answer[i] = StudentAns(i); } //SHOW RESULT; cout << "THIS IS YOUR RESULT.\n"; for(i = 0; i < 20; i++) { cout<<"Question "<<(i+1)<<":"<<endl; if(answer[i] == key[i]) { cout << "was answered is correct.\n"; correct++; //INTITIALIZE COUNTER FOR CORRECT ANSWER } else { cout << answer [i] << " is an incorrect choice.\n"; incorrect++; //INNITIALIZE COUNTER FOR INCORRECT ANSWER } } // total1 += correct++; //TOTAL UP THE CORRECT ANSWERS // total2 += incorrect++; //TOTAL UP THE INCORRECT ANSWERS cout << "you have answered " << correct << " questions correctly.\n"; cout << "You have answered " << incorrect << " questions incorrectly.\n"; if (correct >= 15) { cout << "\nCONGRATUATIONS! YOU HAVE PASSED THE DRIVER'S LICENSE TEST.\n"; } else { cout << "\nSorry. You have failed the Driver's License Exam.\n"; cout << "\nYou must answer at least 15 questions right in order to pass the exam.\n"; } return 0; } char StudentAns(int i) { char choice; cout << "Please enter your choice for Question " << i+1 << endl; cin >> choice; choice = toupper(choice); while (choice != 'A' && choice != 'B'&& choice != 'C' && choice != 'D') { cout << "You must enter A, B, C, or D.\n"; cin >> choice; choice = toupper(choice); } return choice; }
I tried to compile this and it worked out pretty well except for the totaling up the correct answers part. Can you guys please check it?
OMG. I think I'm fainting!
Try that edit. Looks pretty good to me, but I didn't run it myself.
One problem was that on lower-case answers that weren't valid (for example: e), you didn't convert the case to make a lower-case a upper case, which made your code inconsistent.
But mainly: You had too many variables. If you're incrementing correct and incorrect, why do you need total1 and total2?
•
•
Join Date: Jul 2005
Posts: 47
Reputation:
Rep Power: 4
Solved Threads: 0
HEY GUYS I FIGURED IT ALL OUT!!!!!!! YAY FOR ME!!! :mrgreen:
I'm posting this online anyway, for any beginner, like myself, that have programs that are similar to mine.
you guys can use this as a guide. I find an example a lot easier to understand than explanation :lol:
[code]
//Driver's License Exam;
//This program first enter the answer key with 20 answers
//then it will ask the student to enter his/her answer
//The program then totals up the number of questions answered correctly and incorrectly
//The student must answer at least 15 questions right in order to pass the exam.
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
char StudentAns(int );
int main()
{
int i, correct = 0, incorrect =0;
char *key = "BDAACABACDBCDADCBBDA"; //THE ANSWER KEY
char answer[20]; //DECLARE THE STUDENT ANSWER ARRAY
//STUDENT ENTER ANSWERS.
for(i = 0; i < 20; i++)
{
answer[i] = StudentAns(i);
}
//SHOW RESULT;
cout << "THIS IS YOUR RESULT.\n";
for(i = 0; i < 20; i++)
{
if(answer[i] == key[i])
{
cout << "this answer is correct.\n";
correct++; //update COUNTER FOR CORRECT ANSWER
}
else
{
cout << answer [i] << " is an incorrect choice.\n";
incorrect++; //update COUNTER FOR INCORRECT ANSWER
}
}
cout << "You have answered " << correct << " questions correctly.\n";
cout << "You have answered " << incorrect << " questions incorrectly.\n";
if (correct >= 15)
{
cout << "\nCONGRATUATIONS! YOU HAVE PASSED THE DRIVER'S LICENSE TEST.\n";
}
else
{
cout << "\nSorry. You have failed the Driver's License Exam.\n";
cout << "\nYou must answer at least 15 questions right in order to pass the exam.\n\n";
}
return 0;
}
char StudentAns(int i)
{
char choice;
cout << "Please enter your choice for Question " << i+1 << endl;
cin >> choice;
choice = toupper(choice);
while (choice != 'A' && choice != 'B'&& choice != 'C' && choice != 'D')
{
cout << "You must enter A, B, C, or D.\n";
cin >> choice;
}
return choice;
}
Thank you all who have been really patient with me throughout this HW question. Your advices were an immense help. I'm so grateful I discovered this forums. you guys are THE BEST!!!!! :mrgreen:
THANKS AGAIN!
I'm posting this online anyway, for any beginner, like myself, that have programs that are similar to mine.
you guys can use this as a guide. I find an example a lot easier to understand than explanation :lol: [code]
//Driver's License Exam;
//This program first enter the answer key with 20 answers
//then it will ask the student to enter his/her answer
//The program then totals up the number of questions answered correctly and incorrectly
//The student must answer at least 15 questions right in order to pass the exam.
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
char StudentAns(int );
int main()
{
int i, correct = 0, incorrect =0;
char *key = "BDAACABACDBCDADCBBDA"; //THE ANSWER KEY
char answer[20]; //DECLARE THE STUDENT ANSWER ARRAY
//STUDENT ENTER ANSWERS.
for(i = 0; i < 20; i++)
{
answer[i] = StudentAns(i);
}
//SHOW RESULT;
cout << "THIS IS YOUR RESULT.\n";
for(i = 0; i < 20; i++)
{
if(answer[i] == key[i])
{
cout << "this answer is correct.\n";
correct++; //update COUNTER FOR CORRECT ANSWER
}
else
{
cout << answer [i] << " is an incorrect choice.\n";
incorrect++; //update COUNTER FOR INCORRECT ANSWER
}
}
cout << "You have answered " << correct << " questions correctly.\n";
cout << "You have answered " << incorrect << " questions incorrectly.\n";
if (correct >= 15)
{
cout << "\nCONGRATUATIONS! YOU HAVE PASSED THE DRIVER'S LICENSE TEST.\n";
}
else
{
cout << "\nSorry. You have failed the Driver's License Exam.\n";
cout << "\nYou must answer at least 15 questions right in order to pass the exam.\n\n";
}
return 0;
}
char StudentAns(int i)
{
char choice;
cout << "Please enter your choice for Question " << i+1 << endl;
cin >> choice;
choice = toupper(choice);
while (choice != 'A' && choice != 'B'&& choice != 'C' && choice != 'D')
{
cout << "You must enter A, B, C, or D.\n";
cin >> choice;
}
return choice;
}
Thank you all who have been really patient with me throughout this HW question. Your advices were an immense help. I'm so grateful I discovered this forums. you guys are THE BEST!!!!! :mrgreen:
THANKS AGAIN!
•
•
Join Date: Jul 2005
Posts: 47
Reputation:
Rep Power: 4
Solved Threads: 0
HEY, DROWZEE
that was the exact thing I did! wow, without help, too! I figured it out right before i check this website for, like, the hundredth time! Man, don't I sound arrogant! i'm just so darn happy right now. I've been on that project for like 4 days.
anyway, thanks for all your help Drowzee, you were great!
Karen
that was the exact thing I did! wow, without help, too! I figured it out right before i check this website for, like, the hundredth time! Man, don't I sound arrogant! i'm just so darn happy right now. I've been on that project for like 4 days.
anyway, thanks for all your help Drowzee, you were great!
Karen
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)





Linear Mode