An "if " issue...

Hi

l increases at any input, even at the case number[i]=*(fdigit).
What is the problem with the if statement?
How can i fix it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  #include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main ()
{
	
		int n, l=0;
		int number[n];
		cin>>n;
		
		for (int i=0;i<n;i++)
		{
			cin>>number[i] ;
		}
		
 for (int i=0;i<n-1;i++)
	{
		number[i]=number[i]%10;
		
		stringstream first;
		first<<number[i+1];
		string s=first.str();
		char const* fdigit=s.c_str();
		cout<<"this is numaru"<<number[i]<<"\n";
		cout<<"this is fdigit"<<*fdigit<<"\n";
		if (number[i]!=*(fdigit))
	{
	l=l+1;}
		cout<<"this is l"<<l<<"\n";
		}
	cout<<l;
	
return 0;
}


Thanks in advance.
This happens because number[i] is an int, and fdigit is char.
When number[i] == 2 and *fdigit == '2' there is no equality
1
2
2 == 2   // true but
2 == '2' // false  
Last edited on
Line 9: You're trying to allocate an array of n ints, but n is uninitialized.
This type of allocation is not supported in the C++ standard.
Topic archived. No new replies allowed.