0

Hi there! I'm new here so I hope I don't do anything wrong.

I've been frustrated over a little issue I encountered on my code, this is the issue:

warning: multi-character character constant [-Wmultichar] 

This issue goes on all my three cases whenever I run the program, which yes, does run, but doesn't give me the proper results.

This is the code:

#include <iostream>
using namespace std;
int main()
{
  int a,b,c, delta=0;

  cout<<"insert the value of a : "<<endl;
  cin>>a;
  cout<<"insert the value of b: "<<endl;    
  cin>>b;
  cout<<"insert the value of c: "<<endl;
  cin>>c;

  delta= b*b -4*a*c;

  switch (delta){
        case '>0':  cout<<"the solutions are different "<<endl;break;
        case '=0':  cout<<"the solutions are equal "<<endl;break;
        case '<0':  cout<<"the solutions are impossible "<<endl;break;
}
system("pause");

}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 2
    You can't use `switch` this way, you must use `if`. Also what is `:cout`? – tadman Mar 29 '21 at 18:29
  • 2
    `case >0: :cout` is wrong syntax, but I cannot reproduce the warning you report https://godbolt.org/z/Yn6aEP9ao. I'd expect the warning for code that has `'asd'` or similar. Also not clear how this code can run, when it does not compile. Please include a [mcve], actual and desired output – 463035818_is_not_an_ai Mar 29 '21 at 18:30
  • 2
    There is literally no way this can run. If you think it's running, you're running an old build or you're compiling another file altogether. – tadman Mar 29 '21 at 18:30
  • I put the numbers of the cases in singular quotes. My bad. But it ran and gave me that error regardless – Kokoro _codeoow Mar 29 '21 at 18:32
  • 1
    Single quotes with more than 1 character between them are called multicharacter literals. You would almost never want these and in this case it would never do what you want it to do. Use if () statements and remove the switch /case. Related: [https://stackoverflow.com/questions/3960954/multicharacter-literal-in-c-and-c](https://stackoverflow.com/questions/3960954/multicharacter-literal-in-c-and-c) – drescherjm Mar 29 '21 at 18:34
  • 2
    `case '>0'` is not what you think it is. Please, don't just keyboard smash and keep rolling the dice until it compiles. C++ is not something you can gamble with. Get a [good C++ reference book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) that explains the fundamentals so you know what to expect. – tadman Mar 29 '21 at 18:34
  • 1
    Please [edit] your question so the code presented here is the actual code producing your error. – Drew Dormann Mar 29 '21 at 18:39
  • @DrewDormann There was a little bait-and-switch here, but we figured it out. – tadman Mar 29 '21 at 18:41
  • 1
    @tadman frankly, still a bit odd that the reason for the warning isnt present in the code. OP could still fix the question – 463035818_is_not_an_ai Mar 29 '21 at 18:44
  • 1
    @largest_prime_is_463035818 Edit button is right there. Fixed it. – tadman Mar 29 '21 at 18:45
  • @tadman I don't believe bait-and-switch questions should be preserved here. This question would only further confuse someone looking for a similar answer, in my humble opinion. – Drew Dormann Mar 29 '21 at 18:45
  • @DrewDormann You've got a valid point, but it took me less than two seconds to fix. – tadman Mar 29 '21 at 18:46
  • @tadman sorry for being pedantic, but this still wasnt the code OP compiled to get wrong output. Anyhow, I knew where to find the button this time :P – 463035818_is_not_an_ai Mar 29 '21 at 18:48
  • @largest_prime_is_463035818 I'm just saying we can always work with them to get the code in the correct state, there's no reason to be timid about it. – tadman Mar 29 '21 at 18:48

1 Answers1

2

Here's your code translated into C++:

#include <iostream>

// Avoid using namespace std, that prefix exists for a reason

int main()
{
  int a,b,c;

  std::cout << "insert the value of a : " << std::endl;
  std::cin >> a;
  std::cout << "insert the value of b: " << std::endl;
  std::cin >> b;
  std::cout << "insert the value of c: " << std::endl;
  std::cin >> c;

  int delta = b*b - 4*a*c;

  if (delta > 0) {
    std::cout << "the solutions are different " << std::endl;
  }
  else if (delta < 0) {
    std::cout << "the solutions are equal " << std::endl;
  }
  else {
    std::cout << "the solutions are impossible " << std::endl;
  }

  return 0;
}

Note the use of if here, which is required if you're not branching based on direct matches of values. All switch can do is test that the input value is literally equal to the various branch values. It cannot do anything but equivalence comparisons.

If you do something like:

switch (x) {
  case '>y':
    // ...
}

Then you're asking that x is literally equal to the (mutibyte) character '>y', whatever that is.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • 1
    Shite. Thank you very much Tadman for solving it. I am a newbie, but thanks anyway :). – Kokoro _codeoow Mar 29 '21 at 18:39
  • I'm only brow-beating you here because you can't just stuff things into the compiler without careful consideration. C++ is complex and unforgiving, but also incredibly powerful and performs very well. Respect that and it'll treat you right. – tadman Mar 29 '21 at 18:40