4 Replies - 651 Views - Last Post: 26 March 2012 - 07:11 PM Rate Topic: -----

#1 Shalzaon  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 21-January 09

Problem reading strings from input file and printing to output

Posted 26 March 2012 - 03:40 PM

So this is a project i am working on for my C++ class and the idea is to Read an input file CarbonFP.txt which provides partial carbon foot print data for US cities. In which i must assign each city a fine based on their average carbon foot print value. Than i have to print the results to an out file.

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
	system("Color 4E");
	int value = 0;
	int sum = 0;
	int count = 0;
	double fines = 0;

	cout << fixed<<showpoint <<setprecision(2);
	

	string fname;
	cout << "Enter input file name: ";
	getline(cin, fname);
	ifstream in;
	in.open(fname.c_str());

	if(!in)
	{
		cout << "Failed to open input file: \n";
		return 2;
	}

	string fname2;
	cout << "Enter output file name: ";
	getline(cin, fname2);
	ofstream out;
	out.open(fname2.c_str());
	string city;
	double average = 0.0;
	int rounded = 0;

	if(!out)
	{
		cout << "Output file could not be created. Results will be printed to console"<<endl;
		cout << "********************************************************************** \n";
		cout << "City \t\t Rounded Average Carbon FP \t Fines <$> \n"; 
		cout << "********************************************************************** \n";
		in>>city;
		in>>value;

		while(in)
		{
			if(value>0)
			{
			count++;
			sum = sum + value; 
			}
				in>>value;

			if(count != 0)
			{
				average = double(sum)/count;
				rounded = int(average + 0.5);

				if(rounded <= 1)
				{
						fines = 0.0;
				}
				else if(rounded > 1 || rounded <=3)
				{
						fines = 1000000.0;	
				}
				else if(rounded > 3 || rounded <= 5)
				{
						fines = 2000000.0;
				}
				else if(rounded > 5 || rounded <= 7)
				{
						fines = 3000000.0;
				}
				 else if(rounded > 7)
				{
					fines = 4500000;
				}
				else 
				{
					cout<<"No value avalible for carbon FP. Fine not collected: \n";
				}

				cout<<city<<"\t"<<rounded<<"\t\t\t\t"<<fines<<endl;
			}
		}
	}
			
	else
			{
				in.close();
				in.open(fname.c_str());

				in>>city;
				in>>value;

		while(in)
		{
			if(value>0)
			{
			count++;
			sum = sum + value;
			}
			in>>value; 
		

			if(count != 0)
			{
				average = double(sum)/count;
				rounded = int(average + 0.5);

				if(rounded <= 1)
				{
						fines = 0.0;
				}
				else if(rounded > 1 || rounded <=3)
				{
						fines = 1000000.0;	
				}
				else if(rounded > 3 || rounded <= 5)
				{
						fines = 2000000.0;
				}
				else if(rounded > 5 || rounded <= 7)
				{
						fines = 3000000.0;
				}
				 else if(rounded > 7)
				{
					fines = 4500000.;
				}
				else 
				{
					cout<<"No value avalible for carbon FP. Fine not collected: \n";
				}
				cout << city << "\t"<<rounded<<"\t\t\t\t"<<fines<<endl;
			}
			}

		}

	in.close();
	out.close();
	return 0;
}

/* results if no output file is present

Enter input file name: C:\Users\God\Documents\Visual Studio 2010\Projects\Carbon
\CarbonFP.txt
Enter output file name:
Output file could not be created. Results will be printed to console
**********************************************************************
City             Rounded Average Carbon FP       Fines <$>
**********************************************************************
Amarillo        3                               1000000.00
Amarillo        3                               1000000.00
Amarillo        2                               1000000.00
Amarillo        2                               1000000.00
Amarillo        3                               1000000.00
Amarillo        3                               1000000.00
Press any key to continue . . .
*/

/*Results if output file is dragged and dropped into console

Enter input file name: C:\Users\God\Documents\Visual Studio 2010\Projects\Carbon
\CarbonFP.txt
Enter output file name: C:\Users\God\Documents\Visual Studio 2010\Projects\Carbo
n\out.txt
Amarillo        3                               1000000.00
Amarillo        3                               1000000.00
Amarillo        2                               1000000.00
Amarillo        2                               1000000.00
Amarillo        3                               1000000.00
Amarillo        3                               1000000.00
Press any key to continue . . .
*/

