-1

I want to be able to create and store values by using the console and getting user input to create those values. So I would like to be able to type in the console something like

1234

123

1

and it would save it into my map with a tuple such as

std::map<int, std::tuple<int, int>> info; 

info[1234] = { 123, 1 };

I am completely new to this and have been looking up stuff for a couple hours but I do not understand how to use << >> everywhere I look says to use those. I would like to be able to close the program and open it and the values would still be stored as well.

Any and all information would be appreciated. Thanks.

TrebledJ
  • 8,713
  • 7
  • 26
  • 48
  • 2
    You should follow either an introductory book (https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or some other text/course. You will not learn the language properly by looking up random stuff and gluing it together. –  Nov 17 '18 at 20:16
  • Look up [`operator overloading`](https://www.google.com/search?q=c%2B%2B+operator+overloading) and read up on it. – TrebledJ Nov 17 '18 at 20:42
  • Thank you both for the information I did not know there was a guide on here for learning C++. – creation8383 Nov 20 '18 at 07:15

1 Answers1

0

I've put together some sample code to approximate your task. It reads some integers from standard input, and throws it back to standard output. For your purposes, you may wish to format the output differently, and read/save it from/to a file at some point. There is probably more terse, more robust, more efficient, and prettier ways to do it, but I put this together quickly to get you started. If you find things that are strange to you, I would be happy to answer some questions, but the idea of this site is that you prove that you put some work into finding out the answers first (for example by visiting other questions, reading some books, or consulting the C++ reference at: https://en.cppreference.com/w/cpp)

#include <iostream>
#include <map>
#include <stdexcept>

using namespace std;
using MyMap = map<int, pair<int, int>>;

MyMap::value_type read_entry() {
  int key, v_1, v_2;
  bool started_read{false};
  if ((cin >> key) && (started_read = true) && (cin >> v_1) && (cin >> v_2)) {
    return {key, {v_1, v_2}};
  } else if (started_read) {
    throw invalid_argument("improper input");
  } else {
    return {};
  }
}

MyMap read_map() {
  MyMap myMap;
  while (true) {
    auto entry = read_entry();
    if (cin) {
      myMap.insert(move(entry));
    } else if (cin.eof()) {
      return myMap;
    } else {
      throw invalid_argument("io error");
    }
  }
}

void dump_map(const MyMap &myMap) {
  for (auto &&value : myMap) {
    cout << value.first << "\n"
         << value.second.first << "\n"
         << value.second.second << endl;
  }
}

int main() {
  cout << "reading map..." << endl;
  MyMap myMap;
  try {
    myMap = read_map();
  } catch (invalid_argument e) {
    cout << "error encountered reading map: " << e.what() << endl;
    return 1;
  }

  cout << "dumping map..." << endl;
  dump_map(myMap);
}
Nathan Chappell
  • 2,099
  • 18
  • 21