Thread: file problem

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715

    file problem

    I have this problem:
    Code:
    #include <string>
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	string s;
    	cout<<"Enter file name: ";
    	getline(cin,s);
    	ofstream File(s.c_str());
    	if(!File)
    	{
    		cout<<"Error openning file!"<<endl;
    		return 1;
    	}
    	for(;;)
    	{
    		cout<<"Enter new sentence: ";
    		getline(cin,s);
    		File<<s;
    		File<<'\n';
    		cout<<"Enter 0 for exit or something else to continue: ";
    		getline(cin,s);
    		if(s=="0")
    			break;
    		cin.clear();
    		cin.ignore(1000);
    	}
    	return 0;
    }
    but his won't stop. How can I fix it?

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    One possible solution is to call good old C for help
    This code is working fine:
    Code:
    #include <string>
    #include <fstream>
    #include <iostream>
    #include <cstdio>
    using namespace std;
    
    int main()
    {
    	string s;
    	cout<<"Enter file name: ";
    	getline(cin,s);
    	ofstream File(s.c_str());
    	if(!File)
    	{
    		cout<<"Error openning file!"<<endl;
    		return 1;
    	}
    	int end;
    	for(;;)
    	{
    		cout<<"Enter new sentence: ";
    		getline(cin,s);
    		File<<s;
    		File<<'\n';
    		printf("Enter 0 to exit: ");
    		scanf("%d",&end);
    		if(!end)
    			break;
    		fflush(stdin);
    		
    	}
    	return 0;
    }
    Until now I've always used cstdio when dealing with files , but today I decided to learn how C++ handles files and at the very first begining I get problem.

    Thanks

  3. #3
    Registered User dalek's Avatar
    Join Date
    May 2003
    Posts
    135

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    instead of a for loop with terminal sequence creating an endless loop you could try a while loop like this:
    Code:
    while(s != "0")
    {
      cout<<"Enter new sentence.  Enter 0 to exit: ";
      getline(cin,s);
      File<<s;
      File<<'\n';
    }

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Your original code works fine if you put "0" the first time through the loop. If you don't, then the cin.ignore(1000) is screwing you up. I don't see any reason to ignore anything there, because you will have gotten everything in the input stream into the string s already.

    If you are trying to let the user hit "enter" again before asking for a sentence again, then use:

    cin.ignore(1000, '\n');

    or

    cin.ignore(std::numeric_limits<int>::max(), '\n');

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Rename file problem
    By Emporio in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 09:36 AM