Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 10-23-2004, 12:18 PM   #1
Member (5 bit)
 
Join Date: Jul 2004
Posts: 24
C++ Problem

I'm writing a program to read data from a file and then store the results in variables for later calculations, and save the iresults to an outfile. The program reads in the name of a state, that states electoral votes, and the number of votes each candidate got in the 2000 election. An examlpe of the data:

State Elec Vote Gore Bush Nader Buchanan
Alabama 9 695602 944409 18349 6364
Alaska 3 79004 167398 28747 5192

Opening and reading from a file and saving and closing the data isn't a problem, but I'm having a hard time setting up the loop to read in all the data. I know the .getline function is what I should more than likely use, but i'm having problems with the syntax. Also, should a seperate loop be setup to read the first line so it doesn't store junk in the variables? Any help would greatly be welcome. Thanks.
Krisbot is offline   Reply With Quote
Old 10-31-2004, 12:55 PM   #2
Member (9 bit)
 
Join Date: Mar 2004
Posts: 313
Send a message via AIM to daveyp225
Dont use getline..Use a while loop for the inputs into a 2dim array.

Code:
#include  fstream.h    // put carrots on these  *
#include  iostream.h   // put carrots on these  *
#include  apstring.h    // put carrots on these  *

void main(){

	long int states[50][5];
	apstring state_name[50];
	int x = 0;

	ifstream infile;
	infile.open("c:\states.txt");

while ( !infile.fail()  &&  x < 50 )
{
	infile >> state_name[x]; // state name
	infile >> states[x][0]; // elec votes
	infile >> states[x][1]; // gore
	infile >> states[x][2]; // bush
	infile >> states[x][3]; // nader
	infile >> states[x][4]; // buchchanan

	x++;
}

x = 0;

while (x <= 49)
{
	cout << state_name[x] << "  ";
	cout << states[x][0] << "  ";
	cout << states[x][1] << "  ";
	cout << states[x][2] << "  ";
	cout << states[x][3] << "  ";
	cout << states[x][4] << "  " << endl;
	++x;


}

}
hey.. sorry if I did yer hw for ya, but I needed some refresh on the subject!

edit: To skip the first line of strings, just do 7 CINs..

Dave
__________________

Thermaltake Swing | 550W Corsair PSU | Asus P5K DLX/WiFi | Intel C2D E8400 @ (???)ghz | 2x2GB G.Skill DDR2-1000 | eVGA 8800GT Superclocked | 400GB Seagate 7200.11 (32mb) | 20x ASUS DVD-RW with Lightscribe | 20x Samsung DVD-RW | Black Floppy (just in case) | 52-in-1 Card Reader | UV lights & cables


Enermax CS-800TA | 400Watt Antec PSU | Asus P4c800 Deluxe | P4 2.8c @ 3.5ghz | 2x512 KHX pc4000 | ATI 9800pro AIW 128mb | 120GB 7200RPM WD Hard Drive | 8x NEC DVDRW | 16x sony dvd-rom | sony silver floppy


Last edited by daveyp225; 10-31-2004 at 01:04 PM.
daveyp225 is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:10 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2