Question

My code:

char function [100];  
switch (function [i]) {  
    case '+' : {  
    } break;  
    case '*': {  
    }break;  
    case '\'' :{  
    }break;  
    default: {  
          printf ("argument %c not recognized \n", function [i]);  
          return 1;  
        }  
} return 0;  

I'm executing program with: ./a.out "AB+A'*" but everything what I see is that apostrophe is not recognized. How I should write this switch case? Of course I can't give ./a.out "AB+A\'*" - \ not recognized and ./a.out AB+A\'* - ' not recognized

Was it helpful?

Solution

You are missing a break

    case '\'': {
      if (wskaznikStosu < 1) {
        printf ("\nZa dużo operandów \n");
        return 1;}
      else {
      int a = stos [wskaznikStosu - 1];
      wskaznikStosu --;
      stos [wskaznikStosu] = not (a);
      wskaznikStosu ++;
      }
    }
    // YOU ARE MISSING A BREAK HERE AND FALLING INTO THE DEFAULT
    default: {

OTHER TIPS

I have watched answers and all you need to do is read the post:

How to convert string to char array in C++?

and use code in Artur's answer.

Here is what I did. It seems to work :

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  int i;

  if (argc != 3)
    {
      fprintf (stderr, "argument error\n");
      return EXIT_FAILURE;
    }

  for (i = 0; i < atoi(argv[2]); ++i) {
    switch (argv[1][i]) {  
    case '+' :
      printf ("+\n");
      break;  
    case '*':
      printf ("*\n");
      break;  
    case '\'' :
      printf ("'\n");
      break;
    default:
      printf ("argument %c not recognized \n", argv[1][i]);
      return EXIT_FAILURE;
    }
  }


  return EXIT_SUCCESS;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top