reading 3 ints from one line, then 3 from another

This is a discussion on reading 3 ints from one line, then 3 from another within the C++ Programming forums, part of the General Programming Boards category; Hi, I知 having some trouble getting a program to work. What I知 trying to do is input 3 ints from ...

  1. #1
    noobie
    Join Date
    Nov 2005
    Posts
    4

    reading 3 ints from one line, then 3 from another

    Hi, I知 having some trouble getting a program to work. What I知 trying to do is input 3 ints from a specific line in an external .cpp file.
    say I need to find out what ints x, y, and z are. the external .cpp file reads:
    "
    1 10 1900
    4 20 2000
    6 66 1666
    "
    what would I do to first read line one, making x=1, y=10, and z=1900, then after using the data, read the next line, making x=4, y=20, and z=2000?
    if I write:
    infile>>x>>y>>z;
    it will only read the last line in the file. making x=6 and so on.
    would getline work? and if so, how?
    getline can read a specific line, but can only take that line in as a string, not 3 ints.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you need to post code, because the line you posted should work ok. Did you put that line inside a loop that reads to end-of-file? If you did, that would explain why it seems to be only reading the last line, because you tossed each line upon each interation of the loop.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    if I write:
    infile>>x>>y>>z;
    it will only read the last line in the file. making x=6 and so on.
    No, all the values are read into x, y, and z, but everytime through your loop x, y, and z get overwritten by the next line of data. If you want to store all the data make x, y, and z arrays and everytime through the loop, increment the array index. That way x[0], y[0], z[0] will store the data in the first line, x[1], y[1], z[1] will store the data in the second line, etc.

    In addition, the general rule for reading from a file is: the read statement should be a while conditional, e.g.
    Code:
    while( inputFile>>data1 )
    {
    	...
    	...
    }
    inputFile>>data1 calls a function that reads data into data1 and then returns the inputFile object, which converts the while conditional to:

    while(inputFile)

    If any errors occur that will prevent the loop from reading data from the file, the inputFile object will evaluate to false in the while conditional. End of file(eof) is considered an error, so the while loop will terminate correctly when all the data is read in. However, there are other possible errors that can occur while reading from a file. If one of those errors occurs, then a loop such as:
    Code:
    while(!inputFile.eof())
    {
    
    }
    will try to keep reading data because it hasn't encountered eof yet--but the error will prevent the read statement from reading any data. So the loop will keep looping indefinitely: not able to read in any data because of the error and therefore never reaching eof.
    Last edited by 7stud; 11-11-2005 at 08:42 PM.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,068
    Also, I'd like to mention an issue of preloading your entire file into memory or loading parts through out the program. Now in anything you're writing, since it won't be big, I'd say do what 7Stud says and load it all into arrays. This makes it much quicker when running the program when it's all in memory, especially if your loading a file from a floppy disk.

    If you get into databases, though, it's more proper to be selective as to what and how much you get from your file. Rather than putting it all into memory and working with that, you could load in line at a time and do whatever you need to do with that, then repeat for the next line.
    Sent from my iPadョ

  5. #5
    noobie
    Join Date
    Nov 2005
    Posts
    4
    I may as well show you what I have so far in case you're interested...
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <APSTRING.H>
    
    void GetDate(ifstream&, int&, int&, int&);
    void print(ostream&, int, int, int, int);
    bool BadDay(int, int, int);
    int DaySwitch(int, int, int);
    bool LeapCalc(int);
    bool NotDone();
    void OpenFileName(ifstream&);
    
    int main()
    {
    	int y, m, d, caseday;
    	ofstream OutFile;
    	OutFile.open("C:OutM");
    	ifstream InFile;
    	OpenFileName(InFile);
    
    	do
    	{
    		GetDate(InFile, y, m, d);
    		if (BadDay(y, m, d))
    		{
    			cout << "You have entered an invalid date."<<endl;
    		}
    		caseday = DaySwitch(y, m, d);
    		print(cout, y, m, d, caseday);
    		print(OutFile, y, m, d, caseday);
    	}while (NotDone());
    
    	InFile.close();
    	OutFile.close();
    	return 0;
    }
    
    void OpenFileName(ifstream&InFile)
    {
    	apstring infilename;
    	cout << "Enter the input file's name: ";
    	cin >> infilename;
    	InFile.open(infilename.c_str());
    }
    
    void GetDate(ifstream&InFile, int&y, int&m, int&d)
    {
    	while(! InFile.fail())
    	{
    	InFile>>m>>d>>y;
    	}
    }
    
    int DaySwitch(int y, int m, int d)
    {
    	int dig;
    	dig=y+y/4-y/100+y/400+1;
    	if(LeapCalc(y) && (m==1 || m==2))
    		dig--;
    
    	switch(m)
    	{
    	case 1:
    	case 10:
    		break;
    	case 2:
    	case 3:
    	case 11: dig += 3;
    		break;
    	case 4:
    	case 7: dig += 6;
    		break;
    	case 5: dig += 1;
    		break;
    	case 6: dig += 4;
    		break;
    	case 8: dig += 2;
    		break;
    	case 9:
    	case 12: dig += 5;
    		break;
    	default: return 7;
    	}
    
    	dig += d - 1;
    	dig = dig%7;
    	return dig;
    }
    
    bool LeapCalc(int y)
    {
    	return (y%4==0 && y%100 !=0) || (y%400==0);
    }
    
    void print(ostream& oout, int y, int m, int d, int caseday)
    {
    	if(caseday !=7)
    	{
    		oout << m << "/" << d <<"/" << y << " is a ";
    		switch(caseday)
    		{
    
    			case 1: oout << "Sunday.";
    			break;
    			case 2: oout << "Monday.";
    			break;
    			case 3: oout << "Tuesday.";
    			break;
    			case 4: oout << "Wednesday.";
    			break;
    			case 5: oout << "Thursday.";
    			break;
    			case 6: oout << "Friday.";
    			break;
    			case 0: oout << "Saturday.";
    			break;
    		}
    	}
    	else if(caseday==7) cout << "you have entered an invalad date."<<endl;
    
    	oout << endl << endl;
    }
    
    bool BadDay(int y, int m, int d)
    {
    	if(m==2 && !LeapCalc(y))
    		return !(d>0 && d<29);
    	else if(m==2 && LeapCalc(y))
    		return !(d>0 && d<30);
    	else if(m==4 || m==6 || m==9 || m== 11)
    		return !(d>0 && d<31);
    	else return !(d>0 && d<32);
    }
    
    bool NotDone()
    {
    	char yn;
    	cout << "Another? <Y/N>: ";
    	cin>>yn;
    	cout<<endl;
    	return yn=='Y'||yn=='y';
    }
    As you can see, the user is prompted to input the name of a file that contains several dates, then the program calculates what day of the week each dates is.
    Thanks for your ideas. I'll try to use an array in this program for the various dates.
    Last edited by Tokay; 11-12-2005 at 02:17 PM.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Your else if in print() could be an else.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    #include <iostream.h> --> <iostream>
    #include <fstream.h> ---> <fstream>

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    No need to use an array here, just remove the while loop from the GetDate method. You are already calling GetDate inside a loop in main. You might want to have GetDate check the filestream state to see if it failed and return true or false so that the loop in your main function can stop when the file is empty.

  9. #9
    noobie
    Join Date
    Nov 2005
    Posts
    4
    Quote Originally Posted by 7stud
    #include <iostream.h> --> <iostream>
    #include <fstream.h> ---> <fstream>
    I can not remove the .h with my compiler.

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I can not remove the .h with my compiler.
    Then download a free modern compiler that is highly recommended on this forum:

    http://www.bloodshed.net/devcpp.html

  11. #11
    noobie
    Join Date
    Nov 2005
    Posts
    4
    thank you, Daved. my program works now and i didn't need to use an array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-20-2006, 11:17 PM
  2. Replies: 6
    Last Post: 04-28-2006, 12:06 PM
  3. reading file line by line
    By ameet in forum C Programming
    Replies: 7
    Last Post: 09-16-2004, 10:54 AM
  4. Reading a user specificed line from a file
    By hslee16 in forum C++ Programming
    Replies: 3
    Last Post: 03-13-2004, 05:58 PM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 05:04 AM

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