I'm trying to make a Gematria calculator in c++ and want to get the same results i am getting in java. How would I get so in C++ it could print the sum of the values from what the user inputs and get the same ultimate result as I would in Java? Please note I'm a beginner at C++ so please forgive me for any ignorance in this question.
I managed thanks to stackoverflow to make one in Java. In Java I have I could enter multiple words and get the Gematria, and I want to get the same results in C++.
Java Code
package com.company;
`import java.util.Hashtable;`
`import java.util.Scanner;`
public class English_Gematria {
public static void main(String[] args) {
Hashtable<String, Integer> Gematria_Values = new Hashtable<>();
Gematria_Values.put(" ", 0);
Gematria_Values.put("A", 1);
Gematria_Values.put("B", 2);
Gematria_Values.put("C", 3);
// Goes through rest of alphabet.
System.out.println("What do you want the gematria of?");
Scanner i = new Scanner(System.in);
String Gematria = i.nextline();
int sum = 0;
for (int j = 0; Gematria.length(); j++) {
sum += Gematria_Values.get(String.valueOf(Gematria.toUpperCase().charAt(j)));
}
System.out.println("The Gematria of " + Gematria + " is " + sum);
C++ Code
#include <iostream>
#include <cmath>
#include <map>
using namespace std;
int main() {
std::map<std::string, std::double_t> Gematria_Values;
Gematria_Values[" "] = 0;
Gematria_Values["A"] = 1;
Gematria_Values["B"] = 2;
Gematria_Values["C"] = 3;
// Goes through rest of alphabet.
std::string Gematria;
std::cout << "What do you want the gematria of?" << std::endl;
std::cin >> Gematria;
transform(Gematria.begin(), Gematria.end(), Gematria.begin(), ::toupper);
int sum = 0;
for (int i = 0; i < Gematria.length(); i++)
{
sum += Gematria_Values.at(Gematria);
}
std::cout << "The Gematria is " << sum;
return 0;
}
In C++ when user input is (without the quotes) "a" or "A" I get
The Gematria is 1.
When the user input is (without the quotes) "b" or "B" I get
The Gematria is 2.
However when the user input is (without the quotes) "ab" or "bb" I get
terminate called after throwing an instance of 'std::out_of_range'
what(): map::at
My question is: How would I get the same the ultimate result as I do in Java?