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. simple keyboard input ?

simple keyboard input ?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++data-structurestutoriallearning
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.
  • B bitsNbites

    My C++ book says that trying to store more characters then the array has space for can cause problems. What the book I have doesn't tell me is how to prevent a user from entering too many characters. The code sample is; char name[31]; cout << " Enter your name: "; cin >> name; What is the best method to take text input from the user? If only I had more time!

    D Offline
    D Offline
    David Crow
    wrote on last edited by
    #3

    One way would be to accept one character at a time until either Enter is pressed or the maximum number of characters has been reached. Search for examples using getch() and getche().


    "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

    1 Reply Last reply
    0
    • B bitsNbites

      My C++ book says that trying to store more characters then the array has space for can cause problems. What the book I have doesn't tell me is how to prevent a user from entering too many characters. The code sample is; char name[31]; cout << " Enter your name: "; cin >> name; What is the best method to take text input from the user? If only I had more time!

      A Offline
      A Offline
      Andrew Walker
      wrote on last edited by
      #4

      Use a C++ type which is dynamically expandable,std::string s; std::cin >> s;
      Which will be fine unless you have whitespace in the name, and then you would need something like (this would have been a problem with the exising code as well):std::string s; std::getline(cin,s);
      If you wanted to get really fancy you could also use an istream_iterator, but for examples this simple I don't think it really buys you anything.


      1 Reply Last reply
      0
      • B bitsNbites

        My C++ book says that trying to store more characters then the array has space for can cause problems. What the book I have doesn't tell me is how to prevent a user from entering too many characters. The code sample is; char name[31]; cout << " Enter your name: "; cin >> name; What is the best method to take text input from the user? If only I had more time!

        N Offline
        N Offline
        ng kok chuan
        wrote on last edited by
        #5

        is there any limitations on the type used? i guess this is for a school project right? if your project does not have any limitations, use the string class. #include using std::cout; using std::endl; using std::cin; #include using std::string; using std::getline; int main() { string test; cout << "name :"; getline(cin,test); cout << "Hello " << test << endl; return 1; } this effectively removes any limitiations on character size inputs, which imho, is bad. imagine you have an indian name (no offence intended) and an english name. normally indian names are longer, so if you used a larger array size, the english name wastes a lot of memory. string dynamically allocates the memory spaces, so nothing is wasted. and you don't have to explicitly deal with the memory size (i.e. no magic numbers). lastly, you'll find that getline will require you to press return twice to get the line if u're using vc++. this is a bug in the implementation of string. follow http://www.tek-tips.com/faqs.cfm?fid=5193 to fix the bug. hope this is clear enough. "Learning does not make one learned: there are those who have knowledge and those who have understanding. The first requires memory, the second philosophy." - Abbe Faria, The Count on Monte Cristo

        B D 2 Replies Last reply
        0
        • N ng kok chuan

          is there any limitations on the type used? i guess this is for a school project right? if your project does not have any limitations, use the string class. #include using std::cout; using std::endl; using std::cin; #include using std::string; using std::getline; int main() { string test; cout << "name :"; getline(cin,test); cout << "Hello " << test << endl; return 1; } this effectively removes any limitiations on character size inputs, which imho, is bad. imagine you have an indian name (no offence intended) and an english name. normally indian names are longer, so if you used a larger array size, the english name wastes a lot of memory. string dynamically allocates the memory spaces, so nothing is wasted. and you don't have to explicitly deal with the memory size (i.e. no magic numbers). lastly, you'll find that getline will require you to press return twice to get the line if u're using vc++. this is a bug in the implementation of string. follow http://www.tek-tips.com/faqs.cfm?fid=5193 to fix the bug. hope this is clear enough. "Learning does not make one learned: there are those who have knowledge and those who have understanding. The first requires memory, the second philosophy." - Abbe Faria, The Count on Monte Cristo

          B Offline
          B Offline
          bitsNbites
          wrote on last edited by
          #6

          Yes this is a school Project. This C++ is really hard to master. I added one line and got 45 errors. I have to take input from the user and pass to a method which takes a pointer, a char pointer. So I create and fill an array and pass the first element address to the method. I think I have most coded right except that I get a continuous scrolling at line 21. 17 char sValue[30]; 18 int iData; 19 cout << " Enter a name for the Salesman \n" 20 << " limited to 29 chars:"; 21 cin.get(sValue,30); 22 this->SetAgentName(sValue); I took this directly from my book however the sample code in the book only has two lines (# 17 and # 21). There seems to be so many things that all interact to make C++ extra difficult. Can any one tell my why my screen constantly scrolls at the line with .get? If only I had more time!

          N 1 Reply Last reply
          0
          • B bitsNbites

            Yes this is a school Project. This C++ is really hard to master. I added one line and got 45 errors. I have to take input from the user and pass to a method which takes a pointer, a char pointer. So I create and fill an array and pass the first element address to the method. I think I have most coded right except that I get a continuous scrolling at line 21. 17 char sValue[30]; 18 int iData; 19 cout << " Enter a name for the Salesman \n" 20 << " limited to 29 chars:"; 21 cin.get(sValue,30); 22 this->SetAgentName(sValue); I took this directly from my book however the sample code in the book only has two lines (# 17 and # 21). There seems to be so many things that all interact to make C++ extra difficult. Can any one tell my why my screen constantly scrolls at the line with .get? If only I had more time!

            N Offline
            N Offline
            ng kok chuan
            wrote on last edited by
            #7

            ok, two things. firstly cin.get() only gets single characters, one at a time. cin.getline() will get a whole line, up to the end of the '\n' character. i'm not sure what u're doing here.. is this a class or the main method? if u're in the main method, this-> does not have any value associated to it at all. as i said, using a string solves a lot of problems. since u're using c++, u should use the available STLs (standard template libraries) whenever possible. using a char array in c++ is indeed foolhardy. using the code i wrote previously, u can pass the string as a reference. // class declaration class myclass { public: void mymethod(string&); }; // method declaration void myclass::mymethod(string &instr) { cout<

            B 1 Reply Last reply
            0
            • N ng kok chuan

              ok, two things. firstly cin.get() only gets single characters, one at a time. cin.getline() will get a whole line, up to the end of the '\n' character. i'm not sure what u're doing here.. is this a class or the main method? if u're in the main method, this-> does not have any value associated to it at all. as i said, using a string solves a lot of problems. since u're using c++, u should use the available STLs (standard template libraries) whenever possible. using a char array in c++ is indeed foolhardy. using the code i wrote previously, u can pass the string as a reference. // class declaration class myclass { public: void mymethod(string&); }; // method declaration void myclass::mymethod(string &instr) { cout<

              B Offline
              B Offline
              bitsNbites
              wrote on last edited by
              #8

              Ok, these lines of code are directly from the MSDN. char line[25]; cout << " Type a line terminated by carriage return\n>"; cin.get( line, 25 ); cout << line << endl; This will run fine when placed in my main, (first lines). However when placed in a method of a class and called, via the user selecting from a menu, the screen continuously scrolls. Do I need to clear the buffer? Is it always this difficult to code in C++? I have been spending a couple hours just to code to take a line of text from the user. Also what is the "compile with: /EHsc" that the MSDN shows? If only I had more time!

              N 1 Reply Last reply
              0
              • N ng kok chuan

                is there any limitations on the type used? i guess this is for a school project right? if your project does not have any limitations, use the string class. #include using std::cout; using std::endl; using std::cin; #include using std::string; using std::getline; int main() { string test; cout << "name :"; getline(cin,test); cout << "Hello " << test << endl; return 1; } this effectively removes any limitiations on character size inputs, which imho, is bad. imagine you have an indian name (no offence intended) and an english name. normally indian names are longer, so if you used a larger array size, the english name wastes a lot of memory. string dynamically allocates the memory spaces, so nothing is wasted. and you don't have to explicitly deal with the memory size (i.e. no magic numbers). lastly, you'll find that getline will require you to press return twice to get the line if u're using vc++. this is a bug in the implementation of string. follow http://www.tek-tips.com/faqs.cfm?fid=5193 to fix the bug. hope this is clear enough. "Learning does not make one learned: there are those who have knowledge and those who have understanding. The first requires memory, the second philosophy." - Abbe Faria, The Count on Monte Cristo

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #9

                ng kok chuan wrote: ...the english name wastes a lot of memory. The word "lot" is a very subjective term. If the machine has but a few MB of physical RAM, then the unnecessary allocation of a few more bytes might matter, but given everything else that Windows' memory manager is doing, the extra bytes will definitely go unnoticed.


                "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

                N 1 Reply Last reply
                0
                • D David Crow

                  ng kok chuan wrote: ...the english name wastes a lot of memory. The word "lot" is a very subjective term. If the machine has but a few MB of physical RAM, then the unnecessary allocation of a few more bytes might matter, but given everything else that Windows' memory manager is doing, the extra bytes will definitely go unnoticed.


                  "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

                  N Offline
                  N Offline
                  ng kok chuan
                  wrote on last edited by
                  #10

                  yup, i guess u're right. i've been too well-trained to think lean and mean :p.. however, using strings still have their advantages, as it dynamically allocates memory. this can help to prevent some wise user from entering longer than allocated mem space. "Learning does not make one learned: there are those who have knowledge and those who have understanding. The first requires memory, the second philosophy." - Abbe Faria, The Count on Monte Cristo

                  1 Reply Last reply
                  0
                  • B bitsNbites

                    Ok, these lines of code are directly from the MSDN. char line[25]; cout << " Type a line terminated by carriage return\n>"; cin.get( line, 25 ); cout << line << endl; This will run fine when placed in my main, (first lines). However when placed in a method of a class and called, via the user selecting from a menu, the screen continuously scrolls. Do I need to clear the buffer? Is it always this difficult to code in C++? I have been spending a couple hours just to code to take a line of text from the user. Also what is the "compile with: /EHsc" that the MSDN shows? If only I had more time!

                    N Offline
                    N Offline
                    ng kok chuan
                    wrote on last edited by
                    #11

                    maybe u could post more code. ur code is too cryptic for any form of debugging. esp post in detail how u called the code in the class and how the user select from a menu. normally, a scrolling screen is due to a improper looping. in this case, it might be because u've tried to use a while loop to get the user input from the menu, until the user decides to terminate the program with a special character, right? try removing the loop first if this is the case, and debug again. it's not difficult to code in c++ at all, just that you're not used to it. i've coded genetic algoritms, ant colony system and dijkstra's algorithm all in c++, with each algorithm averaging 1.5 weeks. i can't even begin to think of how to start my coding in VB.. "Learning does not make one learned: there are those who have knowledge and those who have understanding. The first requires memory, the second philosophy." - Abbe Faria, The Count on Monte Cristo

                    B 1 Reply Last reply
                    0
                    • N ng kok chuan

                      maybe u could post more code. ur code is too cryptic for any form of debugging. esp post in detail how u called the code in the class and how the user select from a menu. normally, a scrolling screen is due to a improper looping. in this case, it might be because u've tried to use a while loop to get the user input from the menu, until the user decides to terminate the program with a special character, right? try removing the loop first if this is the case, and debug again. it's not difficult to code in c++ at all, just that you're not used to it. i've coded genetic algoritms, ant colony system and dijkstra's algorithm all in c++, with each algorithm averaging 1.5 weeks. i can't even begin to think of how to start my coding in VB.. "Learning does not make one learned: there are those who have knowledge and those who have understanding. The first requires memory, the second philosophy." - Abbe Faria, The Count on Monte Cristo

                      B Offline
                      B Offline
                      bitsNbites
                      wrote on last edited by
                      #12

                      I found the fix. I needed to remove the newline character since cin.get(name, length) leaves it(the newline char) in the buffer. The line to add is a simple "cin.get(discard);" where discard is declared as a char variable. I am using a while loop for the menu. This menu was seeing the newline char constantly. Thanks for your help, I'm still trying to learn all the little things of C++ like this one. Randy If only I had more time!

                      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