0

Apologies for any grammatical errors, english isnt my first language.

my project is to read from a file (whose lines are laid out as "integer"-"integer", eg: 2-6) the first integers represent the number of times a dice needs to be rolled and the second integer represents the number of possible faces on a dice (from the above example; a 6 sided dice was rolled 2 times). i am struggling to use arrays to take the two integers from every line so i can manipulate them.

any idea?

much thanks!!!

  • 1
    related/dupe: https://stackoverflow.com/questions/33106519/ignoring-commas-in-c-cin – NathanOliver Apr 15 '21 at 20:17
  • 3
    It is almost always better to accompany your problem description with your best attempt at solving the problem. This gives potential answers a baseline from which they can construct answers. Without this baseline, answers have to make assumptions about what you know and don't know or start at the very basics like, "Is the computer plugged in?" No one wants that, so do your best to narrow down what does and does not need to be covered with some code. – user4581301 Apr 15 '21 at 20:31
  • Does this do what you want? https://onlinegdb.com/Skn7XNILu – Jerry Jeremiah Apr 15 '21 at 21:39

1 Answers1

0

First, construct a struct (or class if you so prefer) to bind your data i.e. the rolls required and the possible faces into one data structure, something like:

struct Die {
    int rolls;
    int faces;
};

In C++, if you need a dynamic array, prefer to use a std::vector since a lot of the internal memory management (like new/delete) is abstracted away. So, what we need is an array of Dies i.e std::vector<Die>. Now what remains is just reading in the data. First, some error handling

std::ifstream inp("test.txt");

//if error opening file display Error
if(!inp){
    std::cout << "Error opening file";
    return 1;
}

That gets rid of file-opening errors. Next, we create an empty array of Die elements.

std::vector<Die> arr;

Now, it's simple to read in the elements of the Die, one by one:

Die die;
while(inp>>std::ws>>die.rolls) {
    inp.ignore(std::numeric_limits<std::streamsize>::max(), '-');
    inp>>std::ws>>die.faces;
    arr.push_back(die);
}

The std::ws is just to ignore all whitespaces from the input file line. The inp.ignore() part basically reads and ignores all the characters up to - as specified in the code and then the die.faces is read after having ignored the - character. That's it, that reads in one line of numbers like 2-6. Now that just needs to be repeated till the file has no more data to be read, which is taken care of by the while condition while(inp>>std::ws>>die.rolls).

Zoso
  • 3,273
  • 1
  • 16
  • 27
  • thank you so much, for this course we have to use namespace std which i think means no using std::xyz format. but ill work around it. thanks again for the help!!! – MxwlBltzmn Apr 16 '21 at 16:43
  • @MxwlBltzmn If it helps, you can accept the answer :) Also, regarding not using `std::`, you can read why that is the recommended way for using STL constructs and [`using namespace std;`](https://stackoverflow.com/q/1452721/1851678) is generally advised against. – Zoso Apr 16 '21 at 17:47