i have this given structure:

struct Tempscale
{
	double farenheit; 
	double centigrade;
};

struct Reading
{
	int windSpeed;
	double humidity;
	Tempscale temperature;
};

i am then asked:
define a Reading structure variable
is this ok: Reading data;
write statements that will display following data:
wind speed : 37mph
humidity: 32%
farenheit temperature: 32 degrees
centigrade temperature: 0 degrees

my question:
can i use a declaration and cout statement for the data?
ex:

data.windspeed = 37;
data.humidity = 32;

cout << data.windspeed << "mph" << endl;
cout << data.humidity<< "%" << endl;

i think this is the correct way to write the statement for the first 2, but how do i write a statement that will store the data for the farenehit and centigrade? (since temperature is a TempScale structure variable)

the second part of the question asks me to write a function called findReading that should use Reading structure variable as its parameter. the function should then ask user to enter values for each member of the structure

Dani AI

Generated

Quick clarifications for (and thanks to for the pointer): declaring an instance of the Reading type is fine. Simple members like wind speed and humidity are assigned directly from that instance. The temperature fields live inside the nested TempScale member, so reach them through the outer variable and then the inner member (use dot when you have the object, arrow when you have a pointer).

A small practical note: the struct in the thread spells farenheit—the common spelling is fahrenheit. Either keep the name as written (so existing code still compiles) or rename consistently everywhere. To avoid storing redundant input, one common approach is to ask for Fahrenheit and compute Celsius (centigrade) with the standard formula:
C = (F - 32.0) * 5.0/9.0
(or compute F from C if you prefer to ask for Celsius).

Example of a safe input routine (uses a pointer to avoid repeating the exact examples already posted):

void findReading(Reading *dest)
{
    using std::cout; using std::cin;
    cout << "Wind speed (integer): ";
    while(!(cin >> dest->windSpeed)) { cin.clear(); cin.ignore(10000,'\n'); cout << "Please enter an integer: "; }

    cout << "Humidity (percent): ";
    cin >> dest->humidity;

    cout << "Temperature in Fahrenheit: ";
    cin >> dest->temperature.farenheit;
    dest->temperature.centigrade = (dest->temperature.farenheit - 32.0) * 5.0/9.0;
}

A short checklist: validate numeric input (as shown), keep member names consistent if renaming, and use std::fixed/std::setprecision when printing temperatures to control decimals. This covers both storing and displaying the four requested lines without duplicating the earlier snippets.

Recommended Answers

All 3 Replies

>>but how do i write a statement that will store the data for the farenehit and centigrade?

Here is how to access farenheight -- you should be able to figure out the other yourself. data.temperature.farenheit = 32; As for the second part -- do you know how to write a function? And how to use cin and cout ?

im iffy on using functions and structures together-so to answer ur question: not really. no :(

All it wants you to do is create a function named findReading with one parameter, which is a reference to a Reading structure.

void findReading( Reading & rd)
{
   // put your code here.  use cout to display prompts and cin to get keyboard input.
}


// here is how to call the above function
int main()
{
    Reading rd;
    findReading(rd);

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.