|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Probably going to shoot myself soon
Very easy problem..I want the user to type in a full name (First and last) and press enter. Then the user types in a time to be used in a formula. The fullname and the end result of the formula go into a text file. I am using getline() to get the fullname but for some damn reason it skips "name" and goes right to "time." I used cin.ignore(80, '\n'); but then it seems to lose the value of
"name." I am at a loss here and wish not to use character arrays. Is there another method/function/solution to this problem? Any help would be more then appreciated. Here is my code: #include <iostream> #include <fstream> #include <string> #include <math.h> using namespace std; void main() { ofstream files; string names; string name; float t; float a=2; float b=2; float c; cout << "file?\n"; cin >> name; cout << "\nName?\n"; getline(cin, names); cout << "Time?\n"; cin >> t; c = a + b + t; files.open((name + ".txt").c_str(), ios::app); files << "\n" << names; files << "\t" << c; files.close(); }
__________________
hmmm... |
|
#2
|
||||
|
||||
|
Just read that there is "no getline() function for output file variables, but there is putline()"
I can't find any useful information on the putline() function..anyone have any ideas? -andy |
|
#3
|
||||
|
||||
|
There is a bug with getline in the string file. Take a look:
FIX: getline Template Function Reads Extra Character http://support.microsoft.com/defaul...B;EN-US;q240015 This works for me (but not when you uncomment the commented line): Code:
#include <iostream>
#include <fstream>
#include <string>
#include <math.h>
using namespace std;
int main()
{
ofstream files;
string names; // name
string name; // file name
string cr;
float t; // time
float a=2;
float b=2;
float c;
cout << "file?\n";
//cin >> name;
getline(cin,name);
cout << "\nName?\n";
getline(cin,names);
cout << "Time?\n";
cin >> t;
cout << "name = " << name << endl;
cout << "names = " << names << endl;
cout << "t = " << t << endl;
return(0);
c = a + b + t;
files.open((name + ".txt").c_str(), ios::app);
files << "\n" << names;
files << "\t" << c;
files.close();
}
__________________
Jason Doucette / Xona.com™ - Programming Windows Errata Addendum "Discussion is an exchange of knowledge; argument is an exchange of ignorance." Last edited by Jason Doucette : May 16th, 2003 at 05:20 PM. |
|
#4
|
||||
|
||||
|
The code that works for you doesn't open a file and append.
fixed that string bug about a month ago. Thanks though, any other ideas? EDIT:: ONE SEC..I MOVED YOUR RETURN 0; TO THE BOTTOM AND I THINK IT MIGHT WORK FOR MY PURPOSE..ONE SEC..ILL UPDATE. Last edited by andy3109 : May 16th, 2003 at 05:25 PM. |
|
#5
|
||||
|
||||
|
Nm man..i figured it out..it was a weird solution but you pointed me in the right direction. Thanks!
-andy Last edited by andy3109 : May 16th, 2003 at 05:51 PM. |
|
#6
|
|||
|
|||
|
cin >> name;
cout << "\nName?\n"; getline(cin, names); It doesn't have anything to do with a bug--it's the result of the way the >> operator works. Whenever you use the >> operator and subsequently use getline(), you will run into that problem. That's because the >> operator stops reading when it encounters whitespace like a '\n', and very importantly, it leaves the '\n' in the input stream. getline(), on the other hand, reads up to and including the delimiter, and since the default delimiter for getline() is '\n', it reads in the '\n' that was left in the input stream by the >> operator and stops, making it seem like the getline() statement was skipped. The non-weird solution is to remove the '\n' from the input stream after using the >> operator: cin >> name; cin.ignore(); cout << "\nName?\n"; getline(cin, names); Why don't you have that problem when you do something like this: cin>>name; cin>>another_name; Because the >> operator also ignores all leading whitespace, so the '\n' left in the stream by the first cin statement is skipped by the second cin statement, unlike with getline(). Last edited by 7stud : May 17th, 2003 at 08:18 PM. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Probably going to shoot myself soon |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|