/* input file

Amarillo 3 2 1 3 5 -7
Rochester 5 6 7 4 6 -2
Albuquerque 3 4 5 2 3 -9
Durham 9 8 7 9 9 8 -4
Boise 3 4 6 2 8 9 -1
Jacksonville 2 5 2 1 5 7 2 1 1 -3
Lexington 8 9 12 3 4 10 11 -6
Tulsa 8 2 9 5 6 11 8 4 2 9 -1
San_Francisco 1 1 2 1 1 0 0 1 -8
Washington -3
*/



As you can see in the commented out section of my output, the only city name to display is Amarillo, and alliteratively my results are not being printed to my out file. i have tried to various ways to receive the input for my "string city" from setting its value to an empty string, reviving its input from various locations in the while loop, but i cant figure out my problem.... any help would be tremendously appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem reading strings from input file and printing to output

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4100
  • View blog
  • Posts: 12,695
  • Joined: 25-December 09

Re: Problem reading strings from input file and printing to output

Posted 26 March 2012 - 04:20 PM

Please post a small sample of your input file.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Shalzaon  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 21-January 09

Re: Problem reading strings from input file and printing to output

Posted 26 March 2012 - 05:15 PM

The sample of the input is commented out at the very bottom of my code but ill post it again

Amarillo 3 2 1 3 5 -7
Rochester 5 6 7 4 6 -2
Albuquerque 3 4 5 2 3 -9
Durham 9 8 7 9 9 8 -4
Boise 3 4 6 2 8 9 -1
Jacksonville 2 5 2 1 5 7 2 1 1 -3
Lexington 8 9 12 3 4 10 11 -6
Tulsa 8 2 9 5 6 11 8 4 2 9 -1
San_Francisco 1 1 2 1 1 0 0 1 -8
Washington -3
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4100
  • View blog
  • Posts: 12,695
  • Joined: 25-December 09

Re: Problem reading strings from input file and printing to output

Posted 26 March 2012 - 06:29 PM

I would suggest that you start by first reading in your input file, insuring that you are reading it correctly. Once you are sure you have your input file being processed correctly you can move on to the rest of your program. I would start by creating a structure/class to hold the data you will be reading in from your class. You may find this link helpful: C++ file input and output. It should help show how to read information from a file in C++.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Problem reading strings from input file and printing to output

Posted 26 March 2012 - 07:11 PM

Here is one of your input loops:
        in>>city;
        in>>value;

        while (in)
        {
            if (value>0)
            {
                count++;
                sum = sum + value;
            }
            in>>value;

            if (count != 0)
            {
                average = double(sum)/count;
                rounded = int(average + 0.5);

                if (rounded <= 1)
                {
                    fines = 0.0;
                }
                else if (rounded > 1 || rounded <=3)
                {
                    fines = 1000000.0;
                }
                else if (rounded > 3 || rounded <= 5)
                {
                    fines = 2000000.0;
                }
                else if (rounded > 5 || rounded <= 7)
                {
                    fines = 3000000.0;
                }
                else if (rounded > 7)
                {
                    fines = 4500000;
                }
                else
                {
                    cout<<"No value avalible for carbon FP. Fine not collected: \n";
                }

                cout<<city<<"\t"<<rounded<<"\t\t\t\t"<<fines<<endl;
            }
        }


Of course you are processing only 1 city -- you only input 1 city -- before the beginning of the loop -- and inside the loop you input the "value" data. So it processes the data for the first line of the file (that part appears correct, although I didn't study it in great detail), and when it gets to the end of the line, it tries to extract another "value" entry but instead it finds the next city name. There's no way to convert that to an int, so the extraction >> operator "fails", the stream's "fail" flag is set, you drop out of the while loop and your input processing is finished.

Long story short, if you want to input a string, followed by a series of ints (of variable length), followed by another string, followed by another variable-length series of ints, and so on ..., you should set up an outer loop that reads the strings with a nested inner loop that reads and processes the ints.

As a general rule, you have to think about how your input data is organized, and design your input code structure (usually, your loop structure) in a way that is appropriate to the format of the data.

This post has been edited by r.stiltskin: 26 March 2012 - 07:14 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1