(Aug 15, 2015 11:37 AM)0xDEAD10CC Wrote: Code #include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
size_t n = 4; /* index of the 'C' */
char s[] = "The C string to work with";
char *p = s;
memmove(p + 1, p, n); ++p; /* remove 'C' */
memmove(p + 1, p, n); ++p; /* remove extra space which was after 'C' */
puts(p);
exit(0);
}
Pros:
- no reallocations (removing chars means the buffer always shrinks -- no need to realloc really)
- no string concatenation of lower and upper halves to "remove" a char
Cons:
- original chars not preserved, NOTE: A fix for that might look something like below
Code #include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char ch;
size_t n = 4; /* index of the 'C' */
char s[] = "The C string to work with";
char *p = s;
ch = p[n];
memmove(p + 1, p, n); ++p; /* remove 'C' */
*(p - 1) = ch;
ch = p[n];
memmove(p + 1, p, n); ++p; /* remove extra space which was after 'C' */
*(p - 1) = ch;
puts(p);
fputs("Junked chars -> '", stdout);
fwrite(s, 1, p - s, stdout);
putc('\'', stdout);
exit(0);
}
So I'm a little confused. Isn't it easier to use Code string s = "The C string to work with";
? Also, why did you put void inside of the main function? Leaving it blank does the same thing, doesn't it? Unless it's more of a style decision.
(Aug 15, 2015 02:53 PM)_B1N4RY_ Wrote: Your main should always be declared as a void, as it "returns" nothing. On that note, you should always have a return 0 at the end of main ;)
I've never had to declare it as a void. It does this automatically if you leave it empty. Only time I've ever had to declare a void was when I created a function that had no return type. But with parameters, never.
And why isn't anyone doing
using namespace std ?
It saves the time to do std::cout(unless you're using something with multiple libraries that has the potential to conflict with the functions in either library, but I haven't seen that in the code presented on this thread anyway)
(Aug 15, 2015 02:48 PM)sdk Wrote: Actually can you help me with something, I practically made something that removes the beginning character from the string somehow...
It's kinda on topic.
Code #include "stdafx.h"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
for (int i = 0; i < 100; i++) {
std::cout << "The current number " + i << std::endl;
}
std::cin.ignore();
return 0;
}
Code #include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string s = "The current number";
for(int i = 0; i < 100; i++)
{
cout << s << ": " << i << endl;
}
cin.ignore();
return 0;
}
That will fix it.
Your main really doesn't need arguments so long as you're not planning to make it do that which in my opinion would be pretty messy and makes things too complicated, because you're most likely not going to be doing console applications for the rest of your life.
(Aug 15, 2015 07:01 PM)0xDEAD10CC Wrote: You are clearly confused yes. The whole point of the challenge was C strings, not C++ strings. And void means that no arguments can be passed to the function, it's not about style.
That's not what he was asking, and main does return an int, which is why it's declared to return int. :S
If it's not a solution to the challenge, it's off topic, get out of this thread.
Because 'using namespace std;' or 'using namespace' in general defeats the whole point of namespaces, and it's bad to be doing that. Lastly, this thread is not even about C++.
Can we lose the skids in this thread please?? 🤨
Honestly, what are you talking about? return 0 at the end of main()'s scope can be omitted as per the C++ standard, and reaching the terminating } of main() will have the same effect as 0. There's nothing even remotely related to memory leaks with the return of main(). Additionally, you should never omit the return type to any function, including main(). void main() is old, and now also, very poor practice, and so is main() without a return type.
And you don't think GUI applications take command line arguments? I can tell you that you're wrong.
Internet Explorer, Google Chrome, Firefox, Notepad, and pretty much 90% of all other programs I know, allow command line arguments for various things.
Proof: Open cmd.exe and invoke this ->
Listen m8 calm down I can see your pantalooms are ruffled. No need to call people names.
In C++ I never had to put void inside of a parentheses to indicate that there were no arguments because leaving it blank did that for me.
I've never had to put characters inside of the int main() parentheses because in C++, that isn't needed anymore to pass characters to the console. That's seriously some old-style C shit, m8. If you don't believe me, fire up a new cpp application in Visual Studio and create an app without using those characters.
Damn bruh chill out :(
(Aug 17, 2015 04:14 PM)0xDEAD10CC Wrote: Also, my code is C not C++. And there is a difference between a the void being there and being omitted if you do some research.
Maybe because my code IS C?...
Other than that, I don't know what you're talking about here:
It doesn't make sense to me.
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.
(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)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 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++.
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 :)
|