0

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?

Meir
  • 37
  • 1
  • 7

1 Answers1

1
int main()
{
    std::unordered_map<char, double> Gematria_Values = {
        {' ', 0},
        {'A', 1},
        {'B', 2},
        {'C', 3}
    };

    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 (auto ch : Gematria)
        sum += Gematria_Values[ch];
        
    std::cout << "The Gematria is " << sum;
}

Gematria_Values[ch]; behavior: in case ch is not found in the map it creates and inserts a default new pair into the map (with key ch and value 0).

If you want (you should) check for valid user input: You can stick with at which will throw or, if you don't want/need exceptions you can do:

for (auto ch : Gematria)
{
    auto found = Gematria_Values.find(ch);
    if (found == Gematria_Values.end())
    {
        // deal with error
    }
    else
    {
        sum += found->second;
    }
}
bolov
  • 72,283
  • 15
  • 145
  • 224