### Spot the Difference

I wrote a piece of code for a spoj problem, and after about an hour of struggling, I looked up the answer, and I could not see the semantic difference, please help:
My code:
 ``1234567891011121314151617181920212223`` ``````#include #include using namespace std; int main() { int t; long long int b; int A[][4] ={{0, 0, 0, 0}, {1, 1, 1, 1},{2, 4, 8, 6}, {3, 9, 7, 1},{4, 6, 4, 6}, {5, 5, 5, 5},{6, 6, 6, 6}, {7, 9, 3, 1},{8, 4, 2, 6}, {9, 1, 9, 1}}; string a; cin>>t; for(int i=0; i>a>>b; if(b==0) { cout<<'1\n'; continue; } b%=4; b= (b==0) ? 3 : b-1; int e=a[a.length()-1]-48; cout<

The solution:
 ``123456789101112131415161718192021222324`` ``````#include #include using namespace std; int main(int argc, const char *argv[]) { int zad[][4] ={{0, 0, 0, 0}, {1, 1, 1, 1},{2, 4, 8, 6}, {3, 9, 7, 1},{4, 6, 4, 6}, {5, 5, 5, 5}, {6, 6, 6, 6}, {7, 9, 3, 1}, {8, 4, 2, 6}, {9, 1, 9, 1}}; int t; cin >> t; for(int i=0; i> a >> b; if(b==0) { cout << 1 << endl; } else { int aa = a[a.size()-1] - '0'; b %= 4; b = (b == 0) ? 3 : b-1; cout << zad[aa][b] << endl; } } return 0; }``````
`cout<<'1\n';`
Hmm... does that even compile?
why would it not, is prints the number one, with a newline char. Same as
`cout<<"some text\n";`
Last edited on
Can you give sample input and output?
Edit: Only other difference I see is `int e=a[a.length()-1]-48;` and `int aa = a[a.size()-1] - '0';`
Last edited on
Sample I/O:

Input:
3
3 10
6 2
150 53

Output:
9
6
0
Last edited on
 ```Script_Coder.cpp:14:19: warning: multi-character character constant [-Wmultichar] Script_Coder.cpp: In function ‘int main()’: Script_Coder.cpp:7:5: warning: ISO C++ 1998 does not support ‘long long’ [-Wlong-long] ```

It should be `cout << "1\n";` instead because '\n' is a character on its own, and single quotes are used for single characters.

Tip: if you use GCC, you might want to compile with -Wall -Wextra -pedantic for interesting warnings.
@Catfish thank you, so much. Such a stupid mistake. I compile with g++ 4.6.2 , why does it not give me those error messages.
 Edit: Only other difference I see is int e=a[a.length()-1]-48; and int aa = a[a.size()-1] - '0';

The ascii value of '0' is 48, so no difference there.
I was referring to the use of size() instead of length()
@ Script Coder: so it works now as expected?

As for the warnings not showing up, I use GCC 4.7.2 but still, do you use the warning flags I mentioned?
 `g++ -Wall -Wextra -pedantic -std=c++98 Script_Coder.cpp -o Script_Coder.exe`

It works perfectly, and no I use no warning flags at all.
 `g++ Script_Coder.cpp -o Script_Coder.exe`
Topic archived. No new replies allowed.