c + + Beginner step pit collection

Keywords: C++

2. Error prone points 1

When we use the scanf statement, many friends who have just started are prone to make the following mistakes when we first contact this statement. When using scanf ("% d", & n), we must add an & (address character).
Incorrect usage:

#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	scanf("%d",n);
	printf("%d",n); 
	return 0;
}

Correct usage:

#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	scanf("%d",&n);
	printf("%d",n); 
	return 0;
}

3. Error prone points 2

You can find the error in this Code:

Incorrect usage:

#include <bits/stdc++.h>
using namespace std;
void main()
{
	int a;
	cin>>a;
	cout<<a;
	return 0;
}

In fact, the error here is difficult to find. It is clearly wrong to specify that the return value of the main function must be int in c + +. The code is void.

Correct usage:

#include <bits/stdc++.h>
using namespace std;
int main()
{
	int a;
	cin>>a;
	cout<<a;
	return 0;
}

4. Error prone points 3

You can find the error in this Code:

Incorrect usage:

#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n,sum;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		sum=sum+i;
	}
	cout<<sum;
	return 0;
}

It should be well found that this code is seeking the sum of 1 to n. why is it wrong to do so, because the initial value of our local variable is not necessarily equal to 0, which is a random number.

Correct usage:

#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n,sum=0;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		sum=sum+i;
	}
	cout<<sum;
	return 0;
}

Be sure to set an initial value for our summation variable.

5. Error prone points 4

You can find the error in this Code:

Incorrect usage:

#include <bits/stdc++.h>
using namespace std;
int main()
{
	int a,b;
	cin>>a>>b;
	if(a=b)
	{
		cout<<"yes";
	}
	else
	{
		cout<<"no";
	}
	return 0;
}

This is also a very error prone place. It is easy to use errors when using assignment symbol = and relational operator = =. Some editors will not report errors, and it is difficult to find their own errors when checking the code.

Correct usage:

#include <bits/stdc++.h>
using namespace std;
int main()
{
	int a,b;
	cin>>a>>b;
	if(a==b)
	{
		cout<<"yes";
	}
	else
	{
		cout<<"no";
	}
	return 0;
}

Here, we must correctly understand assignment symbols and relational operators:
Assignment symbol:=
Relational operators:==

6. Error prone points 5

You can find the error in this Code:

Incorrect usage:

#include <bits/stdc++.h>
using namespace std;
int main()
{
	int a,b,c;
	cin>>a>>b>>c;
	if(a>b>c)
	{
		cout<<a;
	}
	return 0;
}

This code is to judge whether a is the maximum value and why it is wrong here. In c + +, if you directly write a > b > c, the final result may be wrong, because the relational operators = =,! =, >, <, > =<= The return values are 0 and 1. If it is correct, it is 1, and the error is 0. Write a < B < c directly. If a < B is correct, the last comparison is 1 < c, which is obviously wrong.

Correct usage:

#include <bits/stdc++.h>
using namespace std;
int main()
{
	int a,b,c;
	cin>>a>>b>>c;
	if(a>=b&&b>=c)
	{
		cout<<a;
	}
	return 0;
}

6. Error prone points 5

You can find the error in this Code:

Incorrect usage:

#include <bits/stdc++.h>
using namespace std;
int main()
{
	int a[10];
	for(int i=0;i<=10;i++)
	{
		cin>>a[i];
	}
	for(int i=0;i<=10;i++)
	{
		cout<<a[i]<<" ";
	}
	return 0;
}

The reason for the error here requires a certain understanding of multiple arrays. When defining an array, int a[10], in which the constant 10 is the length of the array. The value range of array subscript is 0-9, and the largest subscript is the array length minus 1.

Correct usage:

#include <bits/stdc++.h>
using namespace std;
int main()
{
	int a[10];
	for(int i=0;i<10;i++)
	{
		cin>>a[i];
	}
	for(int i=0;i<10;i++)
	{
		cout<<a[i]<<" ";
	}
	return 0;
}

Continuous update!

Posted by t.bo on Sat, 23 Oct 2021 20:00:55 -0700