7 Replies - 1086 Views - Last Post: 06 November 2009 - 09:11 AM Rate Topic: -----

#1 sandbanana   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-November 09

fstream and data reading issues

Posted 05 November 2009 - 11:29 PM

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int count=0;


void main ()

{
	double totM,	//total male population
		totF,		//total female population
		old_m=0,	//total males over 70
		old_f=0,	//total females over 70
		tot,		//total population
		tot_m,		//total males
		tot_f,		//total female
		i_s_m,		//input status male
		i_s_f,		//input staus female
		age=0,		//person' age
		x=0,y=0,i=0;		//counters
		
	char gender,	//gender
		 m='m',		//male
		 f='f',		//female
		 ch;

	string str;

	ofstream fout;
	ifstream fin;

	fin.open("town_data.txt", ios::in);
	fout.open("town_out.txt", ios::out);

	if(!fin){
	  cout << "Cannot open file.";
	  exit (1);
   }

	cout<<"This program will print the population percentage of males and females"
	<<"over 70 and the total number of people to the file town_data.txt.";
	fout<<"This program will print the population percentage of males and females"
	<<" over 70 and the total number of people to the file town_data.txt."<<endl<<endl;

	
	while(fin){
		 
	  
	  if((gender=m)&&(age>=70))
	  {
		  x++;
		  old_m=x;
		  fout<<old_m<<endl;
	  }
	  else if((gender=f)&&(age>=70))
	  {
		  y++;
		  old_f=y;
		  fout<<old_f<<endl;
	  }

   }
	

	
	fin.close();
	fout.close();

	cin>>ch;

}




