Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C++ string handler problem
#11
Quote:And btw, what's wrong with main() returning void?

Compatibility. The value returned by main is offered to the OS as an exit value of your executable. That value has a meaning. Usually you use "0" (0x00) for correct execution and -1 (0xFF) for fatal error. If you make the function void that means that god knows which value is being return. It may work in MSDOS, but you are not sure how the different operating systems may behave with a program returning another thing that 0 even when it ran correctly.

The best practice is:

Code:
int main(void)
{
   ...
   return (0);
}
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#12
Quote:Neo's post demonstrates that C syntax is nicer without all that ++ dirt. Btw, neo, why are you using unsigned chars? Chars will work as well. Nothing wrong with your approach, but you have to type more
Can c do this?

Code:
int main(void)
{
String stringname;
stringname = "C++";
stringname += "rocks!";
printf("%s",stringname.c_str());
return 0;
}
(heh, im tired i almost wrote java's System.out.println)

String handling rocks, as well as notice no need to put a free(stringname), deconstructor/constructors rock as do operators overloading and a whole bunch of other stuff i could go on about.

Theres NOTHING wrong with C++. although the casting is a little annoying.

Btw nathan, when do you want to the java game? ive picked up a little java, looks like c++ without pointers :p (and memory allocation)


Quote:Huh. I actually am (I'm ashamed to admit it) a C++-only guy. I don't know C. I never looked at printf, I had no idea it could print char arrays with no trouble.
Uh-oh...don't tell me that cout can do that too
Okay we wont, note that couts operations can be overloading so you can print a struct_of_your_entire_game if you wanted :p

[edit]
Note, im not trying to say c++ roxxors c or anything, right now 90% of my coding i do is C, (5% c++, 5%java, 0.000001% qb in there somewhere), but c++ has many features that shouldnt be overlooked, and can shorten dev time.
b]Hard Rock[/b]
[The Stars Dev Company] [Metal Qb flopped] [The Terror]
Stop Double Posts!
Whats better? HTML or Variables?
Reply
#13
Just out of interest, why does 0 represent success and -1 represent failure when 0 is FALSE and -1 is TRUE?
Reply
#14
Quote:Can c do this?
Code:
int main(void)
{
  String stringname;
  stringname = "C++";
  stringname += "rocks!";
  printf("%s",stringname.c_str());
  return 0;
}

I used to think this was quite cool when I learnt Java, until I discovered that the += isnt doing exactly what it appears to be. I dont know if the same is true in C++, but in Java strings are persistant objects, they cannot be changed. They way that the += operator works with strings is internally Java creates a new string object and puts both concatenates the two strings together to form the new object. This can cause headaches when trying to pass Strings by reference because the reference object for a string can change several times. With C, it may be a little more work (although you could use wrapper functions) but at least you know exactly what is happening.

Quote:Just out of interest, why does 0 represent success and -1 represent failure when 0 is FALSE and -1 is TRUE?
Dunno, although the return value is usually error status. ie returning 0 means that it is false that an error was returned.

Quote:Instead of fgets you can also use "gets" which automaticly reads from stdin.
You should /never/ use gets, read the man page for it. If you use gets you will get buffer overflows, because there is no way to determine where the end of input is.
esus saves.... Passes to Moses, shoots, he scores!
Reply
#15
Quote:Instead of fgets you can also use "gets" which automaticly reads from stdin, or maybe scanf:

Code:
scanf("%s", strBuf);

:rotfl: serisuly nathan everyone with more then 2 brain cells knows that's BS really funny joke nathan since everyone knows gets is a rellic from a long ago past time and what happens if you write outside the buffer and scanf shouldn't be used like that scanf is kinda useless sscanf and fscanf on the other hand.... funny on nathan I'm still laughing... :rotfl:
Reply
#16
Quote: used to think this was quite cool when I learnt Java, until I discovered that the += isnt doing exactly what it appears to be. I dont know if the same is true in C++, but in Java strings are persistant objects, they cannot be changed. They way that the += operator works with strings is internally Java creates a new string object and puts both concatenates the two strings together to form the new object. This can cause headaches when trying to pass Strings by reference because the reference object for a string can change several times. With C, it may be a little more work (although you could use wrapper functions) but at least you know exactly what is happening

Well, im no expert on java, but i'd guess that in C++ (becuase of the uber cool pointers) that new memory is re-allocated within the String class and that is then given the += values. String should still be the same, the pointer inside String probably wont be. It'd work the same way if you wrote your own wrapper probably. Since you pass String, in a reference, or call .c_str() everytime you use it to update the adress, you shouldnt have a problem. (that probably why .c_str() has the tendency to change, the re-allocation of memory)

Anyways trying to understand your java refernce you mean something like this happens?

Code:
JAVA STRING HERE = "HERE"
(lets give this the adress 1)

JAVA STRING HERE += "THERE"
(internaly a new JAVA STRING CREATED, has the adress of 2). JAVA STRING HERE now has the adress of 2, while adress 1 = "HERE".
Is that what you mean? in that case, wouldnt in java no MATTER what string manipulation you perform, always create a new STRING, whenever you manipulate it?


Btw if you think strings do a lot you cant see, you should take a look at what vectors do when you push_back. new memory is allocated, bitwise copy is performed, and the old copy is deleted. That can cause problems if your decontructer free's memory, and your bitwise operator doesnt malloc any new mem.
b]Hard Rock[/b]
[The Stars Dev Company] [Metal Qb flopped] [The Terror]
Stop Double Posts!
Whats better? HTML or Variables?
Reply
#17
Quote:
na_th_an Wrote:Instead of fgets you can also use "gets" which automaticly reads from stdin, or maybe scanf:

