Home > OS >  How to access data in a struct? C
How to access data in a struct? C

Time:04-14

Just quick one, how should I go about printing a value from a struct? the 'winningnums' contains a string from another function (edited down for minimal example)

I've tried the below but the program doesnt output anything at all, is my syntax incorrect?

 struct past_results {
       std::string date;
       std::string winningnums;
    };

int main(){

    past_results results;

    std::cout << results.winningnums;
     
    return 0;
}

EDIT:

just to give some more insight, here's the function that populates my struct members. Is it something here im doing wrong?

 //function to read csv
void csv_reader(){
    std::string line;
    past_results results;
    past_results res[104];
    

    int linenum = 0;  



    //open csv file for reading
    std::ifstream file("Hi S.O!/path/to/csv", std::ios::in);
    if(file.is_open()) 
    {
       
        while (getline(file, line))
        {
            std::istringstream linestream(line);
            std::string item, item1;
    
            //gets up to first comma
            getline(linestream, item, ',');
            results.date = item;
    
            //convert to a string stream and put into winningnums
            getline(linestream, item1);
            results.winningnums = item1;
    
            //add data to struct
            res[linenum] = results;

            linenum  ;
        }
    }
      
    //display data from struct
    for(int i = 0; i < linenum; i  ) {
        std::cout << "Date: " << res[i].date << " \\\\ Winning numbers: " << res[i].winningnums << std::endl;
    }  

}

CodePudding user response:

Your example doesn't initialize the struct members. There is no data to print, so why would you expect it to output anything?

CodePudding user response:

is my syntax incorrect?

No, it's just fine and if you add the inclusion of the necessary header files

#include <iostream>
#include <string>

then your whole program is ok and will print the value of the default constructed std::string winningnums in the results instance of past_results. A default constructed std::string is empty, so your program will not produce any output.


Your edited question shows another problem. You never call csv_reader() and even if you did, the result would not be visible in main() since all the variables in csv_reader() are local. Given a file with the content:

today,123
tomorrow,456

and if you call csv_reader() from main(), it would produce the output:

Date: today \\ Winning numbers: 123
Date: tomorrow \\ Winning numbers: 456

but as I mentioned, this would not be available in main().


Here's an example of how you could read from the file and make the result available in main(). I've used a std::vector to store all the past_results in. It's very practical since it grows dynamically, so you don't have to declare a fixed size array.

#include <fstream>
#include <iostream>
#include <sstream>  // istringstream
#include <string>   // string
#include <utility>  // move
#include <vector>   // vector

struct past_results {
    std::string date;
    std::string winningnums;
};

// added operator to read one `past_results` from any istream
std::istream& operator>>(std::istream& is, past_results& pr) {
    std::string line;

    if(std::getline(is, line)) {
        std::istringstream linestream(line);

        if(!(std::getline(linestream, pr.date, ',') &&
             std::getline(linestream, pr.winningnums)))
        { // if reading both fields failed, set the failbit on the stream
            is.setstate(std::ios::failbit);
        }
    }
    return is;
}

std::vector<past_results> csv_reader() { // not `void` but returns the result
    std::vector<past_results> result; // to store all past_results read in

    // open csv file for reading
    std::ifstream file("csv"); // std::ios::in is default for an ifstream
    if(file) {
        // loop and read records from the file until that fails:
        past_results tmp;
        while(file >> tmp) {   // this uses the `operator>>` we added above
            // and save them in the `result` vector:
            result.push_back(std::move(tmp));
        }
    }

    return result; // return the vector with all the records in
}

int main() {
    // get the result from the function:
    std::vector<past_results> results = csv_reader();

    // display data from all the structs
    for(past_results& pr : results) {
        std::cout << "Date: " << pr.date
                  << " \\\\ Winning numbers: " << pr.winningnums << '\n';
    }
}
  • Related