+ 8
I keep getting compilation errors
#include <iostream> using namespace std; int main() { int Password[3] = {2,4}; int x; int y; cin << x; cin << y; if (x = Password[0] && y = Password[1]){ cout << "correct password" << endl; } return 0; }
19 Antworten
+ 29
*use >> with cin
*use == to compare values
*only need two elements in array Password (in the above code sample anyway)
https://code.sololearn.com/cg70qqrd6UyQ/?ref=app
+ 26
In addition to dear Jay's nice answer I'd like to add some other common mistakes.
Recently, I have reviewed a source code in C++. the following, listed some of the errors.
1. forgetting semicolon at the end of the statements.
2. put insertion operator << instead of extraction operator >> when you want to get input via cin object (cin >> ...; )
3. put extraction operator >> instead of insertion operator << when you want to print something on screen via cout object (cout << ...; )
4. feeding a square root function with a negative number which cause and undefined error in your output. (logical error)
( e.g. n = 3; m = 4; x = sqrt(n-m); )
5. exceeding the length of the array in a loop.
(e.g. int arr[] = {12,13,14,15}; for (int i = 0; i <=4; i++) { ....} )
6. forgetting to put a return value into a function body when the function has a return value.
For example:
int sum(int a, int b)
{
int c = a + b;
}
7. confusion with logical operators.
a = 1;
b = 0;
c = 1;
cout << (a || (b && (c || b)));
// Explanation. Starting from the inner most parenthesis:
1st: ( c || b ), that is ( 1 || 0 ) which is 1
2nd: (b && 1), that is (0 && 1) which is 0
3nd: (a || 0), that is (1 || 0) which is 1
So the output is 1.
Hope this list can be a help.
+ 17
Of course my friend. That's for PUBLIC!:-)
+ 11
@Babak.. Wow! Do you mind if I copy this answer for future referencing. With credits to you of course..
+ 5
I would say please use #include <array> (from the STL) before worrying about the initialization method 😊
+ 3
@Lloyd you are wrong, he tells its compiler that the first two elements of the array will be none 0, and the others will be 0.
Doing what showed next fill the array with 0s :
int array[18] = {0};
+ 2
Thanks so much, didn't realize my mistake.
+ 2
pls don't write 3 in the array brackets leave it blank[] use == instead of = in the if loop
https://www.sololearn.com/discuss/670840/?ref=app
+ 1
Those kind of errors occur sometimes and it's very useful to know how to detect and solve them as quick as quick as possible. If you find it too troubling just be sure it is important to do it and you can always try using some programs like checkmarx or others to help you.
I wish you well.
Ben.
+ 1
use cin>> x>>y; //compilation error remove
and in if statement use == operator to compare (this is logical error)
0
1- Please use " >> " this opertaor for input.
2- Please use " == " for comparison ..
" = " this is used for assigning value to varible.
0
use cin>>
0
and in if(x==password[0]&&y==password[1]
0
ya
0
use>> with cin
0
I keep getting compilation errors
#include <iostream.h>
using namespace std;
int main()
{
int Password[3] = {2,4};
int x;
int y;
cin>>x;
cin>>y;
if (x==Password[0] && y==Password[1])
{
cout << "correct password" << endl;
}
return 0;
}
- 1
and in array u write 3 .it's mean u have three values but u put only two values.
- 1
in array declaration u write 3 ..u should use or "[2]" or empty [ ]
- 2
you have define password array of 3 and assign 2 values only