Alright. I have been hacking this apart for the last few hours and heres what I have ended up with so far.
I am taking this list of data (sorry if this doesn't line up properly):

f 70
m 48
m 17
f 26
m 73
f 52
m 76
f 37

where 'f' is for female and 'm' is for male. The second column is the age. I want to b able to take the percentage of males older than 70 and the percentage of females older than 70 and the total percentage older than 70. I only know of a few loops and every one that I try seems to get hung up on the first line-f 70- and then exists after each line of data is processed. I used to have a while loop counting the lines of data but it was taken out. I realize I have a bunch of extra variables. Those will be needed later, my one roadblock is getting the number of males, females and everyone older than 70 into its own variable.

Is This A Good Question/Topic? 0
  • +

Replies To: fstream and data reading issues

#2 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: fstream and data reading issues

Posted 06 November 2009 - 12:15 AM

Could you tell me the line numbers of your code that have the effect of actually reading from the file?
Was This Post Helpful? 0
  • +
  • -

#3 sandbanana   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-November 09

Re: fstream and data reading issues

Posted 06 November 2009 - 12:29 AM

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

void main ()

{
	double totM,	//total male population
		totF,		//total female population
		old_m=0,	//total males over 70
		old_f=0,	//total females over 70
		tot,		//total population
		tot_m,		//total males
		tot_f,		//total female
		i_s_m,		//input status male
		i_s_f,		//input staus female
		age=0,		//person' age
		x=0,y=0,i=0;		//counters
		
	char gender,	//gender
		 m='m',		//male
		 f='f',		//female
		 ch;

	string str;

	ofstream fout;
	ifstream fin;

	fin.open("town_data.txt", ios::in);
	fout.open("town_out.txt", ios::out);

	if(!fin){
	  cout << "Cannot open file.";
	  exit (1);
   }

	int count;
{	
	ifstream fin("town_data.txt");
	string line;
	vector<string> lines;
	while(getline(fin, line))lines.push_back(line);

	count=lines.size();
}

	

	cout<<"This program will print the population percentage of males and females"
	<<"over 70 and the total number of people to the file town_data.txt.";
	fout<<"This program will print the population percentage of males and females"
	<<" over 70 and the total number of people to the file town_data.txt."<<endl<<endl;

	fin>>gender>>age;
	
	while(i<=count)
	{
  while(gender==m||gender==f)
	  { 
		if(age>=70&&gender==m)
		
			old_m++;
					
		else 	  
			old_m--;
			
		if(age>=70&&gender==f)
	  
			  old_f++;
		 
		else 

			  old_f--;
		 		  
		i++;
  
  fout<<"There are "<<old_m<<" males older than 70."<<endl;
  fout<<"There are "<<old_f<<" females older than 70."<<endl;
	  }
	}
	
	
	fin.close();
	fout.close();

	cin>>ch;


}







Is what my code currently reads as. I think all of it reads? My output reads this


This program will print the population percentage of males and females over 70 and the total number of people to the file town_data.txt.

There are -1 males older than 70.
There are 1 females older than 70.
There are -2 males older than 70.
There are 2 females older than 70.
There are -3 males older than 70.
There are 3 females older than 70.
There are -4 males older than 70.
There are 4 females older than 70.
There are -5 males older than 70.
There are 5 females older than 70.
There are -6 males older than 70.
There are 6 females older than 70.
There are -7 males older than 70.
There are 7 females older than 70.
There are -8 males older than 70.
There are 8 females older than 70.
There are -9 males older than 70.
There are 9 females older than 70.
There are -10 males older than 70.
There are 10 females older than 70.

and it repeats forever.
Was This Post Helpful? 0
  • +
  • -

#4 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: fstream and data reading issues

Posted 06 November 2009 - 01:18 AM

The first thing you need to do is read your code.

But your code is a mess in terms of formatting (indenting really). I.E. it is hard to read. Get a hold of a program called UniversalIndentGui. Use that to reformat your code. Repost your properly formattted code.
Was This Post Helpful? 0
  • +
  • -

#5 sandbanana   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-November 09

Re: fstream and data reading issues

Posted 06 November 2009 - 02:03 AM

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

void main ()

{
	double totM,								  //total male population
		totF,									 //total female population
		old_m=0,								  //total males over 70
		old_f=0,								  //total females over 70
		tot,									  //total population
		tot_m,									//total males
		tot_f,									//total female
		i_s_m,									//input status male
		i_s_f,									//input staus female
		age=0,									//person' age
		i=1;									  //counters

	char gender,								  //gender
		m='m',									//male
		f='f',									//female
		ch;
	int ct;

	string str;

	ofstream fout;
	ifstream fin;

	fin.open("town_data.txt", ios::in);
	fout.open("town_out.txt", ios::out);

	if(!fin)
	{
		cout << "Cannot open file.";
		exit (1);
	}

	int count;
	{
		ifstream fin("town_data.txt");
		string line;
		vector<string> lines;
		while(getline(fin, line))lines.push_back(line);

		count=lines.size();
	}

	cout<<"This program will print the population percentage of males and females"
		<<"over 70 and the total number of people to the file town_data.txt.";
	fout<<"This program will print the population percentage of males and females"
		<<" over 70 and the total number of people to the file town_data.txt."<<endl<<endl;

	fin>>gender>>age;

	for(gender==m||gender==f;i<=count;i++)
	{
		if((age>=70)&&(gender=m))

			old_m++;

		else if(gender=f)
			old_f++;
	}

	fout<<"There are "<<old_m<<" males older than 70."<<endl;
	fout<<"There are "<<old_f<<" females older than 70."<<endl;

	fin.close();
	fout.close();

	cin>>ch;

}




Outputs to:

This program will print the population percentage of males and females over 70 and the total number of people to the file town_data.txt.

There are 8 males older than 70.
There are 0 females older than 70.
Was This Post Helpful? 0
  • +
  • -

#6 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: fstream and data reading issues

Posted 06 November 2009 - 07:09 AM

Wow, the code is so much readable, eh? In general, try and keep your code neatly formatted, but when it becomes a mess, use programs like UIG to fix it.

Do you recall that my first post asked about the lines where you actually read from the file? I was trying to get you to look at your code and check your logic. Look at a portion later in your own code.

	fin>>gender>>age;

	for(gender==m||gender==f;i<=count;i++)
	{
		if((age>=70)&&(gender=m))

			old_m++;

		else if(gender=f)
			old_f++;
	}



How many lines here are involved with reading from the file? The first one only. But notice how you have a loop that repeatedly increments old_m and old_f? How are age and gender changing when you don't read from the file?

This is what I'm trying to get you to do. WHen you have a bug in your program you need to do quite a few things to figure out hte problem. One of those things might be to actually read your own program and trace out the logic.
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: fstream and data reading issues

Posted 06 November 2009 - 07:52 AM

void main ()
is wrong, as this currently active topic elucidates.
Was This Post Helpful? 0
  • +
  • -

#8 sandbanana   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-November 09

Re: fstream and data reading issues

Posted 06 November 2009 - 09:11 AM

Holy Crap. Thanks.

All I did was move that line. Before I was changing everything in the loop around like crazy but not moving the portion to read in from the input file.

					
	for(gender==m||gender==f;i<=count;i++)
		{

		fin>>gender>>age;

		if((age>=70)&&(gender==m))
			
			old_m++;
					
		else if((gender==f)&&(age>=70))
			old_f++;
		}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1