[Solved] C# MemoryCache Changing Data by Itself

Change: Utility.CacheHelper.SaveTocache(“MyKey”,a); to: Utility.CacheHelper.SaveTocache(“MyKey”,a.ToList()); and: List<String> b = Utility.CacheHelper.GetFromCache<List<String>>(“MyKey”); to: List<String> b = Utility.CacheHelper.GetFromCache<List<String>>(“MyKey”).ToList(); Technically just the latter change is required – the former will make it even more bulletproof. This is necessary since if any entries adding to the MemoryCache will be shared amongst callers – so if one caller manipulates the List then … Read more

[Solved] What is wrong with this C code?

I find out using fgets() is the cause of error in getting user input and use this string on strstr() function. By perform some good advice from Blastfurnace, woz and other guys in Question comment section I can fix this problem. Code #include <stdio.h> #include <stdlib.h> #include <string.h> char tracks[][80] = { “I left my … Read more

[Solved] Im not sure how to use strtok to separate tokens [duplicate]

#include <stdio.h> #include <string.h> int main(void) { char *order[] = {“Day”, “Month”, “Date”, “Year”, “Hour”, “Minute”, “Second”}; char text[] = “Saturday, July 8, 2017, 22:14:10″; char* delims = ” ,:”; char* token = strtok(text, delims); char** label = order; while(token) { printf(“%-8s: %s\n”, *label, token); token = strtok(NULL, delims); label++; } return 0; } Output: … Read more

[Solved] I cannot understand why this code gives a segmentation fault. Please, can anyone tell me where have I allocated a memory which cannot be used [closed]

Whoops! Looks like you’re trying to create a variable sized array (which doesn’t exist… kinda) with an undefined variable (which can be anything the system wants it to be). Use pointers instead, and let the user fill the variable before creating the array: int n; std::cin >> n; int* a = new int[n]; Or use … Read more

[Solved] Programming with C# console application

It takes an input string (input), splits it on the space character (input.Split(‘ ‘)) (presumably to get the number of “words”), adds 1 to the .Length of the resulting array (not sure why), converts that number to a binary string (Convert.ToString(int, 2) will convert the int to a base-2 number and return it as a … Read more

[Solved] Why no error is thrown in compilation? [closed]

Can this be a way to initialize an array without declaring its size? No. All int *a; does, if defining a pointer to int, which, as not being initialised, points somewhere, “nowhere”, to “invalid” memory. Any invocation of the []-operator on a, without beforehand having made a point to any valid memory (as for example … Read more

[Solved] Adding a number to the day or month or year in a date [duplicate]

In .NET you could do use the AddMonths method: DateTime date = new DateTime(2013, 5, 19); DateTime newDate = date.AddMonths(14); As far as parsing a date from a string using a specified format you could use the TryParseExact method: string dateStr = “19/05/2013”; DateTime date; if (DateTime.TryParseExact(dateStr, “dd/MM/yyyy”, CultureInfo.InvariantCulture, DateTimeStyles.None, out date)) { // successfully … Read more

[Solved] Remove all duplicate characters from a string (STL)

Solution simple as always: void RemoveDuplicates (std::string& input) { std::string::iterator it = std::unique(input.begin(), input.end()); input.erase(it, input.end()); std::cout << “New input = “<< input << std::endl; } Another solution to return a new string: std::string RemoveDuplicates (const std::string& input) { std::string newT(input); std::string::iterator it = std::unique(newT.begin(), newT.end()); newT.erase(it, newT.end()); return newT; } If desired result is … Read more

[Solved] Finding largest number in array [closed]

Try using std::max_element from <algorithm>: #include <algorithm> int i[] = { 1, 84, 11, 31 }; // for C++11 and later: int max = *std::max_element(std::begin(i), std::end(i)); // for C++03 or earlier: int max2 = *std::max_element(i, i + (sizeof(i) / sizeof(*i))); or if your array is static, you can just use an C++11 initializer list: auto … Read more