Jump to content


BIG C++ HELP!


  • Please log in to reply
7 replies to this topic

#1 BatMan-

BatMan-

    Veteran

  • Members
  • PipPipPipPip
  • 165 posts
  • Bnet Name:BatMan.
  • Bnet Realm:Azeroth

Posted 01 March 2010 - 04:24 AM

Edit: I have the code of what I did on 3rd post ( with few errors I could use help with)



Hey I am really clueless about programing and have no idea how to do this assignment so maybe if I could get some help from anyone thats knows C++ . And yes I am newb and it is due 2morrow @ like 4pm!! HELP!



You have been asked to write a program that will collect and display weather data from numerous weather data collection stations. You will provide for input of temperature, barometric pressure and wind speed for each collection station using metric values (Celcius, kPa & kph). Identify the stations only by number where 1 is the first station entered. The data entry person must be able to quit entering data at any time by typing the sentinel value ‘-999’ at the first prompt (temperature) for any station.
After the data has been entered you will process it placing the data into a table along with the converted imperial values and averages in the last row of the table (virtual example below).
After populating the table display it formatted EXACTLY as the example program.
•   As each data item is entered store it in an appropriate vector (you must NOT store the -999 ‘quit’ value)
•   After data has been entered create a 2D array (table) that is six columns wide and has enough rows to hold all data sets plus a final row for column averages.
•   Populate the table from the data vectors and calculate the appropriate conversions to imperial, placing those values next to the metric value

Here is a virtual  example of the contents of the table for 2 data sets. Note that the column/row headings are there for your convenience and are not part of the table.

#   C   F   kPa   “Merc   km/h   m/h
1   5   41   101.34   29.90   22.4   13.9
2   20   68   101.50   29.94   11.0   6.8
Avg   12.5   54.5   101.42   29.92   16.7   10.35

•   Display the table EXACTLY as in the screen shots.
•   ALL prompts must also be displayed EXACTLY as the screen shots.
For the purpose of testing you MUST write a function that is called from input if the sentinel value ‘-1000’ is entered and is hard coded to enter 5 sets of data. When this function finishes input should quit in the same way as if -999 was entered. Since the function can be called even when data has already been entered it should first clear the data vector before populating them.
Here is a list of the things that I will look for in determining your grade:
•   Design Document
•   Input from the keyboard and output to the monitor
•   Variables and constants
•   Containers (array and vector)
•   Logic for selection and iteration
•   Modularization
•   with high cohesion and
•   using both pass by value and pass by reference


Dont worry about this "display exactly like in screen shots" thingy -.-
so any hints on how to start on this?

ah sorry for Double post; I think it lagged and I mustve clicked twice

Primarily I could use help on the -999 to quit so it goes through to the tables

Edited by BatMan-, 01 March 2010 - 07:17 AM.


#2 summand

summand

    Veteran

  • Members
  • PipPipPipPip
  • 168 posts
  • Bnet Name:summand
  • Bnet Realm:none

Posted 01 March 2010 - 06:36 AM

To get the input I would put getline into a loop and then compare the entered string with "-999" and the quit reading. Something along: (Please note that the following code is untested!)

CODE

const int QUIT = -1;

string input;
while (getline(cin, input)) {
  int ret = processInputLine(input);
  if (ret = QUIT) break;
}


I would use sscanf in processInputLine to get the values and store them in a struct.

Since sscanf is not that easy to use, here a small examle:
CODE


int processInputLine(string const& input) {
  int station;
  double celsius;
  double fahrenheit;
  double t1, t2, t3, t4;


  int result = sscanf (input.c_str(), "%d %lf %lf %lf %lf %lf %lf", &station, &celsius, &fahrenheit, &t1, & t2, &t3, &t4);
  if (result != 7) {
    // Error, the user entered some crap that could not be parsed
    // Whatever you do to handle it...
  }
  if (station == -999) return QUIT;

  // Handle input data aka store them
  return station;
}


hth

#3 BatMan-

BatMan-

    Veteran

  • Members
  • PipPipPipPip
  • 165 posts
  • Bnet Name:BatMan.
  • Bnet Realm:Azeroth

Posted 01 March 2010 - 07:09 AM

HEADER

CODE

#ifndef MYHEADER_H_
#define MYHEADER_H_

#include <iostream>
#include <iomanip>
using namespace std;

float ReadValue(string type);

float Calculate(float dependantVariable, int nameOfCalc);

void SetValue(float StationAr[][6], float value, int row, int& column);

