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. cin issues [modified]

cin issues [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
c++debugginghelp
17 Posts 3 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.
  • H Offline
    H Offline
    Harold_Wishes
    wrote on last edited by
    #1

    Hello, perhaps someone can answer this for me without me having to display my entire program. I am having issues with one of my cin statements getting read in by a user's keyboard input. I am using the debugger in Visual C++ and it appears as though the compiler is skipping this cin statement and moving to the next cout statement. This seems to happen when the program is moving from one function call to another in main(). I found a fix to this by adding 2 consecutive cin statements but I know this is not the correct way to program. Has anyone had a similar experience. If so, how did you handle it. Thanks, HRW. -- modified at 12:56 Monday 24th July, 2006

    D 1 Reply Last reply
    0
    • H Harold_Wishes

      Hello, perhaps someone can answer this for me without me having to display my entire program. I am having issues with one of my cin statements getting read in by a user's keyboard input. I am using the debugger in Visual C++ and it appears as though the compiler is skipping this cin statement and moving to the next cout statement. This seems to happen when the program is moving from one function call to another in main(). I found a fix to this by adding 2 consecutive cin statements but I know this is not the correct way to program. Has anyone had a similar experience. If so, how did you handle it. Thanks, HRW. -- modified at 12:56 Monday 24th July, 2006

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

      How are we expected to offer any reasonable solution without seeing the problematic code? We don't need to see all of it, just the relevant snippet.


      "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

      "Judge not by the eye but by the heart." - Native American Proverb

      H 1 Reply Last reply
      0
      • D David Crow

        How are we expected to offer any reasonable solution without seeing the problematic code? We don't need to see all of it, just the relevant snippet.


        "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

        "Judge not by the eye but by the heart." - Native American Proverb

        H Offline
        H Offline
        Harold_Wishes
        wrote on last edited by
        #3

        Here is a condensed version. There are 2 functions below. Function 1 gets called before function 2. The problem appears in function 2 where I had to enter 2 cin statements. I hope it's readable as the cut and paste did not translate well.

        void add_number_of_subunits (int& data)
        {
        char buff[1024]; // stores whatever the user types in
        unsigned int value;
        int charsRead;

        bool validEntry = false; // sets a flag so that the
                                // validation can be performed
                                 //in the loop below
        while (!validEntry)	
        { 	
          cout << endl;
          cout << "Please enter number of subunits: ";
          cin >> buff; 
        	
          if (sscanf(buff,"%u%n", &value,&charsRead)==1 && value >=1 && value <= 12)	
        	validEntry = true;	// test to see if the user enter a valid integer
        	                        // If it is valid, the flag is changed which allow the 
        	                        // program  to exit the loop. If not, the flag remains
        	                        // as is until the user enter a valid number.
        }
        
        data = value; // The value entered by the user is copied into "data" and returned to main()
                      // which determines the number of cycles the user will have to enter per
                      // subunit
        

        }

        void add_node_at_end(data& item, int& increment)
        {
        // what we are adding to
        char buffer[501] = {0}; // buffer for input

        cout << "Please enter the length of the protein sequence for Subunit " << increment << "> ";
        
            cin >> buffer; // I had to enter 2 cin statements since  the compiler for whatever reason
        cin.getline(buffer, 500, '\\n');               // was not accepting one of them.
        
        item.Length = buffer;
            memset(buffer, 0, 501);
        
        cout << "Please enter the protein sequence for Subunit " << increment << "> ";
        cin.getline(buffer, 500, '\\n');
        
        while (buffer == "\\n")
        {
        	cout << "A sequence is required!" << endl;
        	cout << "Please enter a sequence." << endl;
                    cin.getline(buffer, 500, '\\n');
        }
        
        item.Sequence = buffer;
        memset(buffer, 0, 501);
        item.number = item.Sequence.size();
        
        
        cout << "Enter brief status on the presence of a modified N\_Terminal: ";
        cin.getline(buffer, 500, '\\n');
        item.N\_Terminal = buffer;
        memset(buffer, 0, 501);
        
        cout << "Enter brief status on the presence of a modified C\_Terminal: ";
        cin.getline(buffer, 500, '\\n');
        item.C\_Terminal = buffer;
        memset(buffer, 0, 501);
        
        g\_DataList.push\_back(item)
        
        D 1 Reply Last reply
        0
        • H Harold_Wishes

          Here is a condensed version. There are 2 functions below. Function 1 gets called before function 2. The problem appears in function 2 where I had to enter 2 cin statements. I hope it's readable as the cut and paste did not translate well.

          void add_number_of_subunits (int& data)
          {
          char buff[1024]; // stores whatever the user types in
          unsigned int value;
          int charsRead;

          bool validEntry = false; // sets a flag so that the
                                  // validation can be performed
                                   //in the loop below
          while (!validEntry)	
          { 	
            cout << endl;
            cout << "Please enter number of subunits: ";
            cin >> buff; 
          	
            if (sscanf(buff,"%u%n", &value,&charsRead)==1 && value >=1 && value <= 12)	
          	validEntry = true;	// test to see if the user enter a valid integer
          	                        // If it is valid, the flag is changed which allow the 
          	                        // program  to exit the loop. If not, the flag remains
          	                        // as is until the user enter a valid number.
          }
          
          data = value; // The value entered by the user is copied into "data" and returned to main()
                        // which determines the number of cycles the user will have to enter per
                        // subunit
          

          }

          void add_node_at_end(data& item, int& increment)
          {
          // what we are adding to
          char buffer[501] = {0}; // buffer for input

          cout << "Please enter the length of the protein sequence for Subunit " << increment << "> ";
          
              cin >> buffer; // I had to enter 2 cin statements since  the compiler for whatever reason
          cin.getline(buffer, 500, '\\n');               // was not accepting one of them.
          
          item.Length = buffer;
              memset(buffer, 0, 501);
          
          cout << "Please enter the protein sequence for Subunit " << increment << "> ";
          cin.getline(buffer, 500, '\\n');
          
          while (buffer == "\\n")
          {
          	cout << "A sequence is required!" << endl;
          	cout << "Please enter a sequence." << endl;
                      cin.getline(buffer, 500, '\\n');
          }
          
          item.Sequence = buffer;
          memset(buffer, 0, 501);
          item.number = item.Sequence.size();
          
          
          cout << "Enter brief status on the presence of a modified N\_Terminal: ";
          cin.getline(buffer, 500, '\\n');
          item.N\_Terminal = buffer;
          memset(buffer, 0, 501);
          
          cout << "Enter brief status on the presence of a modified C\_Terminal: ";
          cin.getline(buffer, 500, '\\n');
          item.C\_Terminal = buffer;
          memset(buffer, 0, 501);
          
          g\_DataList.push\_back(item)
          
          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Harold_Wishes wrote:

          cin >> buffer; // I had to enter 2 cin statements since the compiler for whatever reason cin.getline(buffer, 500, '\n'); // was not accepting one of them.

          What compiler error were you receiving (when only one was present)?

          Harold_Wishes wrote:

          while (buffer == "\n")

          What are you hoping to accomplish with this? If you are going to create a C++ program, quit messing around with char[] variables. Use string instead, especially since you are populating them with cin.

          H 1 Reply Last reply
          0
          • D David Crow

            Harold_Wishes wrote:

            cin >> buffer; // I had to enter 2 cin statements since the compiler for whatever reason cin.getline(buffer, 500, '\n'); // was not accepting one of them.

            What compiler error were you receiving (when only one was present)?

            Harold_Wishes wrote:

            while (buffer == "\n")

            What are you hoping to accomplish with this? If you are going to create a C++ program, quit messing around with char[] variables. Use string instead, especially since you are populating them with cin.

            H Offline
            H Offline
            Harold_Wishes
            wrote on last edited by
            #5

            The compiler is not giving me an error per se. It's building the executable. But, when I run the executable, I am prompted to enter data in sequence with a series of multiple cout prompts and cin inputs. I have no problems when I am doing this in the first function call. But when main() makes a call to function 2 after function 1 terminates, the very first cin input in function 2 seems to get skipped (at least that is what the debugger is showing me). It does not recognize it). The debugger moves right to the next cout prompt. :( If I remember correctly, I took this out of functions and slapped it out all in main() without any troubles.

            D 1 Reply Last reply
            0
            • H Harold_Wishes

              The compiler is not giving me an error per se. It's building the executable. But, when I run the executable, I am prompted to enter data in sequence with a series of multiple cout prompts and cin inputs. I have no problems when I am doing this in the first function call. But when main() makes a call to function 2 after function 1 terminates, the very first cin input in function 2 seems to get skipped (at least that is what the debugger is showing me). It does not recognize it). The debugger moves right to the next cout prompt. :( If I remember correctly, I took this out of functions and slapped it out all in main() without any troubles.

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

              Sans the comments, and extraneous code, this should get you real close:

              void add_number_of_subunits (int& data)
              {
              string buff;

              do
              { 	
                  cout << endl;
                  cout << "Please enter number of subunits: ";
                  cin >> buff; 
              	
              } while (atoi(buff.c\_str()) < 1 || atoi(buff.c\_str()) > 12);	
              
              data = atoi(buff.c\_str()); 
              

              }

              void add_node_at_end(_data& item, int& increment)
              {
              string buffer;

              cout << "Please enter the length of the protein sequence for Subunit " << increment << "> ";
              cin >> buffer;
              
              item.Length = buffer;
              
              cout << "Please enter the protein sequence for Subunit " << increment << "> ";
              cin >> buffer;
              
              item.Sequence = buffer;
              item.number = item.Sequence.size();
              
              cout << "Enter brief status on the presence of a modified N\_Terminal: ";
              cin >> buffer;
              item.N\_Terminal = buffer;
              
              cout << "Enter brief status on the presence of a modified C\_Terminal: ";
              cin >> buffer;
              item.C\_Terminal = buffer;
              
              cout << endl;
              
              increment++;
              

              }


              "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

              "Judge not by the eye but by the heart." - Native American Proverb

              Z H 2 Replies Last reply
              0
              • D David Crow

                Sans the comments, and extraneous code, this should get you real close:

                void add_number_of_subunits (int& data)
                {
                string buff;

                do
                { 	
                    cout << endl;
                    cout << "Please enter number of subunits: ";
                    cin >> buff; 
                	
                } while (atoi(buff.c\_str()) < 1 || atoi(buff.c\_str()) > 12);	
                
                data = atoi(buff.c\_str()); 
                

                }

                void add_node_at_end(_data& item, int& increment)
                {
                string buffer;

                cout << "Please enter the length of the protein sequence for Subunit " << increment << "> ";
                cin >> buffer;
                
                item.Length = buffer;
                
                cout << "Please enter the protein sequence for Subunit " << increment << "> ";
                cin >> buffer;
                
                item.Sequence = buffer;
                item.number = item.Sequence.size();
                
                cout << "Enter brief status on the presence of a modified N\_Terminal: ";
                cin >> buffer;
                item.N\_Terminal = buffer;
                
                cout << "Enter brief status on the presence of a modified C\_Terminal: ";
                cin >> buffer;
                item.C\_Terminal = buffer;
                
                cout << endl;
                
                increment++;
                

                }


                "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

                "Judge not by the eye but by the heart." - Native American Proverb

                Z Offline
                Z Offline
                Zac Howland
                wrote on last edited by
                #7

                This code looks vaguely familiar ... someone else was asking about a similar problem a couple weeks ago. Anyway, the problem you are having is that when using cin, it ignores whitespace by default (that is, it sees it, but leaves it in the buffer). This is generally a problem only when reading in strings, but can happen in certain circumstances when reading other datatypes. Basically, this is what happens: Lets say that the following string is in the read buffer for cin: MyString\n cin will read "MyString" into the buffer and leave the \n on the read buffer. Now, the next cin call will see the \n and assume an empty string. An easy way to fix the problem would be to change:

                DavidCrow wrote:

                void add_number_of_subunits (int& data) { string buff; do { cout << endl; cout << "Please enter number of subunits: "; cin >> buff; } while (atoi(buff.c_str()) < 1 || atoi(buff.c_str()) > 12); data = atoi(buff.c_str()); }

                to be this instead:

                void add_number_of_subunits (int& data)
                {
                    int check = 0;
                     
                    do
                    { 	
                        cout << endl;
                        cout << "Please enter number of subunits: ";
                        cin >> check; 
                    } while (check < 1 || check > 12);	
                 
                    data = check; 
                }
                

                If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                D H 2 Replies Last reply
                0
                • Z Zac Howland

                  This code looks vaguely familiar ... someone else was asking about a similar problem a couple weeks ago. Anyway, the problem you are having is that when using cin, it ignores whitespace by default (that is, it sees it, but leaves it in the buffer). This is generally a problem only when reading in strings, but can happen in certain circumstances when reading other datatypes. Basically, this is what happens: Lets say that the following string is in the read buffer for cin: MyString\n cin will read "MyString" into the buffer and leave the \n on the read buffer. Now, the next cin call will see the \n and assume an empty string. An easy way to fix the problem would be to change:

                  DavidCrow wrote:

                  void add_number_of_subunits (int& data) { string buff; do { cout << endl; cout << "Please enter number of subunits: "; cin >> buff; } while (atoi(buff.c_str()) < 1 || atoi(buff.c_str()) > 12); data = atoi(buff.c_str()); }

                  to be this instead:

                  void add_number_of_subunits (int& data)
                  {
                      int check = 0;
                       
                      do
                      { 	
                          cout << endl;
                          cout << "Please enter number of subunits: ";
                          cin >> check; 
                      } while (check < 1 || check > 12);	
                   
                      data = check; 
                  }
                  

                  If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

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

                  Zac Howland wrote:

                  This code looks vaguely familiar ... someone else was asking about a similar problem a couple weeks ago.

                  Same person. Still using sscanf() for validation, even though you provided him an alternative!

                  Zac Howland wrote:

                  int check = 0; ... cin >> check;

                  I thought about this (i.e., using an int), but since he was storing the input string for later processing, I left it as a string.

                  Z 1 Reply Last reply
                  0
                  • D David Crow

                    Sans the comments, and extraneous code, this should get you real close:

                    void add_number_of_subunits (int& data)
                    {
                    string buff;

                    do
                    { 	
                        cout << endl;
                        cout << "Please enter number of subunits: ";
                        cin >> buff; 
                    	
                    } while (atoi(buff.c\_str()) < 1 || atoi(buff.c\_str()) > 12);	
                    
                    data = atoi(buff.c\_str()); 
                    

                    }

                    void add_node_at_end(_data& item, int& increment)
                    {
                    string buffer;

                    cout << "Please enter the length of the protein sequence for Subunit " << increment << "> ";
                    cin >> buffer;
                    
                    item.Length = buffer;
                    
                    cout << "Please enter the protein sequence for Subunit " << increment << "> ";
                    cin >> buffer;
                    
                    item.Sequence = buffer;
                    item.number = item.Sequence.size();
                    
                    cout << "Enter brief status on the presence of a modified N\_Terminal: ";
                    cin >> buffer;
                    item.N\_Terminal = buffer;
                    
                    cout << "Enter brief status on the presence of a modified C\_Terminal: ";
                    cin >> buffer;
                    item.C\_Terminal = buffer;
                    
                    cout << endl;
                    
                    increment++;
                    

                    }


                    "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

                    "Judge not by the eye but by the heart." - Native American Proverb

                    H Offline
                    H Offline
                    Harold_Wishes
                    wrote on last edited by
                    #9

                    Although this did solve my original issue, it has caused 2 old problems to crop up again. 1) I want the user to be able to bypass keyboard entrys when they hit ENTER if they choose to skip a question. Now when I hit ENTER, the program will not let me go to the next cout prompt. It is waiting for something other than "\n" character. I believe this is why I used my getline implementation. 2)Also, if I type in something like "new terminal", such entrys delimited by a space seem to cause the next cin input to read in the second string. But I need to test the output some more with this second issue since I'm getting other strange results.

                    Z D 2 Replies Last reply
                    0
                    • Z Zac Howland

                      This code looks vaguely familiar ... someone else was asking about a similar problem a couple weeks ago. Anyway, the problem you are having is that when using cin, it ignores whitespace by default (that is, it sees it, but leaves it in the buffer). This is generally a problem only when reading in strings, but can happen in certain circumstances when reading other datatypes. Basically, this is what happens: Lets say that the following string is in the read buffer for cin: MyString\n cin will read "MyString" into the buffer and leave the \n on the read buffer. Now, the next cin call will see the \n and assume an empty string. An easy way to fix the problem would be to change:

                      DavidCrow wrote:

                      void add_number_of_subunits (int& data) { string buff; do { cout << endl; cout << "Please enter number of subunits: "; cin >> buff; } while (atoi(buff.c_str()) < 1 || atoi(buff.c_str()) > 12); data = atoi(buff.c_str()); }

                      to be this instead:

                      void add_number_of_subunits (int& data)
                      {
                          int check = 0;
                           
                          do
                          { 	
                              cout << endl;
                              cout << "Please enter number of subunits: ";
                              cin >> check; 
                          } while (check < 1 || check > 12);	
                       
                          data = check; 
                      }
                      

                      If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                      H Offline
                      H Offline
                      Harold_Wishes
                      wrote on last edited by
                      #10

                      To Zac: My last response was just made to the other member (David_Crow) who provided some feedback, but I understand your point with the string datatype and will test this out. Thanks HRW. -- modified at 15:29 Monday 24th July, 2006

                      1 Reply Last reply
                      0
                      • D David Crow

                        Zac Howland wrote:

                        This code looks vaguely familiar ... someone else was asking about a similar problem a couple weeks ago.

                        Same person. Still using sscanf() for validation, even though you provided him an alternative!

                        Zac Howland wrote:

                        int check = 0; ... cin >> check;

                        I thought about this (i.e., using an int), but since he was storing the input string for later processing, I left it as a string.

                        Z Offline
                        Z Offline
                        Zac Howland
                        wrote on last edited by
                        #11

                        DavidCrow wrote:

                        Same person. Still using sscanf() for validation, even though you provided him an alternative!

                        So it is. In C++, there are very few cases where using sscanf does not constitude a flaw in your implementation. This is not one of them.

                        DavidCrow wrote:

                        I thought about this (i.e., using an int), but since he was storing the input string for later processing, I left it as a string.

                        Since he needs it to set the iterations for a loop, it is easier (and more efficient) to read it as an int and to then call itoa or sprintf or use streambufs to store it for whatever purpose.

                        If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                        1 Reply Last reply
                        0
                        • H Harold_Wishes

                          Although this did solve my original issue, it has caused 2 old problems to crop up again. 1) I want the user to be able to bypass keyboard entrys when they hit ENTER if they choose to skip a question. Now when I hit ENTER, the program will not let me go to the next cout prompt. It is waiting for something other than "\n" character. I believe this is why I used my getline implementation. 2)Also, if I type in something like "new terminal", such entrys delimited by a space seem to cause the next cin input to read in the second string. But I need to test the output some more with this second issue since I'm getting other strange results.

                          Z Offline
                          Z Offline
                          Zac Howland
                          wrote on last edited by
                          #12

                          Harold_Wishes wrote:

                          1. I want the user to be able to bypass keyboard entrys when they hit ENTER if they choose to skip a question. Now when I hit ENTER, the program will not let me go to the next cout prompt. It is waiting for something other than "\n" character. I believe this is why I used my getline implementation.

                          That sounds familiar now. I don't agree with this behavior, but if you trully want it, the way to go about it is to do 2 consequetive getline calls (the first will get your data, the second will get you passed the '\n').

                          Harold_Wishes wrote:

                          2)Also, if I type in something like "new terminal", such entrys delimited by a space seem to cause the next cin input to read in the second string. But I need to test the output some more with this second issue since I'm getting other strange results.

                          That is the desired implementation for cin. You can override it, but generally, it is easier to just use getline if you need to get a string with spaces in it.

                          If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                          H 2 Replies Last reply
                          0
                          • H Harold_Wishes

                            Although this did solve my original issue, it has caused 2 old problems to crop up again. 1) I want the user to be able to bypass keyboard entrys when they hit ENTER if they choose to skip a question. Now when I hit ENTER, the program will not let me go to the next cout prompt. It is waiting for something other than "\n" character. I believe this is why I used my getline implementation. 2)Also, if I type in something like "new terminal", such entrys delimited by a space seem to cause the next cin input to read in the second string. But I need to test the output some more with this second issue since I'm getting other strange results.

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

                            Harold_Wishes wrote:

                            1. I want the user to be able to bypass keyboard entrys when they hit ENTER if they choose to skip a question. Now when I hit ENTER, the program will not let me go to the next cout prompt. It is waiting for something other than "\n" character. I believe this is why I used my getline implementation.

                            So what about something like:

                            do
                            {
                            cin.clear(); // reset cin, so more input can be done
                            cin.ignore(100, '\n'); // remove bad data, up to 100 characters
                            // or until the end of line is reached
                            cout << "Please enter number of subunits: ";
                            cin >> data;
                            } while (! cin && (data < 1 || data > 12));

                            Harold_Wishes wrote:

                            2)Also, if I type in something like "new terminal", such entrys delimited by a space seem to cause the next cin input to read in the second string. But I need to test the output some more with this second issue since I'm getting other strange results.

                            Then you would indeed need to use:

                            string buffer;
                            cout << "Please enter the length of the protein sequence for Subunit " << increment << "> ";
                            getline(cin, buffer);

                            1 Reply Last reply
                            0
                            • Z Zac Howland

                              Harold_Wishes wrote:

                              1. I want the user to be able to bypass keyboard entrys when they hit ENTER if they choose to skip a question. Now when I hit ENTER, the program will not let me go to the next cout prompt. It is waiting for something other than "\n" character. I believe this is why I used my getline implementation.

                              That sounds familiar now. I don't agree with this behavior, but if you trully want it, the way to go about it is to do 2 consequetive getline calls (the first will get your data, the second will get you passed the '\n').

                              Harold_Wishes wrote:

                              2)Also, if I type in something like "new terminal", such entrys delimited by a space seem to cause the next cin input to read in the second string. But I need to test the output some more with this second issue since I'm getting other strange results.

                              That is the desired implementation for cin. You can override it, but generally, it is easier to just use getline if you need to get a string with spaces in it.

                              If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                              H Offline
                              H Offline
                              Harold_Wishes
                              wrote on last edited by
                              #14

                              Regarding my first question, you're suggesting something like below:

                              char buffer[501] = {0}; // buffer for input

                              cout << "Please enter the length of the protein sequence for Subunit;
                              cin.getline(buffer, 500, '\n');
                              cin.getline(buffer, 500, '\n');

                              I probably need to modify the above some since both getline functions are doing the same thing -- modified at 16:21 Monday 24th July, 2006

                              Z 1 Reply Last reply
                              0
                              • Z Zac Howland

                                Harold_Wishes wrote:

                                1. I want the user to be able to bypass keyboard entrys when they hit ENTER if they choose to skip a question. Now when I hit ENTER, the program will not let me go to the next cout prompt. It is waiting for something other than "\n" character. I believe this is why I used my getline implementation.

                                That sounds familiar now. I don't agree with this behavior, but if you trully want it, the way to go about it is to do 2 consequetive getline calls (the first will get your data, the second will get you passed the '\n').

                                Harold_Wishes wrote:

                                2)Also, if I type in something like "new terminal", such entrys delimited by a space seem to cause the next cin input to read in the second string. But I need to test the output some more with this second issue since I'm getting other strange results.

                                That is the desired implementation for cin. You can override it, but generally, it is easier to just use getline if you need to get a string with spaces in it.

                                If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                                H Offline
                                H Offline
                                Harold_Wishes
                                wrote on last edited by
                                #15

                                For some reason, this seems to work although I do not know why it should. "cin" does not account for spaces. I added strings with spaces and no troubles much to my surprise :^)

                                char buffer[501] = {0}; // buffer for input

                                cout << "Please enter the length of the protein sequence;
                                cin >> buffer;
                                cin.getline(buffer, 500, '\n');

                                -- modified at 17:56 Monday 24th July, 2006 UPDATE: I see from the output 2 getlines are needed.

                                Z 1 Reply Last reply
                                0
                                • H Harold_Wishes

                                  Regarding my first question, you're suggesting something like below:

                                  char buffer[501] = {0}; // buffer for input

                                  cout << "Please enter the length of the protein sequence for Subunit;
                                  cin.getline(buffer, 500, '\n');
                                  cin.getline(buffer, 500, '\n');

                                  I probably need to modify the above some since both getline functions are doing the same thing -- modified at 16:21 Monday 24th July, 2006

                                  Z Offline
                                  Z Offline
                                  Zac Howland
                                  wrote on last edited by
                                  #16

                                  No, buffer would get overwritten the second time. char buffer[501] = {0}; // buffer for input char buffer1[501] = {0}; // buffer for clearing cout << "Please enter the length of the protein sequence for Subunit; cin.getline(buffer, 500, '\n'); cin.getline(buffer1, 500, '\n');

                                  If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                                  1 Reply Last reply
                                  0
                                  • H Harold_Wishes

                                    For some reason, this seems to work although I do not know why it should. "cin" does not account for spaces. I added strings with spaces and no troubles much to my surprise :^)

                                    char buffer[501] = {0}; // buffer for input

                                    cout << "Please enter the length of the protein sequence;
                                    cin >> buffer;
                                    cin.getline(buffer, 500, '\n');

                                    -- modified at 17:56 Monday 24th July, 2006 UPDATE: I see from the output 2 getlines are needed.

                                    Z Offline
                                    Z Offline
                                    Zac Howland
                                    wrote on last edited by
                                    #17

                                    Why are you surpised that would work? That is pretty much what one of the examples I posted does. The reason it works is that the >> operator for cin will read until it hits whitespace, but not read the whitespace. getline reads until it gets to the number of characters you specificed, or the character you requested and does read it in. You really should look into the use case for "skipping" inputs in a console application. I can't think of any time where I would want the user to be able to hit enter and skip entering data. Now, entering a specific value to state they want to skip it, that is a different story ...

                                    If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                                    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