include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
using namespace std;
int main(){
ifstream fin;
fin.open("monthlyPrecipitation1.txt");
ofstream fout("annualPrecipitation.txt");
string cityName;
double num1,num2,num3,num4,num5,num6,num7,num8,num9,num10,num11,num12;
int height;
double annualPrecipitation;
fout<<"Annual Precipitation"<<endl<<endl;
getline(fin,cityName,'\t');
while (fin>>cityName)
{
fin>>height>>num1>>num2>>num3>>num4>>num5>>num6>>num7>>num8>>num9>>num10>>num11>>num12;
annualPrecipitation=num1+num2+num3+num4+num5+num6+num7+num8+num9+num10+num11+num12;
fout<<cityName<<endl;
fout<<height<<endl<<num1<<endl<<num2<<endl<<num3<<endl<<num4<<endl<<num5<<endl<<num6<<endl<<num7<<endl<<num8<<endl<<num9<<endl<<num10<<endl<<num11<<endl<<num12<<endl;
fout<<annualPrecipitation;
}
fin.close();
}
The loop is to get information from a text document
The rel="nofollow">http://i52.tinypic.com/34ipdv6.png
Yes I realize that it is outputting all the numbers to the screen, that is just to check to make sure the program is gathering the correct inputs.
Lastly: Here is the data it collects for the first 3 rows
AGUA CANYON 8900 2.4 3.1 2.7 1.9 1.5 0.9 1.9 2.7 2.3 2.1 2.2 2.4
BEAVER DAMS 8000 2.9 2.8 3.4 3.4 2.2 0.9 1.1 1.7 1.9 2.4 2.7 2.7
BEAVER DIVIDE 8280 3.3 3.1 3.3 3.3 2.6 1.5 1.4 1.8 1.8 2.2 2.9 3.3
Copyright © 2024 Q2A.ES - All rights reserved.
Answers & Comments
Verified answer
I believe your code was almost there and naturally your city need to be separated from your number itineries through a tab ... :O)
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
using namespace std;
int city_main(){
ifstream fin("city.txt");
ofstream fout("annualPrecipitation.txt");
string cityName;
double num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11, num12;
int height;
double annualPrecipitation;
fout<< "Annual Precipitation" <<endl << endl;
while (!fin.eof()){
getline(fin,cityName,'\t');
if (!fin.eof()) {
fin >> height>> num1 >> num2 >>num3 >> num4 >> num5 >> num6 >> num7 >> num8 >> num9 >> num10 >> num11 >> num12;
annualPrecipitation=num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10 + num11 + num12;
fout << cityName << endl;
fout<< height << endl << fixed << setprecision(2) << annualPrecipitation;
}
}
return 0;
}
the respond is complicated and yet uncomplicated : purely positioned getchar()AFTER printf("nWould you want to calculate yet another grade (y/n)?"); it is going to artwork ! the clarification is that as quickly as u use scanf ( ) the 1st time it leaves a sparkling line character interior the memory , that's study via the whilst loop ; properly of course , a sparkling line isn't 'y' or 'Y' BTW, be cautious on each occasion utilising scanf() in C , as this function is infamous for coming up bugs ! Tip: on each occasion u have a difficulty with a loop (or any programming difficulty) and ur suspicious of enter./output, use printing statements so as that u can see what the person enters and what ur software produced then do away with those printing assertion whilst ur software works