C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 23rd, 2006, 05:47 PM
mrmattly mrmattly is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Location: Chino Valley, Arizona
Posts: 6 mrmattly User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 57 m 46 sec
Reputation Power: 0
Two Small Problems With Simple File I/O

Hello,

I'm in the process of learning C++ and am quite enthusiastic as to how well it's been coming along. I do have a little problem where I'm at, right now. Shown below is the source I'm working with:

Code:
// user defined filehandle
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
    string user_filename, user_input;
    unsigned int line_count;
    cout << "Filename to open for writing: ";
    getline(cin, user_filename);
    ofstream user_filehandle (user_filename.c_str());
    cout << "Begin typing the data you want stored into " << user_filename
         << endl << "Type EOF on a single line when finished" << endl;
    while(1) {
                     if(user_input == "EOF") break;
                     line_count++;
                     getline(cin, user_input);
                     user_filehandle << user_input << endl;
                     fflush(stdin);
    }
    user_filehandle.close();
    cout << "Wrote " << line_count << " lines to " << user_filename << endl;
    cin.get();
    return 0;
}


Problem #1
Now for some reason or other, the line_count integer data type variable is counting up to some insanely high number instead of properly tallying and accounting for the number of lines the user is writing to the filehandle.

Problem #2
I can't seem to prevent the program from writing the very last user input, "EOF" to the filehandle.

Any help would be greatly appreciated. Thanks!

Best Regards,

Matt
Mr. Mattly Networks
Visit us at http://www.mrmattly.net/

Reply With Quote
  #2  
Old March 23rd, 2006, 06:28 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
For problem #1 : The counter will give high value becuse you have put conditions like that.

To be clear: You are just askinh the user to enter a word.Hence even if he presses "Enter" and comes to next line, that will be taken as a word and so counter will be incremented .

Ex: Suppose i enter few words as shown:

" My Name " , as it is a one line , your program outputs it as 2 lines since I have not entered "EOF" . Remember , I have entered two string in a line.Your code takes "Enter" or "Return Key" as character.Therefore for each word you type, counter is updated.

For second part: What do you mean by you can't prevent input as EOf??

Last edited by Cirus : March 23rd, 2006 at 07:00 PM.

Reply With Quote
  #3  
Old March 24th, 2006, 10:19 AM
mrmattly mrmattly is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Location: Chino Valley, Arizona
Posts: 6 mrmattly User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 57 m 46 sec
Reputation Power: 0
Figured it out

Quote:
Originally Posted by Cirus
For problem #1 : The counter will give high value becuse you have put conditions like that.

To be clear: You are just askinh the user to enter a word.Hence even if he presses "Enter" and comes to next line, that will be taken as a word and so counter will be incremented .

Ex: Suppose i enter few words as shown:

" My Name " , as it is a one line , your program outputs it as 2 lines since I have not entered "EOF" . Remember , I have entered two string in a line.Your code takes "Enter" or "Return Key" as character.Therefore for each word you type, counter is updated.

For second part: What do you mean by you can't prevent input as EOf??


No, actually initializing line_count(0) fixed it. Thanks anyways!

Reply With Quote
  #4  
Old March 24th, 2006, 12:28 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
Quote:
Originally Posted by mrmattly
No, actually initializing line_count(0) fixed it. Thanks anyways!


But line_count is a variable of scalar type . How can you initialize like this???

It ain't a class.

Reply With Quote
  #5  
Old March 24th, 2006, 02:02 PM
mrmattly mrmattly is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Location: Chino Valley, Arizona
Posts: 6 mrmattly User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 57 m 46 sec
Reputation Power: 0
by doing usigned int line_count(0);

I tried it. If I left out the (0), then it gives me the insanely high number. But when I start with 0, it then counts up properly. Don't ask me how it works, but I tested it, and it works. I don't know, maybe it's a Dev-C++ thing.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Two Small Problems With Simple File I/O


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 3 hosted by Hostway
Stay green...Green IT