Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8624963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:39:47+00:00 2026-06-12T07:39:47+00:00

I am currently in an introductory c++ class and am working assignment that sorts

  • 0

I am currently in an introductory c++ class and am working assignment that sorts data. We recently covered structs and I decided to use structs to approach the problem rather than create 3 arrays to hold the information from our data file.

The trouble i’m having is when i’m trying to pass my struct to my function.
Here is my error:

analyze_data.cpp: In function ‘int main()’:
analyze_data.cpp:76: error: conversion from ‘weather*’ to non-scalar type ‘weather’ requested
analyze_data.cpp: In function ‘int find_pos_of_smallest(weather, int, int)’:
analyze_data.cpp:110: error: no match for ‘operator[]’ in ‘data[pos]’
analyze_data.cpp:110: error: no match for ‘operator[]’ in ‘data[pos_of_smallest]’

I don’t understand the error from line 76. I have done some research about passing structs to functions, and what i found was adding the “&” in the type declarations. However, i have no idea what it does or why i would need to do that as we haven’t covered it in class. I also did try it, but i just got a different set of errors. So i figured i’d not post those and just start from what i know.

Here is my code

  /*
    Program name: Analyze data
    Program discription:  This program will read a data file named data.txt.
      This data file is expected to be formated in a specific way and contain
      specific weather information.  The program will analyize this data and
      return max, min temperatures, 'perfect days', how many cold fronts per
      year, 10 coldest  and hottest days in a year and finally find the 5
      median days of the year.

    Date:  10/1/2012
  */

  #include <iostream>
  #include <fstream>

  using namespace std;

  /*
  * Declare struct's here
  *
  */ 
  struct weather
  {
    string date;
    int high;
    int low;
  };

  /* 
  *  Forward declaration of a function.  This declares the function,
  *  but does not define it.  (Notice that there is no code, just
  *  a function header with a semicolon after it.
  *
  */
  int find_pos_of_smallest (weather data, int start_pos, int end_pos);

  /* Our main function.
  *
  * Parameters:
  *    none
  *
  * Return value:
  *    0 if we complete successfully, 1 if there was an error.
  */
  int main()
  {
    //read the data file
    ifstream weather_data("data.txt");
    //declare array size, and then create array using struct
    int days = 365;
    weather data[days];

    //store the data.txt in the array and then close the file
    for (int i=0; i<days; i++)
    {
      weather_data >> data[i].date;
      weather_data >> data[i].high;
      weather_data >> data[i].low;
    }
    weather_data.close();

    ofstream data_results ("results.txt");
    // create the first 3 lines of the output reults in the following formated
    data_results << "Assignment #5\n"
                << "CS 1410/2000\n"
                << "Jonathan Larsen\n";
    /*
    for(int i=0; i<3; i++)
    {
      cout<<data[i].date << " "<<data[i].high << " " << data[i].low<<endl;
    }
    cout<<endl;
    */  
    cout<<find_pos_of_smallest(data, 0, days)<<endl;


    return 0;  //no error so return a zero  
  }//end of program


  /**** FUNCTIONS ****/

  /* Write down exactly what the function will do (a postcondition).
  * Write down what is required to use the function (any preconditions).
  * Write down any other behavior or comments that will help a programmer.
  *
  * Parameters:   (list parameters by type and name, and explain them)
  *   int example -- an example parameter
  *
  * Returns:
  *   double -- an example return value
  */

  /* Returns the position of the smallest value found in the specified
  * subarray.  (Only the elements in the subarray
  * between start_pos and end_pos inclusive are checked.)
  *
  * Parameters:
  *    d - a data array
  *    start_pos - the first position to check
  *    end_pos - the last position to check
  */
  int find_pos_of_smallest (weather data, int start_pos, int end_pos)
  {
    int pos_of_smallest = start_pos;

    for (int pos = start_pos+1; pos <= end_pos; pos++)
      if (data[pos].low < data[pos_of_smallest].low)
        pos_of_smallest = pos;

    return pos_of_smallest;
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:39:48+00:00Added an answer on June 12, 2026 at 7:39 am

    You need to pass your array as a pointer:

    int find_pos_of_smallest (weather *data, int start_pos, int end_pos)
    

    Declaring the parameter as weather data means just a single instance of the struct. Making it a pointer means you can pass an array (the pointer will get the address of the first element in the array, and when you index it with data[pos] you will get the relevant element in that array).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently in an introductory level Java class, and am working on the classic
Currently working with converting SQLException error messages into messages that are more useful for
Currently, my MVC 3 app has a dependency on a static class that is
Currently I am writing a program for an introductory Java class. I have two
Currently I know of only two ways to cache data (I use PHP but
Hello this is my question: I am currently working on an introductory course on
I'm currently learning Python using Zelle's Introductory text, and I'm trying to recreate one
Currently, I am writing a MiddleWare application that synchronizes information between and accounting application
Currently, I'm working on a project where I have a server - client relationship
Currently I am trying to use a bunch of custom perl modules, test.pm as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.