The Way to Programming
The Way to Programming
This is the code:
Date::Date(char * date) { char * string; int date2[3]; int counter = 0; string = strtok(date,"/"); while(string != NULL) { date2[counter] = atoi(string); string = strtok(NULL,"/"); counter++; } }
I give a string something like “12/12/2011″ as parameter and the constructor splits it but the rule string = strtok(date,”/”); gives a infinite loop.
I’ve seen on
http://www.cplusplus.com/reference/clibrary/cstring/strtok/
for more information but I don’t get it why it doesn’t work.
I tried your code in an easy program:
#include#include #include #include using namespace std; int main (){ char date[11]; cin >> date; char * string; int date2[3]; int counter = 0; string = strtok(date,"/"); while(string != NULL){ date2[counter] = atoi(string); string = strtok(NULL,"/"); counter++; } for(int i=0; i<3; i++){ cout<< endl<
And i do not see any problem.
The piece of code looks just fine to me.
(What variables are member of the private part of your class, what is the point of making a constructor when you do not use the information made in a the function.)
You should show more code... give an example when it crashes... get into the loop.
Try
Date::Date(char * date){ day = atoi(strtok(date,"/")); month = atoi(strtok(NULL,"/")); year = atoi(strtok(NULL,"/")); }
Tested example
#include#include #include #include using namespace std; int main (){ char date[11]; cin >> date; int day = atoi(strtok(date,"/")); int month = atoi(strtok(NULL,"/")); int year = atoi(strtok(NULL,"/")); cout<< endl<
And working for me
1>------ Rebuild All started: Project: webclient4, Configuration: Debug Win32 ------ 1> stdafx.cpp 1> AssemblyInfo.cpp 1> main.cpp 1>main.cpp(37): warning C4996: 'strtok': This function or variable may be unsafe. Consider using strtok_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(197) : see declaration of 'strtok' 1>main.cpp(40): warning C4996: 'strtok': This function or variable may be unsafe. Consider using strtok_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\string.h(197) : see declaration of 'strtok' 1> Generating Code... 1> .NETFramework,Version=v4.0.AssemblyAttributes.cpp 1> webclient4.vcxproj -> D:\webclient4\Debug\webclient4.exe ========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
Dear friend,
i tried your code in visual studio 2010 express.It passses the build test but gives this warning.The warning is self explanatory.
The said code is deprecated and can produce dangerous results;which in your case seems to be crashing.
Hence follow the solution given with the warning itself.
strtok() will modify the original string by replacing a deliminator with ‘\0’.
... char * dateString = "1/1/1"; Date dateClass(dateString); ... ------------------------------------ ... Date dateClass("1/1/1"); ...
The code above will give you an access violation because your compiler will make dateString immutable and “1/1/1” will be treated as a constant which will also be immutable.
... char dateString[] = "1/1/1"; Date dateClass(dateString); ...
The code above will work because dateString can be modified as long as it’s within its boundaries. You can also make a copy of the original string and use the copy with strtok().
Sign in to your account