void CreateFiveStations(float StationAr[][6], int& count);



#endif /* MYHEADER_H_ */



Main
CODE

#include "myheader.h"

int main()
{

    float StationAr[20][6];
    float tempC;
    float tempF;
    float kpa;
    float mmHg;
    float kmh;
    float mph;
    float tempCAvg;
    float tempFAvg;
    float kpaAvg;
    float mmHgAvg;
    float kmhAvg;
    float mphAvg;
    int i = 0;
    int j = 0;

    tempC = ReadValue("Degrees Celsius");

    while (tempC != -999 && tempC != -1000)
    {
        j = 0;

        SetValue(StationAr, tempC, i, j);
        tempF = Calculate(tempC, 1);
        SetValue(StationAr, tempF, i, j);
        kpa = ReadValue("Kilo Pascals");
        SetValue(StationAr, kpa, i, j);
        mmHg = Calculate(kpa, 2);
        SetValue(StationAr, mmHg, i, j);
        kmh = ReadValue("Kilometers per Hour");
        SetValue(StationAr, kmh, i, j);
        mph = Calculate(kmh, 3);
        SetValue(StationAr, mph, i, j);

        tempFAvg += tempF;
        tempCAvg += tempC;
        kpaAvg += kpa;
        mmHgAvg += mmHg;
        kmhAvg += kmh;
        mphAvg += mph;


        tempC = ReadValue("Degrees Celsius");

        i++;

    }


    if (tempC == -1000)
    {
        CreateFiveStations(StationAr, i);
    }

    if (i > 0)
    {
        j = 0;

        tempFAvg /= i;
        tempCAvg /= i;
        kpaAvg /= i;
        mmHgAvg /= i;
        kmhAvg /= i;
        mphAvg /= i;

        SetValue(StationAr, tempCAvg, i, j);
        SetValue(StationAr, tempFAvg, i, j);
        SetValue(StationAr, kpaAvg, i, j);
        SetValue(StationAr, mmHgAvg, i, j);
        SetValue(StationAr, kmhAvg, i, j);
        SetValue(StationAr, mphAvg, i, j);

        int k = 0;

        cout << fixed << setprecision(2) << endl << "#" << setw(10) << "C"
        << setw(10) << "F" << setw(10) << "kpa" << setw(10) << "mmHg"
        << setw(10)    << "km/h" << setw(10) << "mp/h" << endl;

        while(k <= i)
        {
            if(k != i)
            {
                cout << k + 1;
            }
            else
            {
                cout << endl;
                cout << "Avg";
            }

            for(j = 0; j < 6; j++)
            {
                cout <<  setw(10) << StationAr[k][j];
            }
            cout << endl;

            k++;
        }
    }




return 0;
}




Functions
CODE

#include "myheader.h"

float ReadValue(string type)
{

    float value;

    cout << "What is the value for (" << type << ")";
    cin >> value;
    cin.ignore(100, '\n');

    return value;
}

float Calculate(float dependantVariable, int calcNumber)
{
    float value;

    switch(calcNumber)
    {
    case 1: value =  dependantVariable * 9/5 + 32;
    break;
    case 2: value = dependantVariable / .133;
    break;
    case 3: value = dependantVariable * 0.6215;
    break;
    default: cout << "Error with parameter";
    break;
    }

    return value;
}

void SetValue(float StationAr[][6], float value, int row, int& column)
{
    StationAr[row][column] = value;

    column++;
}

void CreateFiveStations(float StationAr[][6], int& count)
{
    int j = 0;
    int k = 0;
    float tempC;
    float tempF;
    float kpa;
    float mmHg;
    float kmh;
    float mph;
    float tempCAvg;
    float tempFAvg;
    float kpaAvg;
    float mmHgAvg;
    float kmhAvg;
    float mphAvg;

    while(k < count)
    {
        for (j = 0; j < 6; j++)
        {
            StationAr[k][j] = 0;
        }
        k++;
    }

    for(count = 0; count < 5; count++)
    {
        j = 0;

        tempC = ReadValue("temp C");
        SetValue(StationAr, tempC, count, j);
        tempF = Calculate(tempC, 1);
        SetValue(StationAr, tempF, count, j);
        kpa = ReadValue("kpa");
        SetValue(StationAr, kpa, count, j);
        mmHg = Calculate(kpa, 2);
        SetValue(StationAr, mmHg, count, j);
        kmh = ReadValue("kmh");
        SetValue(StationAr, kmh, count, j);
        mph = Calculate(kmh, 3);
        SetValue(StationAr, mph, count, j);

        tempFAvg += tempF;
        tempCAvg += tempC;
        kpaAvg += kpa;
        mmHgAvg += mmHg;
        kmhAvg += kmh;
        mphAvg += mph;
    }

    j = 0;


    tempFAvg /= count;
    tempCAvg /= count;
    kpaAvg /= count;
    mmHgAvg /= count;
    kmhAvg /= count;
    mphAvg /= count;

    SetValue(StationAr, tempCAvg, count, j);
    SetValue(StationAr, tempFAvg, count, j);
    SetValue(StationAr, kpaAvg, count, j);
    SetValue(StationAr, mmHgAvg, count, j);
    SetValue(StationAr, kmhAvg, count, j);
    SetValue(StationAr, mphAvg, count, j);

}



