My First Program

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;

if (ans1==qans1) { cout << " Correct !! " << endl; }

else { cout<<" Wrong !! " << endl; }

cout << " " << endl;

cout << " Question 2 : What is 10 Percent of 100 ? " << endl;
cin>>ans2;
cout<<" " << endl;

if ( ans2==qans2) { cout<<" Correct !! "<< endl;}
else { cout<< " Wrong !!" << endl;}

cout<< " Question 3 : What is The Total interior angle of a triangle ?" << endl;
cin>> ans3;
cout << " " << endl;

if (ans3==qans3) { cout << " Correct !! " << endl; }

else { cout << " Wrong !! "<< endl;}

cout << " Last Question " << endl;
cout <<" " << endl;
cout << " What is x in x + 2 =5" << endl;
cin>>ans4;
cout << " " << endl;

if (ans4==qans4) { cout << " Correct !!" << endl;}
else { cout << " Wrong !! " << endl;}
cout << " " << endl;


cout <<" Calculating Average " << endl;
cout << " " << endl;

if( ans1== qans1) mark1=10;

else mark1=0;


if( ans2== qans2) mark2=10;

else mark2=0;

if( ans3== qans3) mark3=10;

else mark3=0;

if( ans4== qans4) mark4=10;

else mark4=0;

avg=((mark1+mark2+mark3+mark4)/.4);


cout <<avg<< " " << " Percent " << endl;
cout << " " << endl;

if (avg>=60)
{
cout<< " You Pass " << endl;

}

else {
cout<<" You Fail"<< endl;


}



if ( ( avg>= 60) & ( avg<69.99) ) { cout<<" c" << endl;
}

else if ( (avg>=70) & (avg<79.99) )
{ cout<< " B " << endl;

}
else if ( ( avg>=80) & (avg<89.99) )
{ cout << " A " <<endl;

}

else if ( ( avg>=90) & ( avg<=100) )
{cout<<" A ++ "<< endl;
}

else {
cout<<" F" << endl;
}
cout << " " << endl;
cout << " " << endl;


cout << " Was The Exam Easy( yes or no) ? " << endl;
cin>>yes_no;
cout << " " << endl;

if (yes_no=="yes") { cout << " You Studied Well" << endl;}

else { cout << " You Need To Study More " << endl;}

cout << " " << endl;

system("pause>nul") ;

return 0 ;
}
Here is something I wrote along time ago that uses a for loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <ctime>

const int NUM_PROBLEMS = 10;

int randNumber();
int addNumbers(int, int);
int subtractNumbers(int, int);
void math();

int main() {

	srand(unsigned(time(0)));

	std::cout << NUM_PROBLEMS << " Math Problems\n\n";
	math();

	return 0;
}


void math(){

	int x, y, ans, input, cnt = 0;

	for (int i = 0; i < NUM_PROBLEMS; i++){

		x = randNumber();
		y = randNumber();

		if (rand() % 2){
			ans = addNumbers(x, y);
			std::cout << x << " + " << y << " = ";
		}
		else{
			ans = subtractNumbers(x, y);
			std::cout << x << " - " << y << " = ";
		}

		std::cin >> input;
		std::cout << std::endl;

		if (input == ans){
			std::cout << "You are correct" << std::endl << std::endl;
			cnt++;
		}
		else
			std::cout << "You are incorrect the correct answer is " << ans << std::endl << std::endl;
	}
	std::cout << "You got " << cnt << " problems correct out of " << NUM_PROBLEMS << std::endl;

}

int randNumber(){return (rand() % 10);}

int addNumbers(int x, int y){return (x + y);}

int subtractNumbers(int x, int y){return (x - y);}
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.
thank you very much :) this helps
Topic archived. No new replies allowed.