-4

I'm doing a command interpreter and one of the commands must have as result quiting from the program. When user inserts "q", the program should stop executing.

I've written this, but it seems very useless.

Thanks in advance.

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

void exiting (char x){
if (x=='q') exit(0);

int main(){
char x;
scanf("%c", x);
exiting(x);
return 0;
}
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
cooper
  • 23
  • 3
  • Format code and get it to compile – Ed Heal Mar 23 '15 at 15:29
  • 1
    What, exactly, is your question? – DevSolar Mar 23 '15 at 15:29
  • Not understanding what the question is. Aside from the missing `}` at the end of `exiting`, there doesn't appear to be any issue. (ETA: and the fact that you should use `&x` instead of `x`) – wolfPack88 Mar 23 '15 at 15:29
  • 2
    `scanf("%c", x);` should be `scanf(" %c", &x);`. – haccks Mar 23 '15 at 15:30
  • Calling `exit(0)` is a [perfectly reasonable](http://stackoverflow.com/questions/2425167/use-of-exit-function) way to halt program execution, but you might want to at least print out to the user that the program is halting because they typed the exit command first. – aruisdante Mar 23 '15 at 15:31
  • 1
    In the current form, your program will either `exit(0)`or `return 0;`which is the same (for all practical purposes) – DrKoch Mar 23 '15 at 15:32
  • 1
    @DrKoch In the current form, it will not compile. – Emil Laine Mar 23 '15 at 15:33
  • @zenith Well, while reading all the comments I applied (mentally) all necessary patches before writing my comment ;) – DrKoch Mar 23 '15 at 15:36
  • @wolfPack88 Thank you. I was distracted. Now it compiles and it is working. – cooper Mar 23 '15 at 15:48
  • @cooper , I've rolled-back your edits as it made my answer, as well as, many of the comments above meaningless. BTW, Remove `else main()` from your edited code. – Spikatrix Mar 23 '15 at 16:01
  • @CoolGuy Thank you very much for your answer. Just tell me... Why should I remove else main() ? It actually seems "not elegant", but I verified that if I do not put that part of the code, any letter can quit the program, isn't that so? – cooper Mar 23 '15 at 18:12
  • @cooper , Calling `main()` recursively is a bad idea. The better way would be to use a `do...while` loop. Yes, any letter can quit the program but I am assuming that you will add more code in the program soon. – Spikatrix Mar 24 '15 at 03:31

2 Answers2

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

void exiting (char x){
    if (x=='q') exit(0);
}

int main(){
    char x;
    scanf("%c", &x); //scanf requires a pointer since it's the only C way to change a value of a variable within a function
    exiting(x);
    return 0;
}

Or, if you want your program to run until you press 'q', that would be the code:

#include <stdio.h>
int main(int argc, char* argv[])
{
    char x;
    do
    {
    scanf("%c", &x);
    }while(x != 'q');
}
Ivo Valchev
  • 215
  • 3
  • 11
0
scanf("%c", x);

Should be

scanf("%c", &x);

Because scanf with %c expects an argument of type char*. You provide the argument x, which is a char and thus, gives the value of x(which is garbage as x isn't initialized) as the second argument instead of giving the address of x for scanf to store the input. This invokes Undefined Behavior. & functions as the address-of operator and gives the address of its operand.

Also, you forgot to add } here:

void exiting (char x){
    if (x=='q') exit(0);
} //this
Spikatrix
  • 20,225
  • 7
  • 37
  • 83