Aug 18, 2015 06:45 PM
(This post was last modified: Aug 18, 2015 06:48 PM by 0xDEAD10CC.)
(Aug 17, 2015 05:52 PM)Blender Wrote: Sometimes in old C++ to input into the console people would need to do like int main(char(mander)*something) but it isnt required any longer because C++ is now upgraded into the cyberdalek form of happy. So, hopefully that clears everyting up sirs and sirs.
Old C++? There's nothing old about the common and valid signatures for the main() function, and absolutely nothing new about grabbing arguments via Win32 functions either... "C++ is now upgraded" -- You don't know what you're talking about at all.
(Aug 18, 2015 06:45 PM)0xDEAD10CC Wrote: Old C++? There's nothing old about the common and valid signatures for the main() function, and absolutely nothing new about grabbing arguments via Win32 functions either... "C++ is now upgraded" -- You don't know what you're talking about at all.
Or maybe you don't know how to interpret my words into something sensible which is quite alright since most normal people don't. What I am basically saying is that C++ has more or less "evolved" since the early 2000's and having anything within the goddamn main function is unnecessary unless you need to take parameters for something other than the console.
How do I know this?
GOOD QUESTION
I've coded applications inside the console before without the need of anything inside of the int main() function. I said "upgrade" because long ago when Yoda was still cool, you needed something inside of the int main() function if you wanted the console to take input.
I LIKE PIZZA
Aug 19, 2015 03:12 PM
(This post was last modified: Aug 19, 2015 03:19 PM by 0xDEAD10CC.)
Any interpretation of your words can't help anyone comprehend something sensible from what you're trying to say because there is no such sensible thing in regards to what you are trying to say. Anyone with real experience with the language wouldn't explain the concepts like that. C++ has evolved but any of the points of evolution of the language that you have mentioned have nothing to do with the language of C++ itself, nor have they been part of the "evolution" of the language because they have been there for as long as I've dealt with the C++ language. This valid signature:
Has been around for a long time within the series of language standards for C++.
This makes no sense:
Quote:having anything within the goddamn main function is unnecessary unless you need to take parameters for something other than the console
Something other than the console? Whatever it is you're trying to say -- either you don't know what it is you're trying to say exactly, or you make it sound like it's some kind of black magic when you change the signature of the main() function.
Quote:I've coded applications inside the console before without the need of anything inside of the int main() function.
You still don't know what you're talking about as far as I can tell, and hello world programs don't count. I never said it was invalid to have no parameters, I simply said that it was not "old C++" to do so as you were calling it. You don't even explain things with proper terminology...
(Aug 17, 2015 05:52 PM)Blender Wrote: Sometimes in old C++ to input into the console people would need to do like int main(char(mander)*something) but it isnt required any longer because C++ is now upgraded into the cyberdalek form of happy. So, hopefully that clears everyting up sirs and sirs.
Really, you should stop smoking that kraut, the side effects are too strong.
(Aug 19, 2015 03:12 PM)0xDEAD10CC Wrote: Any interpretation of your words can't help anyone comprehend something sensible from what you're trying to say because there is no such sensible thing in regards to what you are trying to say. Anyone with real experience with the language wouldn't explain the concepts like that. C++ has evolved but any of the points of evolution of the language that you have mentioned have nothing to do with the language of C++ itself, nor have they been part of the "evolution" of the language because they have been there for as long as I've dealt with the C++ language. This valid signature:
Has been around for a long time within the series of language standards for C++.
This makes no sense:
Something other than the console? Whatever it is you're trying to say -- either you don't know what it is you're trying to say exactly, or you make it sound like it's some kind of black magic when you change the signature of the main() function.
You still don't know what you're talking about as far as I can tell, and hello world programs don't count. I never said it was invalid to have no parameters, I simply said that it was not "old C++" to do so as you were calling it. You don't even explain things with proper terminology...
So why on earth would you use characters in the int main() ? I just don't really see why it's necessary.
(Aug 19, 2015 08:23 PM)Blender Wrote: So why on earth would you use characters in the int main() ? I just don't really see why it's necessary.
characters?... As I said, you don't know what you're talking about. It's an argument count and an array of c-strings...
(Aug 20, 2015 04:59 PM)0xDEAD10CC Wrote: characters?... As I said, you don't know what you're talking about. It's an argument count and an array of c-strings...
An argument count isn't needed in the main function of C++.
(Aug 20, 2015 07:42 PM)Blender Wrote: An argument count isn't needed in the main function of C++.
@noobs reading this: Please ignore the above, it's nonsense - and the compilers know it. Example:
Code #include <stdio.h>
// wrong arg list:
int main(char *argv[]) {
int argc=2;
for (int i=0;i<argc;i++) printf("%i\t%s\n", i, argv[i]);
return 0;
}
G++ warnings:
Code Tmp.cpp:3:5: warning: first argument of 'int main(char**)' should be 'int' [-Wmain]
int main(char *argv[]) {
^
Tmp.cpp:3:5: warning: 'int main(char**)' takes only zero or two arguments [-Wmain]
It gets compiled, though, but the exe crashes.
The correct way is like this:
Code #include <stdio.h>
int main(int argc, char *argv[]) {
for (int i=0;i<argc;i++) printf("%i\t%s\n", i, argv[i]);
return 0;
}
Note that argv[0] is the name of the executable itself. Launched e.g. with Tmp.exe These are the arguments, you get this:
Code 0 Tmp.exe
1 These
2 are
3 the
4 arguments
If you don't need arguments, you can choose
int main() {
or
int main(void) {
I seriously didnt get what I should do, what do you mean by taking out characters?
is it for example my string is "hello world"
you choose to take "llo" off so I should print "he world"?
please dont hate me :( I would love to do this challenge, please explain :)
Aug 21, 2015 05:37 PM
(This post was last modified: Aug 21, 2015 05:39 PM by Masm4ever.)
(Aug 21, 2015 03:02 PM)Hacktivator Wrote: I seriously didnt get what I should do, what do you mean by taking out characters?
is it for example my string is "hello world"
you choose to take "llo" off so I should print "he world"?
please dont hate me :( I would love to do this challenge, please explain :)
Yes, exactly. Something like this:
Code #include <stdio.h>
int main(void) {
char s[]="hello world";
strcpy(&s[2], &s[5]);
printf("[%s]", &s[0]);
}
There are various ways to achieve this, strcpy() being the simplest one. Find your own, and explain why it works.
Or start by explaing why strcpy() works ;-)
(Aug 20, 2015 07:42 PM)Blender Wrote: An argument count isn't needed in the main function of C++.
You have fun with buffer overruns then and determining somehow on how many pointers argv contains 🤝
Clearly as proven yet again you have no clue. A main() function that has only 1 parameter is not specified by any valid signature for main() in any of the language standards...
Aug 22, 2015 02:57 AM
(This post was last modified: Aug 22, 2015 03:03 AM by sunrix.)
Guys I really tried this challenge. This is what I came up with.(Mind the extreme "noobness"). I'm still an extreme beginner.
PHP Code
<?php #include <stdio.h> #include <stdlib.h> #include <string.h>
int main() { char str[] = "This is a C string to work with"; char sub[20], new[50]; char *ptr; int num;
printf("Enter in a substring to delete ?:"); fgets(sub, 20, stdin);
printf("Enter in the position of substring in the string (starting from 0):"); scanf("%d", &num);
sub[strcspn(sub, "\n")] = '\0';
strcpy(new, str);
ptr = strstr(new, sub);
if (ptr == NULL) { printf("Substring does not exist...\n"); exit(0); } memmove((new + num), (ptr + strlen(sub)), (strlen(ptr + strlen(sub)) + 1));
printf("%s\n", new);
return 0; }
I just want to know is there a way I can get the index number without having to count till the starting of the substring. ? Thanks for the challenge :)
My solution based on the explanation suggested by Hacktivator :
> is it for example my string is "hello world"
>you choose to take "llo" off so I should print "he world"?
PHP Code
<?php #include <stdio.h> #include <string.h>
void remove_chars(char *str, char *substr); int main(int argc, char *argv[]) { char original[] = "foobarfoobarfoobar"; remove_chars(original, "foo"); printf("[%s]\n", original); return 0; }
void remove_chars(char *str, char *substr) { while(1) { char *occurrence = strstr(str, substr); if(occurrence == NULL) return; size_t substr_len = strlen(substr), str_len = strlen(str); size_t i; for(i = occurrence - str; i < str_len - substr_len; i++) str[i] = str[i + substr_len]; str[i] = '\0'; } }
Pros :
-No dynamic allocation
-Removes all the occurrences of the substring in the original string ((foobarfoobar, foo) becomes barbar)
Cons :
-I may have done something stupid without realizing it
-The for loop could probably be replaced by a clever function call
|