HELP! What's wrong with my code!

This is a discussion on HELP! What's wrong with my code! within the C++ Programming forums, part of the General Programming Boards category; I have made this program which im really certain would work, it compiles with no errors, but runs continuously after ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    8

    Question HELP! What's wrong with my code!

    I have made this program which im really certain would work, it compiles with no errors, but runs continuously after getting the name of the output file.

    The program takes a loads of names each with 12 numbers, gets the name using getline, and puts the numbers into an array.

    Or that's what it is meant to do, what have i missed?


    Code:
    #include <iostream.h>
    #include <string>
    #include <fstream>
    using namespace std;
    
    bool getInputFilename(char fname[])						
    {
    	ifstream fin;										
    
    	cout << "Please enter the filename for input : ";
    	cin >> fname;										
    														
    	fin.open(fname, ios::nocreate);						
    														
    	if (!fin.is_open())
    		return false;									 
    	
    	fin.close();
    	return true;										
    }
    
    
    bool getOutputFilename(char fname[])					
    {
    	ofstream fout;
    
    	cout << "Please enter the filename for output : ";
    	cin >> fname;										
    														
    	fout.open(fname);									
    
    	if (fout.fail())									
    		return false;
    
    	fout.close();
    	return true;										
    }
    
    void getInput(ifstream& fin, char name[], int marks[])
    {
    	while(!fin.eof())
    	{
    		fin.get();
    		fin.getline(name, 100, '\n');
    
    		for(int j=0; j<12; j++)
    		{
    			fin >> marks[j];
    
    		}
    
    		fin.get(); 
    	}
    	
    }
    
    
    
    void main()
    {
    	ifstream fin;
    	ofstream fout;
    
    	char name[100];
    	int marks[12];
    
    	char IFname[20], OFname[20];
    
    
    	while (!getInputFilename(IFname))
    	{
    		cout << "Invalid filename try again!\n\n";
    	}
    
    	while (!getOutputFilename(OFname))
    	{
    		cout << "Invalid filename try again!\n\n";
    	}
    
    
    	fout.open(OFname);
    	fin.open(IFname);
    
    	getInput(fin, name, marks);
    
            fout << name << endl;
            fout << marks << endl;
    
    	
    	fout.close();
    	fin.close();
    }

  2. #2
    Registered User
    Join Date
    Jan 2006
    Posts
    19
    I threw it into Visual C++ and am getting a comple error that 'nocreate' is an undeclared identifier.

    I am not sure if that will help you...

    What I would try is using cin.get(); rather then using cin >> for getting the output file.

  3. #3
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    first things first, change your #include <iostream.h> to just #include <iostream>
    next, you're getting an error probably because ios::nocreate is deprecated. not used anymore... it's not included in the library <fstream>.
    check out this website, i just now found all this out by searching google: http://www.devx.com/tips/Tip/14544
    the solution to what you're trying to do is simple: try to open the file in read-only mode. if it exists, close the file, and reopen it in write mode. i just copied that info from the site... if the file doesn't exist, you will not be able to open it in read-only mode. hope that helps!
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    8

    Still not working!?

    Comiles yet continuously runs without ending, any ideas? Changed ios::nocreate like you stated.


    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    bool getInputFilename(char fname[])						
    {
    	ifstream fin;										
    
    	cout << "Please enter the filename for input : ";
    	cin >> fname;										
    														
    	fstream fs(fname, ios_base::in);
    	
    	if (!fs)
    	{
    		return false;
    	}
    	else
    	{
    		fin.close();
    		return true;
    	}
    }
    											
    
    bool getOutputFilename(char fname[])					
    {
    	ofstream fout;
    
    	cout << "Please enter the filename for output : ";
    	cin >> fname;										
    														
    	fout.open(fname);									
    
    	if (fout.fail())									
    		return false;
    
    	fout.close();
    	return true;										
    }
    
    void getInput(ifstream& fin, char name[], int marks[])
    {
    	while(!fin.eof())
    	{
    		fin.get();
    		fin.getline(name, 100, '\n');
    
    		for(int j=0; j<12; j++)
    		{
    			fin >> marks[j];
    
    		}
    
    		fin.get(); 
    	}
    	
    }
    
    
    
    void main()
    {
    	ifstream fin;
    	ofstream fout;
    
    	char name[100];
    	int marks[12];
    
    	char IFname[20], OFname[20];
    
    
    	while (!getInputFilename(IFname))
    	{
    		cout << "Invalid filename try again!\n\n";
    	}
    
    	while (!getOutputFilename(OFname))
    	{
    		cout << "Invalid filename try again!\n\n";
    	}
    
    
    	fout.open(OFname);
    	fin.open(IFname);
    
    	getInput(fin, name, marks);
    
            fout << name << endl;
            fout << marks << endl;
    
    	
    	fout.close();
    	fin.close();
    }

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Chnge void main to int main

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    And see my post in your other thread about using eof() to control the loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 09:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 05:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 01:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21