+ 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; }

30th Aug 2017, 2:30 PM
Tyrell Reid
Tyrell Reid - avatar
19 Respuestas
+ 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
30th Aug 2017, 2:38 PM
jay
jay - avatar
+ 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.
30th Aug 2017, 2:48 PM
Babak
Babak - avatar
+ 17
Of course my friend. That's for PUBLIC!:-)
30th Aug 2017, 2:51 PM
Babak
Babak - avatar
+ 11
@Babak.. Wow! Do you mind if I copy this answer for future referencing. With credits to you of course..
30th Aug 2017, 2:50 PM
jay
jay - avatar
+ 5
I would say please use #include <array> (from the STL) before worrying about the initialization method 😊
31st Aug 2017, 12:53 PM
jay
jay - avatar
+ 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};
31st Aug 2017, 12:30 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 2
Thanks so much, didn't realize my mistake.
30th Aug 2017, 2:48 PM
Tyrell Reid
Tyrell Reid - avatar
+ 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
31st Aug 2017, 12:43 PM
Pratham Kulkarni
Pratham Kulkarni - avatar
+ 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.
30th Aug 2017, 9:38 PM
Ben hart
+ 1
use cin>> x>>y; //compilation error remove and in if statement use == operator to compare (this is logical error)
3rd Sep 2017, 10:03 AM
Azam
Azam - avatar
0
1- Please use " >> " this opertaor for input. 2- Please use " == " for comparison .. " = " this is used for assigning value to varible.
1st Sep 2017, 4:28 AM
Naveed Ahmed
Naveed Ahmed - avatar
0
use cin>>
1st Sep 2017, 8:57 AM
Prateeksha Jain
0
and in if(x==password[0]&&y==password[1]
1st Sep 2017, 8:59 AM
Prateeksha Jain
0
ya
2nd Sep 2017, 6:58 PM
ajeet
ajeet - avatar
0
use>> with cin
3rd Sep 2017, 9:57 AM
Vivek Gupta
Vivek Gupta - avatar
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; }
3rd Sep 2017, 10:00 AM
Vivek Gupta
Vivek Gupta - avatar
- 1
and in array u write 3 .it's mean u have three values but u put only two values.
1st Sep 2017, 4:29 AM
Naveed Ahmed
Naveed Ahmed - avatar
- 1
in array declaration u write 3 ..u should use or "[2]" or empty [ ]
3rd Sep 2017, 10:02 AM
Naveed Ahmed
Naveed Ahmed - avatar
- 2
you have define password array of 3 and assign 2 values only
3rd Sep 2017, 6:44 PM
sanchit srivastava
sanchit srivastava - avatar