Coding Help

I am posting the following code snippet unable to understand the error created by the bold underlined line.Please suggest.

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
36
37
38
39
40
41
42
43
44
45
46
47
#include<stdio.h>
#include<string.h>
int output1;

void GetCount(int input1,char* input2)
{
	if(input1<=0)
	{
		output1=0;
		printf("There is no bowler info is given\n");
	}
	else
	{
		int count=0,bestBowler=0;
		int totalWkt=0,avgWkt=0;
		float numMatch=0.0;
		
		char delims[] = ",";
		char *result = NULL;
		char *p;
		for(count=0;count<input1;count++)
		{
			result=strtok(input2,delims);
			p=result;
			while(p!=NULL)
			{
				totalWkt+=(int)(*p-'0');				
				numMatch++;
				p++;
			}
			avgWkt=totalWkt/numMatch;
			if(avgWkt>=5)
				bestBowler+=1;
			input2=NULL;	
			p=NULL;
			numMatch=0;
		}
		output1=bestBowler;
	}
}


int main()
{
	GetCount(5,"2101,5,14340,0012130020,457");
	return 0;
}
Last edited on
first - USE code tag!!! it's unreadable!!!!
second - http://www.cplusplus.com/reference/clibrary/cstring/strtok/

Return Value
A pointer to the last token found in string.
A null pointer is returned if there are no tokens left to retrieve.
try writing your code in c++ and not c that will help to
why? C - bad? C++ doesn't have buld in tokenizer... ( without boost\loki e.t.c )
it does not matter c or c++ i think...
Could you post the error you're getting?
strtok() is an ugly little function that expects an 'char *'. The pointer you're providing is a 'const char *' (unfortunately it doesn't look like, but it is). So it will crash

To make it work you have to do something like this:
1
2
	char test[] = "2101,5,14340,0012130020,457";
	GetCount(5,test);


By the way: instead of using a global variable 'output1' why don't you just return the count?
closed account (DSLq5Di1)
@Gaminic
Pfft, where's the sport in that?!

@avishek
strtok() modifies the string that is passed in, and since you're passing in a string literal.. bigbadaboom.

1
2
3
4
5
6
7
int main()
{
        char input[] = "2101,5,14340,0012130020,457";

	GetCount(5, input);
	return 0;
}

1
2
3
4
5
		for(count=0;count<input1;count++)
		{
			result=strtok(input2,delims);
			p=result;
			while(p!=NULL && *p != NULL) // check the character is not null also. 


-edit-
@coder777
Bah, ninja post!
Last edited on
I never said that c is bad, what I mean is why use precompiled .h files when there is a c++ equivalent? why use printf instead cout? Im not saying c is bad, but if you are writing in c++ why use c code that has a c++ equivalilent?
closed account (1vRz3TCk)
why use printf instead cout? Im not saying c is bad, but if you are writing in c++ why use c code that has a c++ equivalilent?
printf and cout are not equivalent ... and they are both C++.
no printf is c but because c++ is built on c you can use anything in it for c++. That doesn't mean you should though. and what do you mean they aren't equivalent both are used for printing text
closed account (1vRz3TCk)
I didn't say that printf was not C. C++ is a language in its own right, it does not treat C as a separate entity therefor printf is also C++.

That doesn't mean you should though.
It doesn't mean that you shouldn't. It would depend more on what paradigm you want to follow.

what do you mean they aren't equivalent both are used for printing text
I mean that they are not equivalent, take a look at the documentation for them and see if you can all the same operations in both.

Then maybe consider not dismissing the 'C parts' of C++ out of hand just because it is not class oriented.
while c++ is largely oop, thats not what I am talking about. Yes, I have already said that because c++ is built on c, you can just put an int before main and its valid c++. However, I am talking about c++ syntax. In c, printf is used to print text. In c++ that is couts job. You can mix them all you want and probably without compiler error, that doesn't make it proper. Why use a c command (for lack of a better term) when you can use the proper c++ one and actually take up less space in the source. Its like mixing lotr Elvish and English. Lotr Elvish is based off English, however it would be hard to understand because they aren't the same thing
closed account (1vRz3TCk)
I will say it one more time; printf is a function in the C++ standard library. You include the <cstdio> header from the C++ standard library. This is not the header for the C standard library (stdio.h, the use of which is deprecated in C++). For a compiler to be compliant with the C++ standard it has to implement printf (along with 200 odd other functions), so printf is 'proper C++' (just not class oriented C++)
And I will say it one more time. yes what you said is true however (s)he is using stdio.h, which is a C header file. so, he is writing valid c++ code, however I am targeting syntax. printf would be proper syntax with cstdio, but with a c header file it is written in c flavor. Actually let me rephrase. You are completely right. However if you notice (s)he is using .h files. so can you agree that in this case he isn't writing in proper c++ syntax because he isn't using proper files for it
closed account (1vRz3TCk)
I'm not commenting on what the OP wrote, just your assertion that printf is not C++.

So the OP posted some code whiteout saying if it is C or C++. If you are unsure if the OP is writing C or C++, then ask them. If it turns out to be C++, them inform them of the correct header to include (if you must). Even with the .h files, it is not wrong or improper C++, deprecated just means that you should not relied upon as it may not be there in future versions of the standard.
well, i was commenting on what he wrote and i was actually going to do this, but then we started our arguement
Topic archived. No new replies allowed.