strlen()... quick question, quick answer :)
-
hello :) i was wondering what was wrong with the following code: lr = strlen(menu); lr is an intiger,... the reason i want to use strlen is because: my program accepts a letter for you to tell the computer what you want... when you put in more than one character into the cin << menu, the program breaks down and goes non-stop through the main loop. thanks for your help! ~SilverShalkin :rose:
The problem is you should be using C++ instead of C. Make menu a std::string and use menu.length() to find out how big it is. Otherwise, if menu is a char array, try strlen(&menu[0]); It's probably something like that, but I'm not sure - I don't program much C. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002
-
hello :) i was wondering what was wrong with the following code: lr = strlen(menu); lr is an intiger,... the reason i want to use strlen is because: my program accepts a letter for you to tell the computer what you want... when you put in more than one character into the cin << menu, the program breaks down and goes non-stop through the main loop. thanks for your help! ~SilverShalkin :rose:
Is menu a char or a char*? Nish
Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.
-
hello :) i was wondering what was wrong with the following code: lr = strlen(menu); lr is an intiger,... the reason i want to use strlen is because: my program accepts a letter for you to tell the computer what you want... when you put in more than one character into the cin << menu, the program breaks down and goes non-stop through the main loop. thanks for your help! ~SilverShalkin :rose:
I don´t really understand what are you trying to acomplish, but strlen counts the characters on the string until it finds a '\0', like "stan\0", so, if menu is declared as a char, there is no room for the ending mark (\0) and it keeps reading your program memory until it finds an invalid address or an address that has a 0 value! hope this helps! Gabriel don´t worry drink happy
-
The problem is you should be using C++ instead of C. Make menu a std::string and use menu.length() to find out how big it is. Otherwise, if menu is a char array, try strlen(&menu[0]); It's probably something like that, but I'm not sure - I don't program much C. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002
Christian Graus wrote: The problem is you should be using C++ instead of C. Make menu a std::string and use menu.length() to find out how big it is. Hmmm, not everyone want to use STD, I don't like it and try to avoid it when I can. And depending on what you are programming it's not always a good idea. I spend a lot of my time programming server applications where I need all the speed I can get, and a std::string is way slower than a char array ;-) - Anders Money talks, but all mine ever says is "Goodbye!"
-
Christian Graus wrote: The problem is you should be using C++ instead of C. Make menu a std::string and use menu.length() to find out how big it is. Hmmm, not everyone want to use STD, I don't like it and try to avoid it when I can. And depending on what you are programming it's not always a good idea. I spend a lot of my time programming server applications where I need all the speed I can get, and a std::string is way slower than a char array ;-) - Anders Money talks, but all mine ever says is "Goodbye!"
Anders Molin wrote: Hmmm, not everyone want to use STD, I don't like it and try to avoid it when I can. Why don't you like it ? Anders Molin wrote: depending on what you are programming it's not always a good idea. I have no doubt, but while that may be the case, overall, too many people use clunky C style strings and so on IMO. They are not safe, they are hard to use, but yes, they are faster in some instances. My main beef is that Universities do not teach C++, they teach C with classes. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002
-
Anders Molin wrote: Hmmm, not everyone want to use STD, I don't like it and try to avoid it when I can. Why don't you like it ? Anders Molin wrote: depending on what you are programming it's not always a good idea. I have no doubt, but while that may be the case, overall, too many people use clunky C style strings and so on IMO. They are not safe, they are hard to use, but yes, they are faster in some instances. My main beef is that Universities do not teach C++, they teach C with classes. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002
Christian Graus wrote: Why don't you like it ? Hmmm, I think most of STL makes things hard to use, yes, I really do... The strings are slow, the documentation sucks, and I don't like the way you use it... Christian Graus wrote: I have no doubt, but while that may be the case, overall, too many people use clunky C style strings and so on IMO. They are not safe, they are hard to use, but yes, they are faster in some instances. Well, C strings are not that hard to use, sure, there's a lot more that can go wrong, but there's also lot's of bennefits :-) Christian Graus wrote: My main beef is that Universities do not teach C++, they teach C with classes. I don't know about what they teaches, but I'll agree with you on that one ;-) - Anders Money talks, but all mine ever says is "Goodbye!"
-
I don´t really understand what are you trying to acomplish, but strlen counts the characters on the string until it finds a '\0', like "stan\0", so, if menu is declared as a char, there is no room for the ending mark (\0) and it keeps reading your program memory until it finds an invalid address or an address that has a 0 value! hope this helps! Gabriel don´t worry drink happy
Ok... i just jumped on to ask another question and saw the continuation of this question :) first off... char is not a pointer. its a character.... "wait i think i just figuered out my problem with my program :)" anyways... they reason i want to do it is to make the user of the program not beable to enter more than one char... because if you do, then problems accure "thats the thing i just figuered out" if i just make it for that the char is bigger like char[10] or somthing,... than would you run into problems entering more than one character? "most likly not." but the question remains. ok cristian, im going with you on this one :) the problem is: how do you declare my char menu as a std::string? Thanks everyone for your posts... "see you in the next question :)" ~SilverShalkin :rose:
-
Christian Graus wrote: Why don't you like it ? Hmmm, I think most of STL makes things hard to use, yes, I really do... The strings are slow, the documentation sucks, and I don't like the way you use it... Christian Graus wrote: I have no doubt, but while that may be the case, overall, too many people use clunky C style strings and so on IMO. They are not safe, they are hard to use, but yes, they are faster in some instances. Well, C strings are not that hard to use, sure, there's a lot more that can go wrong, but there's also lot's of bennefits :-) Christian Graus wrote: My main beef is that Universities do not teach C++, they teach C with classes. I don't know about what they teaches, but I'll agree with you on that one ;-) - Anders Money talks, but all mine ever says is "Goodbye!"
Anders Molin wrote: Hmmm, I think most of STL makes things hard to use, yes, I really do... The strings are slow, the documentation sucks, and I don't like the way you use it... I dunno about the string speed, I've never had to stress it. Beyond that, the docs in MSDN suck for a reason, but there are plenty of good books out there, and std::string is a long way from being a central feature. What do you use for containers then ? Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002
-
Anders Molin wrote: Hmmm, I think most of STL makes things hard to use, yes, I really do... The strings are slow, the documentation sucks, and I don't like the way you use it... I dunno about the string speed, I've never had to stress it. Beyond that, the docs in MSDN suck for a reason, but there are plenty of good books out there, and std::string is a long way from being a central feature. What do you use for containers then ? Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002
Christian Graus wrote: What do you use for containers then ? Well, a long time ago when I first tried templates I wrote a template-based list which I today use for almost everything. It's small fast and works for everything I need :) - Anders Money talks, but all mine ever says is "Goodbye!"
-
Christian Graus wrote: What do you use for containers then ? Well, a long time ago when I first tried templates I wrote a template-based list which I today use for almost everything. It's small fast and works for everything I need :) - Anders Money talks, but all mine ever says is "Goodbye!"
Well, if you don't need a variety of containers or built in algorithms then I suppose you've duplicated the idea of the STL all by yourself :-) Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002
-
Well, if you don't need a variety of containers or built in algorithms then I suppose you've duplicated the idea of the STL all by yourself :-) Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002
:) - Anders Money talks, but all mine ever says is "Goodbye!"