Question

I was wondering if I could get some help on the last program I need to...

I was wondering if I could get some help on the last program I need to complete for an engineering C++ class. It is a very simple program however there is one part of the problem stumping me. Here is the problem statement

. Write a C++ program that reads in from a file two month names, followed by the rainfall amounts for each month in that span of months. These rain amounts are totaled, and then an average for each month in the span of months is calculated. Because the program does not know how many months of rainfall data there is, it must read and process data values until the end of file is encountered. The data for this program can be found in the Rainfall.txt file, which is located in on Blackboard

Below is the file data. As you can see it is extremely simple however I cannot for the life of me figure out how to make the program distinguish between strings and numeric data to sort into respective arrays. I have tried 2d arrays, Two 1D arrays, the peek function, getline functions and a multitude of other approaches.

April
August
1.16 2.02 1.34 1.51 .30

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* C++ Program to read the month and amount of rainfall from a file.
Then input two month names and then the rain fall amount of these span of month is totaled
and then average is computed and the printed.*/
#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main()
   {
   string month[12];
   string startmonth,endmonth;
   double rain[12];
   double sum=0.00;
   ifstream fin;
   //open the input file
   fin.open("Rainfall.txt",ios::in);
   if(fin.fail())
       {
       cout<<"unable to open file.";
       exit(0);
       }
  
   int i=0;
   //read the month names with amount of rain fall and store in two array month[] and rain[]
   while(!fin.eof())
       {
       fin>>month[i]   ;
       fin>>rain[i];
       i++;
       }
   //user input
   cout<<endl<<"Enter start month : ";
   cin>>startmonth;
   cout<<endl<<"Enter end month : ";
   cin>>endmonth;
  
   //find index of start month
   int start=0;
   while(month[start]!=startmonth)
       start++;
  
   //find index of end month
   int end=0;
   while(month[end]!=endmonth)
       end++;
      
   int numOfMonth=0;
   //compute ths sum of rainfalls in the span of two months
   if(start<=end)
       {
       for(i=start;i<=end;i++)
           {
           sum=sum+rain[i];
           numOfMonth++;
           }
       }
   else
       {
       for(i=start;i<12;i++)
           {
           sum=sum+rain[i];
           numOfMonth++;
           }
       for(i=0;i<=end;i++)
           {
           sum=sum+rain[i];
           numOfMonth++;
           }
       }
  
   //compute average rainfall
   double average=sum/numOfMonth;
   //print
   cout<<endl<<"Total number of Month: "   <<numOfMonth;
   cout<<endl<<"Total Rain falls : "   <<sum;
   cout<<endl<<"Average Rain falls : "   <<average;
   return 0;
   }

//---------------------------------------------------------------------------------------------------------------------------------

Input File(Rainfalls.txt)

January   0.50
February   0.20
March   0.90
April   1.16
May   2.02
June   1.34  
July   1.51
August   .30
September   1.50  
October   .80
November   1.00
December   .50

----------------------------------------------------------------

output

---------------------------------------------

CATurboC+ Diski TurboC31BINIHomeworkLib Rainfall.exe Enter start monthApril nter end month August Total number of Month: 5 Total Ra

Add a comment
Know the answer?
Add Answer to:
I was wondering if I could get some help on the last program I need to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Your next programming assignment at the Research Center is to write a program that reads data...

    Your next programming assignment at the Research Center is to write a program that reads data from a file and produces a report of rainfall for that period of time. The program should read the starting month name from the data file, then read an ending month name, followed by reading in monthly rainfall amounts for each of the months from the starting month through the ending month. As it reads the monthly totals, it should sum (accumulate) the rainfall...

  • I was wondering if I could get some help with a Java Program that I am...

    I was wondering if I could get some help with a Java Program that I am currently working on for homework. When I run the program in Eclipse nothing shows up in the console can you help me out and tell me if I am missing something in my code or what's going on? My Code: public class Payroll { public static void main(String[] args) { } // TODO Auto-generated method stub private int[] employeeId = { 5658845, 4520125, 7895122,...

  • how to make this program for c++ visual studio 2015. Also, can I show your working...

    how to make this program for c++ visual studio 2015. Also, can I show your working and program. Also, Can you have notice for pseudo code? For the first problem, please implement Problem 4 on page 142 (p 143, 7E) of the text. A scan of the problem is provided below. This problem asks you to calculate the average rainfall for three months. The program should ask the user to enter the name of each month, such as June or...

  • I need help with my assignment. It is a java program. Please make it as simple...

    I need help with my assignment. It is a java program. Please make it as simple as you can. Create an ArrayList with elements the objects of previous class. Using a simple loop populate the ArrayList with data from some arrays with data, or from console. Use a loop with iterator and write the information in a File using PrintWriter. Use this address for the file (c:\\result\\MyData.txt). Use the Scanner class to display the content of the file on console.

  • I was wondering if anyone could help with finding the theoretical yield? i need to get...

    I was wondering if anyone could help with finding the theoretical yield? i need to get the Limiting Reagent to find the theoretical yield. it would be appreciated thanks! cyclohexanol MW: 110.16 cyclohexene MW: 82.14 Walol. 2. Cyclohexanol and especially cyclohexene are flammable liquids. Do not heating source in this experiment som stalo tento omos nopo of videos bos bosohoo Physical constants Cyclohexanol Cyclohexene bp (°C) 161 density (g/mL) 0.960 0.810 83 Experimental Procedure (macroscale) Add 20 mL of cyclohexanol...

  • HI, I am having trouble finsihing this program i was wondering if someone could help me....

    HI, I am having trouble finsihing this program i was wondering if someone could help me. Thanks in adavnce. Here is the code that I have so far..... //Stack.h // implementation file for the stack class #include <iostream> using namespace std; const int stack_size = 100; class stack { private: char data [stack_size]; // elements in the stack int top; // index of the top element of the stack public: stack (); // constructor creates an empty stack void push...

  • C++: Need help debugging my code I am writing this and using some other examples as reference code while rewriting it with my own understanding of the material but am having trouble making it finally...

    C++: Need help debugging my code I am writing this and using some other examples as reference code while rewriting it with my own understanding of the material but am having trouble making it finally compile. Below is a picture of the error messages. //main.cpp //Semester Project //Created by J---on 5/6/2019 #include <iostream> #include <fstream> #include <string> #include <sstream> #include <bits/stdc++.h> using namespace std; void instructions(); //displays program details and instructions void openFile(); void takeInput(int*); void switchBoard(int*); struct price {...

  • I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I...

    I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I just have to explain a lot so you understand how the program should work. In C programming, write a simple program to take a text file as input and encrypt/decrypt it by reading the text bit by bit, and swap the bits if it is specified by the first line of the text file to do so (will explain below, and please let me...

  • Need help writing beginner C# program, I will make sure to provide feedback to whoever can...

    Need help writing beginner C# program, I will make sure to provide feedback to whoever can help me figure it out! No public or global variables should be used. You need to consider passing arguments between the methods according to the ways described in the lecture. i.e. all variables should be declared inside the methods and passed to other methods by value/ref/out as needed Description: We want to design a Date class to represent a date using three integer numbers...

  • hello pls I need help and steps on how to do this, this is the 2nd...

    hello pls I need help and steps on how to do this, this is the 2nd time I'm posting it, it wasn't correct. chapter 22 accounting I find it complicated, unlike other chapters. I will highly appreciate it, secondly the steps and making sure is correct as I'm struggle with it. You recently began a job as an accounting intern at River Golf Park. Your first task was to help prepare the cash budget for April and May. ​Unfortunately, the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT