Diego
I have to use c++98 for application i am developing for, [EDIT2] i want to control the behavior of constructor based on a static member of class, (in my case a boolean) i want to control the behavior of constructor based on if it is a first ever call to constructor or not. For first ever constructor call, i want to execute some extra code but it must not execute in consecutive calls, For this i decided to have a static bool that will have default value of false, and after first constructor call, it will be set true for every other call. class data_sender { private: static bool library_initialized; public: // etc. }; This is kept private because i dont want consumer of this class to manipulate this directly. Its not const because obviously i want to change it inside constructor. Now i cannot assign the default value for this, it says c++ forbids this non-const static variable initialization. I think my approach is wrong here and i should be using some other method. I am relatively new to c++ so even if you point me to some article or tell me what to search for this. [EDIT] Another strange thing i noticed is, when i define the library_initialized as false within the .h file, class data_sender { .... }; bool data_sender::library_initialized = false; it gives me linker error. But if i define it inside the data_sender.cpp, it allows me to do this, bool data_sender::library_initialized = false; // This is inside data_sender.cpp
You should be doing this as data_sender::library_initialized = false; actually, afaik
Diego
yes😅
Well then you should be good just looking up an extension for C++ development in the vscode marketplace I don't know any since I use Visual Studio, so you'll have to find them yourself, unless someone else does
Harshal
You should be doing this as data_sender::library_initialized = false; actually, afaik
I did and it works but only when i write this inside .cpp file and not in .h file. How is that possible?
Diego
.h is only for declaration, and member initialization should not be inlined
Chandresh
but there is also some process of mingw installation nd path setup😅
Anonymous
but there is also some process of mingw installation nd path setup😅
Get mingw from winlibs.com and add the folder to your path (google it).
Cristi
Hello
Cristi
I need to transform this string 07/12/21 to December 7 2021. how to do that in C ? how to write function that identify 07 and stopping at / ?
Cristi
How to make a (similar python Dictionary where is stored string representation of months in numbers)
Cristi
Scanf
I have this scanf(" %[^\n]s", string); for (int i = 0; i <strlen(string); i++){ }But I don't know what do do after
klimi
I dont understand
klimi
I would just do scanf "%d/%d/%d"
Cristi
I would just do scanf "%d/%d/%d"
But if it get already written 07/12/21 data
Cristi
Which I can't introduce by myself
klimi
?
Naveen
Ok
AHMED
Can I have a getter function which returns 2 variables?
Harshal
Use struct or union. or you can use standard container to have pair of types returned.
Nameful
Anonymous
Why not?
Because it's error prone af
Cristi
I have this code int main() { char string[40]; char tempm[10], tempd[10], tempy[10]; int month, date, year; int count = 0; printf("Introduceti data: \n"); scanf(" %[^\n]s", string); for (int i = 0; i <strlen(string); i++){ if (string[i] == '/'){ count ++; } else{ if (count == 0){ strncat(tempm, &string[i], 1); } if (count == 1){ strncat(tempd, &string[i],1); } if (count == 2){ strncat(tempy, &string[i], 1); } } } printf("%s",tempm); }```Why when I am run it I get this result: ```Introduceti data: 12/12/12 �<�U12 How to delete �<�U
armandofsanchez
I think you should inicialize tempm
armandofsanchez
With memset
Cristi
With memset
I wrote: if (count == 0){ memset(tempm, string[i], 1*sizeof (char)); }and got this output: Introduceti data: 12/11/21 2h�U
Cristi
There's no null terminator in your arrays of characters
#include <stdio.h> #include <stdlib.h> #include <stdio.h> #include <string.h> int main() { char string[40]; char tempm[10], tempd[10], tempy[10]; int month, date, year; int count = 0; printf("Introduceti data: \n"); scanf(" %[^\n]s", string); for (int i = 0; i <strlen(string); i++){ if (string[i] == '/'){ if (count == 0){ memset(tempm, '\0', 1*sizeof(char)); } if (count == 1){ memset(tempd, '\0', 1*sizeof(char)); } count ++; } else{ if (count == 0){ memset(tempm, string[i], 1*sizeof (char)); } if (count == 1){ memset(tempd, string[i], 1*sizeof (char)); } if (count == 2){ strncat(tempy, &string[i], 1); } } memset(tempy, '\0', 1*sizeof(char)); } printf("%s",tempm); }
Cristi
There's no null terminator in your arrays of characters
Now there is, but there is no any output
Anonymous
Stupid bot
Anonymous
Now there is, but there is no any output
Use debugger, I'm not a psychic
Cristi
Use debugger, I'm not a psychic
Did I add correct null symbols at the end ? memset(tempy, '\0', 1*sizeof(char));
Anonymous
Now there is, but there is no any output
Well lol because you're clearing the string Do you like programming by randomly putting instructions until it works?
Anonymous
Learn how to use null terminated string
Cristi
Well lol because you're clearing the string Do you like programming by randomly putting instructions until it works?
I wrote in Python. I have to write a mini code in C for my friend, I already forgot practically all of it's syntax and I wan't to understand where I am doing mistakes
Cristi
No
Anonymous
No
Try to read some tutorials first
Anonymous
Quit
they wont refund the money tho.
Cristi
Try to read some tutorials first
I need to write this code in less than 1 hour :(
Anonymous
they wont refund the money tho.
And you even pay for this.. Poor you..
Anonymous
I need to write this code in less than 1 hour :(
Maybe someone can help you Sorry, I'm busy
Cristi
ok
Junaid
Is it always necessary to implement data hiding and encapsulation in a program? Explain with the help of an example in case yes or no
Anonymous
Wat
Anonymous
Any body can help
No one will solve your assignment
Cristi
I have to transform string '12/05/21' to May 12 2021. Does someone have any example of similar code ?
Lava
Or just user enters 12/05/21 . Transform that ?
Cristi
I have to write by myself this code. I get string '12/05/21' and to write a program that transform it in 'May 12 2021'
MRT
hi, what is that Segmentation fault SIGSEGV in QT ?
Lava
I have to write by myself this code. I get string '12/05/21' and to write a program that transform it in 'May 12 2021'
Read through the string with iterations over the month part only . Then store it in a variable say montht. In an strings assign month names for 12 iterations . If the iteration == montht then store the respective month into a string . Later print then month string and the initial string ( iterating over non month part ) .. this would be a brute force approach
Cristi
What type of variable should be montht?
Anonymous
/warn offtop
Cristi
Read through the string with iterations over the month part only . Then store it in a variable say montht. In an strings assign month names for 12 iterations . If the iteration == montht then store the respective month into a string . Later print then month string and the initial string ( iterating over non month part ) .. this would be a brute force approach
I tryed to write something similar: int main() { char string[40]; char tempm[10], tempd[10], tempy[10]; int month, date, year; int count = 0; printf("Introduceti data: \n"); scanf(" %[^\n]s", string); for (int i = 0; i <strlen(string); i++){ if (string[i] == '/'){ count ++; } else{ if (count == 0){ strncat(tempm, &string[i], 1); } if (count == 1){ strncat(tempd, &string[i],1); } if (count == 2){ strncat(tempy, &string[i], 1); } } } printf("%s",tempm); }
Cristi
Hey I think strtok is your answer
char string[40] = "12/06/21"; char *ptr1; char *ptr2; long tempd; long tempm; long tempy; for (int i=0; i< strlen(string); i++) { if (string[i] == '/') { tempd = strtol(string, &ptr1, i); for (int z=0; z< strlen(ptr1); z++){ if (ptr1[i] == '/') { tempd = strtol(string, &ptr1, i); }
Lava
what function of storing should I use ? (How I understood I can't use strncat)
Say If month iteration of string =="05" then monthstring="May"
Cristi
Harshal
If you want to Manually do everything, I guess you are reading from terminal. Scanf a string. Strtok with delim / Get 3 tokens. (Day month year) Transform them as per requirement.
Harshal
Something like this ?
Use strtok to separate tokens
Harshal
For more info, just google man strtok STRTOK(3) Linux Programmer's Manual STRTOK(3) NAME strtok, strtok_r - extract tokens from strings SYNOPSIS #include <string.h> char *strtok(char *str, const char *delim); char *strtok_r(char *str, const char *delim, char **saveptr); DESCRIPTION The strtok() function breaks a string into a sequence of zero or more nonempty tokens. On the first call to strtok(), the string to be parsed should be specified in str. In each subsequent call that should parse the same string, str must be NULL. The delim argument specifies a set of bytes that delimit the tokens in the parsed string. The caller may specify different strings in delim in successive calls that parse the same string. Each call to strtok() returns a pointer to a null-terminated string containing the next token. This string does not include the delimiting byte. If no more tokens are found, strtok() returns NULL. A sequence of calls to strtok() that operate on the same string maintains a pointer that determines the point from which to start searching for the next token. The first call to strtok() sets this pointer to point to the first byte of the string. The start of the next token is determined by scanning forward for the next nonde‐ limiter byte in str. If such a byte is found, it is taken as the start of the next token. If no such byte is found, then there are no more tokens, and strtok() re‐ turns NULL. (A string that is empty or that contains only delimiters will thus cause strtok() to return NULL on the first call.)
Cristi
If you want to Manually do everything, I guess you are reading from terminal. Scanf a string. Strtok with delim / Get 3 tokens. (Day month year) Transform them as per requirement.
What function should I use to get string representation of month ? for example I got month = 12. What should I use to identify the string name of this month ?
Cristi
if (month == '12' ) ?