简体   繁体   中英

How to properly call functions?

I'm working on my gematria project that I was asking about before and I managed to fix my main issue there, however I'm trying to break the program down into methods, and I'm getting a error with it and am having trouble figuring out how to fix it. Here is my current code.

#include<iostream>
#include<unordered_map>
#include<string>
void PrintGematria(){
std::string Gematria = std::string AskGematria()
    if(!Gematria.empty()){
      std::cout << "The gematria of " << Gematria << " is " << int ConvertLetters(Gematria);
      PrintGematria();
     }
     else {
         std::cout << "Hope you enjoyed\n";
        }
}

[[maybe_unused]] std::string AskGematria(){
   std::cout << "What do you want the gematria of?\n";
   std::string Gematria;
   std::getline(std::cin, Gematria);
   return Gematria;
}

[[maybe_unused] int ConvertLetters(const std::string& Letters){
//Converts letters to numerical value.
std::unordered_map <char, double> Gematria_Values = {
      {' ', 0},
      {'א', 1},
      {'ב',  2},
      {'ג', 3},
//Goes through rest of hebrew alphabet. 
};
int sum = 0;
for (auto ch : Letters)
    sum += Gematria_Values[ch]
return sum;
}

int main() {
//Gives the Value to the user. 
PrintGematria();
return 0;

So at the end in my main method when I reference PrintGematria(), it seems to be fine, however in my PrintGematria() method itself I'm getting an error when calling AskGematria() and when calling ConvertLetters(Gematria), it says about both of those methods Expected '(' for function-style cast or type construction. The thing is that when I try

void PrintGematria(){
std::string Gematria = std::string (AskGematria());
       if(!Gematria.empty()){
           std::cout << "The gematria of " << Gematria << " is " << int (ConvertLetters(Gematria));
           PrintGematria();
       }
       else {
           std::cout << "Hope you enjoyed\n";
       }

I also get a error albiet here it just shows the red and doesn't say what to do there. So ultimately I want to know how do I properly call the methods in this case?

There are two issues with your function call.

  1. You are attempting to call a function before it has been declared / defined. Either move AskGematria above PrintGematria or add a prototype declaration for AskGematria ahead of PrintGematria:

     std::string AskGematria();
  2. The proper way to call the function is std::string Gematria = AskGematria(); . You don't need to put std::string before the function call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM