Thread: Input / Output help (arrays)

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

    Question Input / Output help (arrays)

    Hi, im attempting to make a program that takes a name of up to 3 words long from a text file and outputs it into a different text file.

    Then outputs numbers on a line below the name as well.

    As there can be any number of names with their 12 numbers, im finding it hard to work out.

    At the moment i can take the first name in the file and place it in an output file, but it does nothing with the numbers or other data in the text input file.

    Also the way i have done it, if the name is only 1 or 2 words, it will end up outputting the first name then 1 or 2 numbers.

    As an example the text file could contain the following:

    David Smith

    90 12 38 49 58 69 38 57 38 53 74 73

    Liam David Smith

    38 47 36 16 34 69 57 04 37 27 40 12




    Heres what i have, any clues?

    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 firstArray(ifstream& fin, int marks [], char name[], char middleName[], char surname[])
    {
    	fin >> name >> middleName >> surname;
    	 
    	int i = 0;
    	while (i < 12 && fin>>marks[i]) {
    		i++;
    	}
    }
    
    
    
    
    
    
    void main()
    {
    	ifstream fin;
    	ofstream fout;
    
    	char IFname[20], OFname[20];
    	int marks [12];
    	char name [20];
    	char middleName[20];
    	char surname[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);
    
    	firstArray(fin,marks, name, middleName, surname);
    
    	fout << name << " " << middleName << " " << surname;
    
    
    	fout.close();
    	fin.close();
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > void main()
    main returns int, see the FAQ

    > Also the way i have done it, if the name is only 1 or 2 words, it will end up outputting the first name then 1 or 2 numbers.
    Yeah, what you have done just reads 'words' which are delimited with spaces.
    Look up the 'getline' method which reads an entire line into a single variable. Then if necessary you can process that line in whatever way makes most sense.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bad output -- possible input buffer problems
    By jake123 in forum C Programming
    Replies: 8
    Last Post: 02-18-2008, 03:36 PM
  2. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 09:12 PM
  3. Using SSCANF input string output two variables
    By kevndale79 in forum C Programming
    Replies: 9
    Last Post: 10-02-2006, 02:57 PM
  4. Noob question(User input Arrays)
    By wildex999 in forum C++ Programming
    Replies: 12
    Last Post: 02-19-2006, 04:25 PM
  5. copying input to output
    By kermit in forum C Programming
    Replies: 15
    Last Post: 08-09-2003, 08:57 PM