This is what ive gotten

I have a couple of questions and errors

(If I enter -1000 as first value then do it the avg just comes up as 00.00)
and how do I set the avg value to be 2 decimal places since always the avg for the celcius always comes off as something wierd
even if I just type 1 number like 10 for celcius the avg will be like something crazy

Edited by BatMan-, 01 March 2010 - 07:14 AM.


#4 summand

summand

    Veteran

  • Members
  • PipPipPipPip
  • 168 posts
  • Bnet Name:summand
  • Bnet Realm:none

Posted 01 March 2010 - 07:47 AM

You are declaring e.g. tempFAvg in main and in your Create Function. When you do this, you get two different variables. You fill one with data and print the other one.

Easy solution: Define the variables once outside of main and delete the other definitions:

CODE

    float tempC = 0;
    float tempF = 0;
    float kpa = 0;
   ...

int main() {
...


Its bad, but the fastest/easiest fix.

Another important thing: When you define variables, set them to 0
BAD:      float tempC;
GOOD:   float tempC = 0;

If you don't do that, they might be filled with random values.

hth

Edited by summand, 01 March 2010 - 07:48 AM.


#5 BatMan-

BatMan-

    Veteran

  • Members
  • PipPipPipPip
  • 165 posts
  • Bnet Name:BatMan.
  • Bnet Realm:Azeroth

Posted 01 March 2010 - 08:03 AM

sorry didnt understand what u mean; where should I define them and where should I take them out of (both places where I defined it? because that way the function wont work? )

ok I fixed the crazy avg from celcius by setting each variable to = 0; like u said but the avg if I type -1000 as first number still comes out 0

#6 summand

summand

    Veteran

  • Members
  • PipPipPipPip
  • 168 posts
  • Bnet Name:summand
  • Bnet Realm:none

Posted 01 March 2010 - 10:42 AM

What I forgot/overlooked was that you have to put main and your CreateFiveStations into the same file for the solution I told you to work.

A variable exists always in the whole enclosing scope and just there. A scope is an area surrounded by brackets {}

Now, when you define a variable twice in different scopes (here once in main and once in your CreateFiveStations function, you create different variables that just happen to have the same name.

What you have to do is to use always the same variable. So, delete the lines like float tempC = 0; and put them before main. That way you declare the variables in the scope of the whole file, enclosing main and your function.

And, as mentioned above, move the CreateFiveStations Function into the same file.

CODE

float tempC = 0;
float tempF = 0;
float kpa = 0;
...

int main() {
...
}

void CreateFiveStations() {
...
}



#7 BatMan-

BatMan-

    Veteran

  • Members
  • PipPipPipPip
  • 165 posts
  • Bnet Name:BatMan.
  • Bnet Realm:Azeroth

Posted 01 March 2010 - 11:04 AM

ok I deleted variables and moved them all above the main function so it includes everything and no longer does the avg for -1000 if entered first comes 0. It still comes false I believe my calculations might be incorrect but I cant seem to find the mistake can you try to look?

And what do mean by move the createfivestation function into same fule?

I look closely at the avg for when entered -1000 and it divides by 5 (since its 5 sets) one more time than it should like the avg should be 5 but its 1; avg should be 7 but its 1.40 etc

#8 summand

summand

    Veteran

  • Members
  • PipPipPipPip
  • 168 posts
  • Bnet Name:summand
  • Bnet Realm:none

Posted 02 March 2010 - 03:24 AM

Please post the whole code again. Also describe what you get and what you expect/want.

As I said, if you declare a variable, it exists in the file (and all files that include it). Now, when you move your variables outside of main, they exists in every function that comes afterwards. Since you need those variables in CreateFiveStations, you need to put that function into the same file.

Not sure what you describe with your last sentence.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users