-5

Today i was learning and testing different forms of using switch statement in C++; then i wrote this code to make a function that allows user to input three characters that two are the cases and one is the comparing character(Main input). I decided to use pointers since i couldn't use variables for the cases, but my approach didn't work and i just don't understand why? cause using pointers actually means that i'm pointing at the value of the address which already has been defined!

This is the errors:


[Error] 'iloc' cannot appear in a constant-expression
[Error] '*' cannot appear in a constant-expression

This is the code:

#include <iostream>

using std::cout;
using std::cin;

void switch_function(char i, char j, char c){

//inputing values by the user

cout <<"Insert i(char): ";
cin >> i;
cout <<"Insert j(char): ";
cin >> j;
cout <<"Insert c(char) "<< i <<" Or "<< j <<": ";
cin >> c;

//declaring pointers

char * iloc;
char * jloc;
char * cloc;

//registering memory adresses

iloc = &i;
jloc = &j;
cloc = &c;

//switch function

switch(*cloc){

    case *iloc:

        cout << i;
        break;

    case *jloc:

        cout << j;
        break;
    }
   }

 int main(){

//s and f characters are the cases and the third f is the main user input.

switch_function('s', 'f', 'f');

cout <<"\n";

int location;
int * target;

target = &location;
cout << &location;
cout <<"\n"<< target + 1;
}
  • 4
    Presumably, "didn't work" means that the compiler told you that something was wrong. Read the error message carefully. If you can't figure out what it means, include it in your question. – Pete Becker Jul 13 '17 at 14:38
  • 2
    share with us too where are you learning ***"switch classes"*** please! – ΦXocę 웃 Пepeúpa ツ Jul 13 '17 at 14:41
  • Sorry, my bad. so do you have any thing to say about this? @ΦXocę 웃 Пepeúpa ツ –  Jul 13 '17 at 14:53
  • I just did... so what is the problem? @PeteBecker –  Jul 13 '17 at 14:54
  • 4
    You probably need a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Passer By Jul 13 '17 at 14:57
  • @NotGreen Note: you have other problems in your code - pointers will not point to the right thing. In your case, pointers are not needed; you can use a numeric flag instead, whose meaning is something like "0 - print this; 1 - print that". As an added bonus, if you use a simple number, you can use `switch`. – anatolyg Jul 13 '17 at 15:06
  • 1
    yes... what can you infer is meant with this msg: ***foo cannot appear in a constant-expression*** – ΦXocę 웃 Пepeúpa ツ Jul 13 '17 at 15:11

1 Answers1

1

case *iloc: is not possible. case only accept constants known at compile time. use if/else if in this case instead

IlBeldus
  • 1,040
  • 6
  • 14
  • Thank you sir, but may i say that using pointers makes the variable a constant because it actually pointing to the value that has defined(when it's defined so it's a constant!) before the switch statement come in line? @IlBeldus –  Jul 13 '17 at 15:09
  • 1
    @NotGreen Not really. What you need to ask yourself is: "When I compile my program, can the compiler know the value of `*iloc`?" if the answer is no then it can't be in a `case` condition. For the technical explanation see https://stackoverflow.com/a/10965360/4124855 – IlBeldus Jul 13 '17 at 15:12
  • @NotGreen Not only does case not work with strings, but you'll find that your iloc isn't 'const' which means it can change. If it can change, then it's not a constant at compile time. – UKMonkey Jul 13 '17 at 15:13
  • @UKMonkey despite the name, char is a numeric literal so `const char iloc = 'A'` can be used in a switch case – IlBeldus Jul 13 '17 at 15:15
  • @UKMonkey I'm afraid I have to disagree. it's a pointer to a char integral. it it points to the first `char` in an array of `char` it 'can be seen as a string' but it's definitely NOT a string – IlBeldus Jul 13 '17 at 15:21