Home » excel » arrays – How to incorporate a large list of numbers into a c++ code

arrays – How to incorporate a large list of numbers into a c++ code

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have four lists each containing 84 different rates which I want to be able to access using if/else statements based on the inputted information and I was hoping there was something more efficient than typing each one into an array.

What would be the easiest way to do this? Any hints would be very helpful I just need a starting point.

   #include "MaleNonSmoker.txt"  
    using namespace std;  
    double ratesmn[85] = {  
    #include "MaleNonSmoker.txt"  

        - 1  
   };  
    #include <iostream>  
    #include <string>  
    #define STARTAGE 15  
    int main() {  
        double const *rates;  
        rates = ratesmn;  
        int age;  
            cout << "How old are you?\n";  
cin >> age;  
double myrate = ratesmn[age - STARTAGE];  

        return 0; 
    }

The errors that I am getting are from line 1: syntax error: ‘constant’
and from line 7: ‘too many initializers’

How to&Answers:

If the numbers do not change, there is no real need to read the numbers at runtime from a file. You can also use them at compile time.

Create four files with the arrays with any tool you like, but a comma after each number so it looks like this:

51,
52,
53,

In your c++ code, define 4 arrays, and use #include to include the numbers from the text file;

int ratesms[85] = {

#include "ratesms.txt"

    -1 // add another number because the txt file ends with a comma
};

Do the same for the other arrays.

In your code determine which list you want to use, and set a pointer to that list, for example

int const *rates;

if ( /* smoking male */ )
  rates = ratesms;
else if ( /* other variations */ )
  rates = ...

And then use it like this;

#define STARTAGE 15

int age=35; // example
int myrate=rates[age-STARTAGE];

If you don’t want to substract the start age from the array index, you can also add 15 dummy numbers to the array;

int ratesms[100] = {

    0,0,0,0,0,
    0,0,0,0,0,
    0,0,0,0,0,

#include "ratesms.txt"

    -1 // add another number because the txt file ends with a comma
};

now ratesms[15] will contain the first number from the txt file.

Answer:

You can define an array of numbers in C++ like this:

int[6] rates = {1, 2, 3, 4, 5, 6};

Answer:

What’s the format of your “lists?”

Reading them in should be very simple — check out this tutorial on file I/O in C++. If you save your lists as simple .txt files, you can read each list item line by line by creating an ifstream and calling getline(). The file data will be read as strings, so you can use stoi() and stod() to convert them to integers and doubles, respectively (check out the string reference for more conversion methods).

You also might want to look into saving your excel files as comma-separated-value (.csv) files, which can then be read in line by line in the same manner. Each line will represent a row with cell values separated by commas, which are very easy to parse.