Afridi_Haque's blog

By Afridi_Haque, history, 2 years ago, In English

Hi Programmers, I am new to CP, I made my CP template but it's showing error while using some of it's features. Below I am pasting the template snippet, and later will post the error.

//	Headers
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pdbs;
using namespace std;

//	Special Numbers
#define mod 1000000007
#define inf 1e18

//	Datatypes
#define ll long long
#define double long long double

//	Vector operations
#define pb push_back
#define v(int) vector<long long>
#define vb v.begin()
#define ve v.end()

//	Pair operations
#define pll pair<long, long>
#define f first
#define s second
#define mp make_pair

//	Loops
#define w(t) int t; cin >> t; while(t--)
#define forn(a,b) for(int i = a; i < b; i++)
#define forr(a,b) for(int i = a; i >= b; i--)

//	Printing Outputs
#define py cout<<"YES"<<endl;
#define pn cout<<"NO"<<endl;

//	Driver Function Start
//	Type Code from here
int main(){
	
	return 0;
}

My code is —

int main(){
	int xsum = 0, ysum = 0, zsum = 0;
	w(t){
		int x, y, z;
		cin >> x >> y >> z;
		xsum += x;
		ysum += y;
		zsum += z;
	}
	
	if(xsum == 0 && ysum == 0 && zsum == 0)	py;
	else	pn;
	return 0;
}

But it's showing error for — py and pn definitions(last 2 lines of template). The error follows as -

49 2 C:\Users\AFRIDI\OneDrive\Desktop\CP\A2OJ\Young Physicist.cpp [Error] 'else' without a previous 'if'

Please help me to find a way out. Your help will be appreciated. In comments I am posting the error screen shot.

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
2 years ago, # |
  Vote: I like it 0 Vote: I do not like it

you are using a semicolon in py and then using another semicolon in the main so the code will look like this:

if(xsum == 0 && ysum == 0 && zsum == 0)	cout<<"YES"<<endl;;

with two semicolons, then, the else is not associated with the if sentence.

»
2 years ago, # |
  Vote: I like it +10 Vote: I do not like it

Because of problems like this one, it may be better to avoid using #define statements.

Another example of problem people face when using #define is setting $$$inf=10^9+7$$$ (#define inf 1e9+7). If you later use $$$-inf$$$ it isn't $$$-(10^9+7)$$$ but $$$-10^9+7$$$. Solution:

const int INF=1e9+7;

Recommendation: use functions and constants instead of #define statements.