JacieS
JacieS

Reputation: 1

Display array data in seperate data

I am a student currently made to take a module on computer programming that is supposed to be very basic. Thing is, they don't exactly teach the C++ programming language, but rather bits of it and expect us be able to piece the puzzle together on our own and build a program to hone our "problem thinking skills".

I am looking to display the data, in an array, hidden away in a separate item. In other words, the data is in an array, in a separate item known as data, while in the main program, I would like to display the data when the user keys in their selection.

For example, in the context of a monthly weather data, how can I make the information, arranged in array, to show when the user enter a certain day. So when the user keys in 21, it should display the temperature for the 21st of the month, also at the 20th index of the array.

This is what I have done so far:

printf("Enter selected month.\n"); 
scanf_s("%c", &month);
if (month==October)
{
    printf("Enter selected date, from 1st to 31st.\n"); 
    scanf_s("%d", &octoberTemperature[i]); 
    printf("%.2f\n", octoberTemperature[i]);
}

Any help is very much welcome! :)

Upvotes: 0

Views: 71

Answers (2)

VP.
VP.

Reputation: 16755

In &octoberTemperature[i] you have a problem - you are trying to write into an object of the array. You should do it like this:

printf("Enter selected month.\n"); 
scanf_s("%c", &month); 
unsigned short day = 0;

if (month==October)
{
    printf("Enter selected date, from 1st to 31st.\n"); 
    scanf_s("%d", &day);         
    printf("%.2f\n", octoberTemperature[day - 1]);
    // as it starts from 0
    // if your array containing the month report starts from 0 then it has to be (day - 1)
}

Upvotes: 1

sharan
sharan

Reputation: 226

In that code of yours u have actually inputed the day u like to find the weather for in a array and printed the same array. Now do not use the same var to get the day as input

scanf ("%d",&day)//I assumed day as a var to receive the month day cout<<Octobertemperature[day-1]; // now if u already have a array containing the data of the weather report..then this will solve your problem.

Upvotes: 0

Related Questions