Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. strlen()... quick question, quick answer :)

strlen()... quick question, quick answer :)

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
12 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S SilverShalkin

    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:

    N Offline
    N Offline
    Nish Nishant
    wrote on last edited by
    #3

    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.

    1 Reply Last reply
    0
    • S SilverShalkin

      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:

      G Offline
      G Offline
      Gabriel P G
      wrote on last edited by
      #4

      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

      S 1 Reply Last reply
      0
      • C Christian Graus

        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

        A Offline
        A Offline
        Anders Molin
        wrote on last edited by
        #5

        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!"

        C 1 Reply Last reply
        0
        • A Anders Molin

          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!"

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #6

          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

          A 1 Reply Last reply
          0
          • C Christian Graus

            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

            A Offline
            A Offline
            Anders Molin
            wrote on last edited by
            #7

            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!"

            C 1 Reply Last reply
            0
            • G Gabriel P G

              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

              S Offline
              S Offline
              SilverShalkin
              wrote on last edited by
              #8

              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:

              1 Reply Last reply
              0
              • A Anders Molin

                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!"

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #9

                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

                A 1 Reply Last reply
                0
                • C Christian Graus

                  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

                  A Offline
                  A Offline
                  Anders Molin
                  wrote on last edited by
                  #10

                  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!"

                  C 1 Reply Last reply
                  0
                  • A Anders Molin

                    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!"

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #11

                    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

                    A 1 Reply Last reply
                    0
                    • C Christian Graus

                      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

                      A Offline
                      A Offline
                      Anders Molin
                      wrote on last edited by
                      #12

                      :) - Anders Money talks, but all mine ever says is "Goodbye!"

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups