CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Oct 2008
    Location
    Indiana
    Posts
    56

    Simple I/O question

    Code:
    while(true){
    string input = "";
    cin.get();
    getline(cin, input, '\n');
    cout << "NAME: " << input << endl;
    }
    For some reason the code above cuts off the first character entered.

    Example:

    (Input)

    Bill
    Tim
    John

    (Output)

    ill
    Tim
    John

    Could someone explain to me why this is and how I may go about fixing it?

  2. #2
    Join Date
    Mar 2002
    Location
    Kent, United Kingdom
    Posts
    399

    Re: Simple I/O question

    You might want to remove the cin.get() - its eating your first character.
    your humble savant

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Simple I/O question

    Your "output" does not correspond your code:
    Code:
    cout << "NAME: " << input << endl;
    Please, show your actual code (it would be great if you used indentations) and actual iputs and outputs.
    Victor Nijegorodov

  4. #4
    Join Date
    Oct 2008
    Location
    Indiana
    Posts
    56

    Re: Simple I/O question

    Quote Originally Posted by TheRogue View Post
    You might want to remove the cin.get() - its eating your first character.
    Below is the output I get with the cin.get() in there, which is exactly what I want, with the exception of not having the first letter.

    Input employee name
    Bill
    Input hours employee worked
    45
    Input employee pay rate
    22
    Input employee name
    Jim
    Input hours employee worked
    33
    Input employee pay rate
    47

    EMPLOYEE----PAY
    NAME-----------RATE----HOURS
    ill__________22.00____45
    Jim_________47.00____33


    This is what happens if I don't have the cin.get() call in the code:

    Input employee name
    Bill
    Input hours employee worked
    45
    Input employee pay rate
    55
    Input employee name
    Input hours employee worked
    23
    Input employee pay rate
    44
    Input employee name
    Input hours employee worked

    First time around, everything is fine. But for some reason when it gets back to asking for another name it seems to just skip over needing an input, and immediately prompts the user for the hours employee worked.

  5. #5
    Join Date
    Oct 2008
    Location
    Indiana
    Posts
    56

    Re: Simple I/O question

    Quote Originally Posted by VictorN View Post
    Your "output" does not correspond your code:Please, show your actual code (it would be great if you used indentations) and actual iputs and outputs.
    That's pretty much the code for it.

  6. #6
    Join Date
    Aug 2007
    Posts
    858

    Re: Simple I/O question

    First time around, everything is fine. But for some reason when it gets back to asking for another name it seems to just skip over needing an input, and immediately prompts the user for the hours employee worked.
    Post your actual code. Without it all we can do it make guesses about what might be happening.

  7. #7
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: Simple I/O question

    Quote Originally Posted by blinksumgreen View Post
    That's pretty much the code for it.

    No it isn't. I can tell by the previous post that it isn't even close. Where is the input for all of the other input stream operations? The problem probably has to do with mixing cin for integrals and getline for the string. This is a very common problem. See the example at the following link.
    http://www.parashift.com/c++-faq-lit....html#faq-15.6

    If that isn't the problem then you need to post the entire program.

  8. #8
    Join Date
    Oct 2008
    Location
    Indiana
    Posts
    56

    Re: Simple I/O question

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    void inputEmployeeInfo(string name[], float hours[], float payrate[], int & size){
    	bool k = true;
    	int i = 0;
    	
    	while(k == true){
    
    		cout << "Input employee name" << endl;
    
    		string input = "";
    		cin.get();
    		getline(cin, input, '\n');
    		//cout << "NAME: " << input << endl;
    
    		// is only 0 when they are equal
    		if(input.compare("end") == 0){
    			k = false;
    		}
    		else{
    			name[i] = input;
    
    			cout << "Input hours employee worked" << endl;
    			cin >> hours[i];
    
    			cout << "Input employee pay rate" << endl;
    			cin >> payrate[i];
    
    			i++;
    		}
    	}
    
    	size=i;
    }
    
    void outputPayReport(string name[], float hours[], float payrate[], int size){
    	cout << "EMPLOYEE         PAY" << endl;
    	cout << "NAME            RATE    HOURS" << endl;
    
    	for(int i=0; i < size; i++){
    		// for formatting decimal places
    		cout << name[i] << "	"; 
    		printf("%.2f", payrate[i]);
    		cout << "	 " << hours[i] << "	";
    		cout << endl;
    	}
    
    }
    
    int main ()
    {
    	string name [50];
    	float hours [50];
    	float payrate [50];
    
    	int size = 0;
    
    
    	inputEmployeeInfo(name, hours, payrate, size);
    
    	// at this point the size variable has the total number of employees
    
    	outputPayReport(name, hours, payrate, grosspay, netpay, size); 
    		
    
    	// this is only here so the screen doesn't close
    	int x;
    	cin >> x;
    
      return 0;
    }

  9. #9
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: Simple I/O question

    Yes so see the link that I previously posted for the correct solution. Get rid of the cin.get which is moving the stream passed the first character of input. You wouldn't want to do that in the beginning anyway. The best solution is to use the ignore member function at the bottom so that the stream is moved passed the '\n' after the last numeric input. it is better to use the generic solution in case the user fat fingers a key while entering the numeric input. Theoretically there could be other characters in the buffer after the cin.

    Also read sections 15.3 and 15.4 in order to learn how to properly check for errors when asking for numeric input.
    http://www.parashift.com/c++-faq-lite/input-output.html

  10. #10
    Join Date
    Oct 2008
    Location
    Indiana
    Posts
    56

    Re: Simple I/O question

    Thank you, and sorry for not seeing that earlier.

    Code:
    cout << "Input employee name" << endl;
    
    		string input = "";
    		//cin.get();
    		getline(cin, input, '\n');
    		std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    When I add the ignore line in it does two things.

    1. I have to hit enter twice after typing in the first name, and
    2. It won't recognize 'end' as my exit command.


    OUTPUT
    Input employee name
    Billy Boy

    Input hours employee worked
    44
    Input employee pay rate
    33
    Input employee name
    Jimmy James
    Input hours employee worked
    33
    Input employee pay rate
    58
    Input employee name
    end
    Input hours employee worked

  11. #11
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: Simple I/O question

    Do not add the cin.ignore after getline. getline already takes care of the '\n'. You need it after the input of the integral numbers and before the next use of getline.

    Code:
    cin >> intVariable; // doesn't move stream past the '\n'
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ); // now you are ready for a getline
    The other option is to use getline for all inputs and then convert the numeric strings to integers within the program.

  12. #12
    Join Date
    Oct 2008
    Location
    Indiana
    Posts
    56

    Re: Simple I/O question

    Quote Originally Posted by kempofighter View Post
    Do not add the cin.ignore after getline. getline already takes care of the '\n'. You need it after the input of the integral numbers and before the next use of getline.

    Code:
    cin >> intVariable; // doesn't move stream past the '\n'
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ); // now you are ready for a getline
    Thank you! That solved the problem!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured