Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include<stdio.h>
#include<string.h>
main()
{
	char* newme(char* input1);
	char newi[100],data[100]="10,10,50,120,20,30,10,140,205";
	strcpy(newi,newme(data));
	printf("%s");
	return 0;
}
char* newme(char* input1)
{
	int Data[20],i=0;
	float A,B,C,D,E,F,G,H,I;
	char seps[]=",";
	char* token;
	char input2[25];
	strcpy(input2,input1);
	printf("%s",input2);
	token=strtok(input2,seps);
	while(token!=NULL)
	{
		Data[i]=atof(token);
		token=strtok(NULL,seps);
		i++;
		printf("%f",Data[i]);
	}
	printf("\n%d",i);
     A=Data[0];B=Data[1];C=Data[2];D=Data[3];E=Data[4];F=Data[5];G=Data[6];H=Data[7];I=Data[8];
	printf("\n data =%f %f %f %f %f %f %f %f %f",A,B,C,D,E,F,G,H,I);
	return input2;
}

The output must be same as the the input but instead it gives -1.#INDO for most of the values

What I have tried:

Increasing the buffer size, changing atof to atoi
Posted
Updated 9-Sep-16 0:47am
v3

I post here the output of a modern compiler with the 'all warnings activated' option (namely gcc -Wall) invoked on your code:
foo.c:3:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
 main()
 ^
foo.c: In function ‘main’:
foo.c:8:9: warning: format ‘%s’ expects a matching ‘char *’ argument [-Wformat=]
  printf("%s");
         ^
foo.c: In function ‘newme’:
foo.c:23:11: warning: implicit declaration of function ‘atof’ [-Wimplicit-function-declaration]
   Data[i]=atof(token);
           ^
foo.c:26:10: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat=]
   printf("%f",Data[i]);
          ^
foo.c:30:9: warning: function returns address of local variable [-Wreturn-local-addr]
  return input2;
         ^
foo.c:29:2: warning: ‘I’ is used uninitialized in this function [-Wuninitialized]
  printf("\n data =%f %f %f %f %f %f %f %f %f",A,B,C,D,E,F,G,H,I);
  ^
foo.c:29:2: warning: ‘H’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘G’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘F’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘E’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘D’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘C’ is used uninitialized in this function [-Wuninitialized]



Now let's examine the above mess...
foo.c:3:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
 main()
 ^
You should always specify the type of the return value of a C function (in this particular scenario you are lucky because the default, int, is correct).


foo.c: In function ‘main’:
foo.c:8:9: warning: format ‘%s’ expects a matching ‘char *’ argument [-Wformat=]
  printf("%s");
         ^
You didn't provide the required argument (the string to print).

foo.c: In function ‘newme’:
foo.c:23:11: warning: implicit declaration of function ‘atof’ [-Wimplicit-function-declaration]
   Data[i]=atof(token);
           ^
You should include stdlib.h in order to use atof.

C#
foo.c:26:10: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat=]
   printf("%f",Data[i]);
          ^
There is a mismatch between the format specifier and the type of the variable to printout (you usually have to either change the format specifier of the variable type).


foo.c:30:9: warning: function returns address of local variable [-Wreturn-local-addr]
  return input2;
         ^
This should make ring a bell very loud in your head, because it is usually the first sign of the incoming catastrophe: after function execution, a local variable does not exist any more.


C#
foo.c:29:2: warning: ‘I’ is used uninitialized in this function [-Wuninitialized]
  printf("\n data =%f %f %f %f %f %f %f %f %f",A,B,C,D,E,F,G,H,I);
  ^
foo.c:29:2: warning: ‘H’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘G’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘F’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘E’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘D’ is used uninitialized in this function [-Wuninitialized]
foo.c:29:2: warning: ‘C’ is used uninitialized in this function [-Wuninitialized]
Printing out uninitialized variables usually produces fancy results.


The bottom line is: the compiler is your friend, use it wisely.
 
Share this answer
 
Comments
Erebus_22 9-Sep-16 6:56am    
The rest of the code is fine, the returned value will be copied into another variable because its temporary. The main function default value is int . The %s is extraneous statement in main . Actually the concern is in the user defined funciton
<pre>
token=strtok(input2,seps);
while(token!=NULL)
{
Data[i]=atof(token);
token=strtok(NULL,seps);
i++;
printf("%f",Data[i]);
}
printf("\n%d",i);
A=Data[0];B=Data[1];C=Data[2];D=Data[3];E=Data[4];F=Data[5];G=Data[6];H=Data[7];I=Data[8];
printf("\n data =%f %f %f %f %f %f %f %f %f",A,B,C,D,E,F,G,H,I);

</pre>
The values returned should be the values passed to the function but are not, That's the issue.
Thanks for your input but the main issue was the exclusion of stdlib.
Try to replace
C++
i++;
printf("%f",Data[i]);

by
C++
printf("%f",Data[i]);
i++;

it may help

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
Erebus_22 9-Sep-16 6:58am    
Thanks but the main issue was stdlib

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900