-
June 17th, 2010, 03:08 AM
#1
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?
-
June 17th, 2010, 03:30 AM
#2
Re: Simple I/O question
You might want to remove the cin.get() - its eating your first character.
your humble savant
-
June 17th, 2010, 03:36 AM
#3
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
-
June 17th, 2010, 02:21 PM
#4
Re: Simple I/O question
Originally Posted by TheRogue
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.
-
June 17th, 2010, 02:27 PM
#5
Re: Simple I/O question
Originally Posted by VictorN
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.
-
June 17th, 2010, 04:11 PM
#6
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.
-
June 17th, 2010, 04:15 PM
#7
Re: Simple I/O question
Originally Posted by blinksumgreen
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.
-
June 17th, 2010, 08:26 PM
#8
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;
}
-
June 17th, 2010, 08:35 PM
#9
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
-
June 17th, 2010, 09:18 PM
#10
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
-
June 21st, 2010, 11:38 AM
#11
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.
-
June 21st, 2010, 11:08 PM
#12
Re: Simple I/O question
Originally Posted by kempofighter
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|