C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 16th, 2003, 03:57 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
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...

Reply With Quote
  #2  
Old May 16th, 2003, 05:02 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
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

Reply With Quote
  #3  
Old May 16th, 2003, 05:17 PM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 6
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();
}

Last edited by Jason Doucette : May 16th, 2003 at 05:20 PM.

Reply With Quote
  #4  
Old May 16th, 2003, 05:21 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
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.

Reply With Quote
  #5  
Old May 16th, 2003, 05:34 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
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.

Reply With Quote
  #6  
Old May 17th, 2003, 03:19 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Probably going to shoot myself soon


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT