Outputting an Array, what's wrong...?

This is a discussion on Outputting an Array, what's wrong...? within the C++ Programming forums, part of the General Programming Boards category; Ok this code outputs a name, but i cant figure out how to output the array in my function, from ...

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

    Exclamation Outputting an Array, what's wrong...?

    Ok this code outputs a name, but i cant figure out how to output the array in my function, from the main( ).

    I know the code in my getInfo function works, and if i output the information inside the function it works, however how do i output it from inside the main( ).

    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 getInfo(ifstream& fin, ofstream& fout, int marks[], char name[])
    {
    		fin.getline(name, 100, '\n');
    		for(int j=0; j<12; j++)
    		{
    			fin >> marks[j];
    		}
    }
    
    
    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);
    
    
    		getInfo(fin, fout, marks, name);
    
    		fout << name << endl;
    		fout << marks << endl;
    	
    
    	fout.close();
    	fin.close();
    }
    code in blue is where i try to output the marks[j], tried a few things to no avail.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You need a loop to output marks. ( same as for input ).
    Code:
    for(int j=0; j<12; j++)
    	fout << marks[j] << endl;
    K

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    > void main()
    Read the FAQ - main returns int

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

    Thanks, one more question :)

    Cheers, that works fine now, however i try to read in the data until end of file, and it doesnt work, just runs continuously... code in blue is what i added to try input data until end of file:

    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 getInfo(ifstream& fin, int marks[], char name[])
    {
    	while(!fin.eof())
    	{
    		fin.getline(name, 100, '\n');
    		for(int j=0; j<12; j++)
    		{
    			fin >> marks[j];
    		}
    	}
    }
    
    
    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);
    
    
    		getInfo(fin, marks, name);
    
    		fout << name << endl;
    		
    		for(int j=0; j<12; j++)
    			fout << marks[j] << " ";
    	
    
    	fout.close();
    	fin.close();
    }

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Don't use eof() to control the loop. Check the return value of the input method (whether it be getline or >> or both). When it goes in an infinite loop, it is usually because an error occurred. eof() never returns true if an error occurs.

    So use the getline call as the control of the while loop: while(fin.getline(name, 100, '\n')). To be extra safe, check the return value when you are reading in the marks: if (!(fin >> marks[j])) // indicate an error.

    BTW, you really should be using the C++ string class like you tried before. If you aren't going to use it, then you don't need to #include<string>.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    > void main()
    Strrrrrike 2!

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Hey, Salem! Ever heard of Michael Kaplan?
    Strike One
    Strike Two
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is wrong with this 2d array?
    By FoxeySoulSLayer in forum C++ Programming
    Replies: 9
    Last Post: 04-10-2009, 04:14 PM
  2. What's wrong with my Average Array program?
    By special1zed in forum C Programming
    Replies: 12
    Last Post: 03-15-2009, 11:01 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  5. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM

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