it has been two days since i started learning c++ following from youtube, i have made a math exam asking questions and printing out the average, letter grade for average and if (pass/fail), also it ask if it was easy/hard . What Do You think ? How can i make it shorter should i use a for loop ?
#include <iostream>
#include <string>
using namespace std;
int main()
{
int ans1;
int qans1=20;
int ans2;
int qans2=10;
int ans3;
int qans3=180;
int ans4;
int qans4=3;
int mark1;
int mark2;
int mark3;
int mark4;
int avg;
string yes_no;
cout << " Welcome To Razer's Math Quiz , Good Luck " << endl;
cout << " " << endl;
cout<< " Question 1 : What is 10 + 10 ? " << endl;
cout << " Answer = "; cin>>ans1;
cout << " "<< endl;
Looks Interesting , ik how to make a random generator but i have not reached to your program skill yet but for like 6 hours learning what i did was good for me :)
The first thing I would do to make it shorter would be to get rid of most of those variables. Instead of storing the answers in variables, just use the expression, for example:
1 2 3
if (ans1==qans1) { cout << " Correct !! " << endl; }
// could simply be
if (ans1==10 + 10) { cout << " Correct !! " << endl; }
Also you do not need a separate variable for each input answer and mark.
1 2 3 4 5 6 7 8 9
int mark = 0;
//...
if (ans == 10 + 10)
{
cout << " Correct !! " << endl;
mark++
}
//...
avg = (mark * 10 / .4);
That is just a start and not necessarily the best way to go about it, but it should give you some ideas on how to shorten it up and make it easier to follow.