0

When I started to make this code I encountered problem with string and switch statement that's why I am not sure that I bypassed this problem correctly. And the main problem is that program print answers only for chart but not for string determinations. Maybe that's because use wrong ''If". Here is my code :

#include <iostream>
#include <string> 

using std::cin;
using std::cout;
using std::string;
using namespace std;

 constexpr long long string_hash(const char *s) {
long long hash{}, c{};
for (auto p = s; *p; ++p, ++c) {
    hash += *p << c;
}
return hash; }   constexpr long long operator"" _sh(const char *s, size_t) {
return string_hash(s); }

int main() {
    cout << "Ievadiet atzimi ar burtiem (A, B, C, D, F) ==> ";
    string atzime;
    char burts;
    double atzime_sk, pluss, minuss;

    cin >> atzime;
    burts = atzime[0];
    switch(burts) 
    {
    case 'A':
        atzime_sk = 4;
        cout << "Tava atzime ir ==> " << atzime_sk;
        break;
    case 'B':
        atzime_sk = 3;
        cout << "Tava atzime ir ==> " << atzime_sk;
        break;
    case 'C':
        atzime_sk = 2;
        cout << "Tava atzime ir ==> " << atzime_sk;
        break;
    case 'D':
        atzime_sk = 1;
        cout << "Tava atzime ir ==> " << atzime_sk;
        break;
    case 'F':
        atzime_sk = 0;
        cout << "Tava atzime ir ==> " << atzime_sk;
        break;
    default:
        break;
    }
    
   if (atzime[1] == '-' || '+')
    {
      switch (string_hash(atzime.c_str()))
      {
      case "+"_sh:
        pluss = atzime_sk + 0.3;
        cout << "Tava atzime ir ==> " << atzime_sk;
        break;
      case "-"_sh:
        minuss = atzime_sk - 0.3;
        cout << "Tava atzime ir ==> " << atzime_sk;
        break;
      default:
        break;
      }
    }
 }
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Alex
  • 1
  • 1
  • 1
    Why did you tag this with [tag:c]? This is _not_ C. – Ted Lyngmo Mar 12 '21 at 15:26
  • 1
    `if (atzime[1] == '-' || '+')` won't do what you expect - that needs to be `if (atzime[1] == '-' || atzime[1] == '+')`. And how did you not get a diagnostic on `case "+"_sh:`? – John Bode Mar 12 '21 at 15:29
  • The `default` cases do nothing with `atzime_sk`, `pluss` or `minuss` which remain uninitialised. – Weather Vane Mar 12 '21 at 15:30
  • What kind of problem? If it's a compilation error, copy compiler messages to the question. If it's undesired output, post it along with expected output. – n. m. could be an AI Mar 12 '21 at 15:30
  • FYI, the `case` statement only works with integers. Single characters are essentially integers. The `case` statement doesn't work with strings. – Thomas Matthews Mar 12 '21 at 15:31
  • What is this funky syntax: `"-"_sh`? What do you want it to do / represent? – Thomas Matthews Mar 12 '21 at 15:32
  • The main problem is that If I write in console ''A'' program gave me correct answer 4, but when I try to write ''A+'' it do noting and print just 4. – Alex Mar 12 '21 at 15:35
  • with ''_sh" as you can see I use ''constexpr'' that's because I can not use string with switch. – Alex Mar 12 '21 at 15:37
  • @Alex Did you read the duplicate question? If so, do you understand why `if (atzime[1] == '-' || '+')` is always `true`? – Ted Lyngmo Mar 12 '21 at 17:18

1 Answers1

0
if (atzime[1] == '-' || '+')

That does not do what you think it does.

((atzime[1]=='-') || '+') so if the first test fails, the second test is just '+' by itself which is true. So it's always true.

You need to write that as: (atzime[1] == '-' || atzime[1] == '+')

Meanwhile, I don't think your subsequent switch statement will ever match anything. You seem to be expecting a string that begins with a letter and then a + or -. So if you type, for example "A+" the hash will not match either that of "+" nor "-" by itself.

JDługosz
  • 5,592
  • 3
  • 24
  • 45
  • If the answer is supposed to provide some sort of help you could break it down and explain what it really does and show how to do what OP intended to do. – Ted Lyngmo Mar 13 '21 at 12:09