Code:
scanf("%s", strBuf);

:rotfl: serisuly nathan everyone with more then 2 brain cells knows that's BS really funny joke nathan since everyone knows gets is a rellic from a long ago past time and what happens if you write outside the buffer and scanf shouldn't be used like that scanf is kinda useless sscanf and fscanf on the other hand.... funny on nathan I'm still laughing... :rotfl:

Funny how the spammer laughs at me... Funny indeed. May I have two brain cells but I use them to try to help and not posting silly posts and links to stupid sites.
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#18
Quote:Funny how the spammer laughs at me... Funny indeed. May I have two brain cells but I use them to try to help and not posting silly posts and links to stupid sites.

I guess I put it kinda harsh....The message of my post was that gets and scanf are relics in the way their used today, they had meaning for a long time ago but today thier relics didn't mean to insult you.
Reply
#19
Quote:serisuly nathan everyone with more then 2 brain cells knows that's BS really funny joke nathan since everyone knows gets is a rellic from a long ago past time and what happens if you write outside the buffer and scanf shouldn't be used like that scanf is kinda useless sscanf and fscanf on the other hand.... funny on nathan I'm still laughing...
Dear Mr. Misspell-a-lot Shogun:
C/C++ in and of itself is a relic from long ago. And so is QB. Welcome to the land of relics. Doesn't that make your annoying argument a little...moot? I love your skill (or lack thereof) in English, with your amazingly wonderful run-on sentences and your blatant spelling errors. Lovely.

Maybe you need to go back to school. And no one messes with na_th_an...got it? Good.

Now go sit in the corner and behave yourself.

(and if anyone fails to see the satire in this post, then that is just sad.) Big Grin
I'd knock on wood, but my desk is particle board.
Reply
#20
adosorken http://www.albinoblacksheep.com/flash/you.html

Yes C/C++ are relics from a long ago forgoten time but the fact that they're still the most used laughes (not including Java that is) and that people develep them makes them highly upto date I'm sure you know all of all the new C standards for example. And my spelling? I should go back to school? Thinking of that I'm going to school edjucating myself and so getting straight As in english I think you should STFU.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)