I have no idea hw to approach this... i need to end my program, shown below by the user pressing the letter Q on the keyboard. Help?

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

float subtract(float x, float y);

int main (void)
{
    float x, y;

    printf("Please enter a real number:\n");
    scanf("%f", &x);

    printf("Please enter another real number:\n");
    scanf("%f", &y);

    printf("The difference of both values inputted: % f\n", subtract(x, y));       
}


float subtract (float x, float y)

{
    return x-y;    
}

I think you'll need to use the Chr(Charactercode) statement. I haven't done these for a while. You'll need the ASCII value for an upper and lower case q and perhaps use an if statement.

Basically, if user input equals the Chr value then quit.

I'm very rusty so don't have the code on the tip of my tongue but I hope this perhaps helps.

Oh I'm sorry I just realised this is C your after. I thought this was vb.

Edited 3 Years Ago by mark.scutt.7

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

float subtract(float x, float y);
int main ()
{
    float x, y;
    printf("Please enter a real number:\n");
    scanf("%f",&x);
    printf("Please enter another real number:\n");
    scanf("%f",&y);
    printf("The difference of both values inputted: %f\n", subtract(x, y));

    return 0;
}
float subtract (float x, float y)
{
    return abs(x-y);    
}

shown below by the user pressing the letter Q on the keyboard. Help?

what do you mean that?

kind of... more like this?

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

struct complex

{
  int real,imag;
};

int main()
{
     struct complex x, y, z;
    printf("Please enter the two real and imaginary complex numbers (a+jb) :\n");
    printf("\nReal Number:");
    scanf("%d", &x.real);
    printf("\nImaginary Number:");
    scanf("%d", &x.imag);

    printf("\nPlease enter the next two real and imaginary complex numbers (c+jd) :\n");
    printf("\nReal Number:");
    scanf("%d", &y.real);
    printf("\nImaginary Number:");
    scanf("%d", &y.imag);

    printf("\nFrist Complex Number:%d+i%d",x.real, x.imag);
    printf("\nSecond Complex Number:%d+i%d\n",y.real, y.imag);

    z.real=x.real+y.real;
    z.imag=x.imag+y.imag;


    printf("\nThe result of the addition of the two complex numbers:\n",z.real, z.imag);  


   return 0;

i'm having difficulty showing the result though?

i'm having difficulty showing the result though?

because you write

printf("\nThe result of the addition of the two complex numbers:\n",z.real, z.imag);

you must write

  printf("\nThe result of the addition of the two complex numbers:%d+i%d\n",z.real, z.imag); 

as z.real and z.imag is integer veriables

ooopsss i was posting in the wrong discussion D: i feel stupid now.
i don't mean return 0;

i basically need to end the program by pressing Q in the command prompt

At what point would the user be pressing Q to quite the program?
Or is it at any point?

Edited 3 Years Ago by RockJake28

ooopsss i was posting in the wrong discussion D: i feel stupid now.

no problem

i don't mean return 0;

you should write return 0 to end main function and program.

as you write main function return integer.

you can write that:

void main()
{

}

instead of that:

int main()
{

return 0;
}

i basically need to end the program by pressing Q in the command prompt

you can use ASCII number of character q

#include <stdlib.h>
    #include <stdlib.h>
    float subtract(float x, float y);
    int main (void)
    {
    float x, y;
    char quit;

    printf("                                                       >>>>Q to exit<<<<\n"); 
    printf("Please enter a real number:\n");

    scanf("%f", &x);                                                        
    scanf("%c", &quit);
    if(quit == 'q' || quit=='Q' )
    exit(0);



    printf("Please enter another real number:\n");
    scanf("%f", &y);

     printf("The difference of both values inputted: % f\n", subtract(x, y));
    }
    float subtract (float x, float y)
    {
    return x-y;
    }

There is a better and easier solution. Note: you will also have to press the <Enter> key becuse C does not have a standard function to just get a single key. You could use functions in conio.h to do that, but be warned that not all compilers support that because it's non-standard.

int main()
{
    int quit = 'N';
    while( quit != 'Q' )
    {
        // do something here

        printf("Do you want to continue? (Press Q to quit)\n");
        quit = getchar();
    }
    return 0;
}
#include<stdio.h>
#include<conio.h>

struct Complex
{
  int real,imag;

};
void Menu(void);

int main()
{
  int flag=0;
  char ch;
  struct Complex x;

   do
  {  
     Menu();
     ch=getch();

         if(ch==69||ch==101) // if user enter E or e from keyboard
         {
             clrscr();
             printf("Please enter the two real and imaginary complex numbers (a+jb) :\n");
                 printf("real number:");
                 scanf("%d",&x.real);
                 printf("\nImaginary number:");
                 scanf("%d", &x.imag);         
         }
         else if(ch==81||ch==113)  // if user enter Q or q from keyboard  
         {
            flag=1;  
         }
  }while(flag!=1);  


 return 0;
}
void Menu()  // menu  which  diplay every time for user and  still wait until user enter E or Q
{
 clrscr();
 printf("Press E to Enter Complex Number");
 printf("\nPress Q to quit");
}

another solution to your question !!!

Edited 3 Years Ago by ahmedhamdy

if(ch==69||ch==101)
else if(ch==81||ch==113)

What do 69 and 101 mean? What do 81 and 113 mean? If you force me to memorize ASCII/Unicode or go to asciitable.com to figure out your code then your code is bad. Use the character literals that correspond to what you want, it makes the code much clearer:

if (ch == 'E' || ch == 'e') {
    ...
}
else if (ch == 'Q' || ch == 'q') {
    ...
}

You can also use toupper() or tolower() to make a single test:

if (toupper(ch) == 'E') {
    ...
}
else if (toupper(ch) == 'Q') {
    ...
}

Note: you will also have to press the <Enter> key becuse C does not have a standard function to just get a single key

getch() don't need press <Enter> every time after user enter any character
and getch() don't print character which user enter it

What do 69 and 101 mean? What do 81 and 113 mean? If you force me to memorize ASCII/Unicode or go to asciitable.com to figure out your code then your code is bad. Use the character literals that correspond to what you want, it makes the code much clearer:

you are right

#include<stdio.h>
#include<conio.h>

struct Complex
{
  int real,imag;

};
void Menu(void);

int main()
{
  int flag=0;
  char ch;
  struct Complex x;

   do
  {  
     Menu();
     ch=getch();

         if(ch=='E'||ch=='e') // if user enter E or e from keyboard
         {
             clrscr();
             printf("Please enter the two real and imaginary complex numbers (a+jb) :\n");
                 printf("real number:");
                 scanf("%d",&x.real);
                 printf("\nImaginary number:");
                 scanf("%d", &x.imag);         
         }
         else if(ch=='Q'||ch=='q')  // if user enter Q or q from keyboard  
         {
            flag=1;  
         }
  }while(flag!=1);  


 return 0;
}
void Menu()  // menu  which  diplay every time for user and  still wait until user enter E or Q
{
 clrscr();
 printf("Press E to Enter Complex Number");
 printf("\nPress Q to quit");
}

getch() don't need press <Enter> every time after user enter any character
and getch() don't print character which user enter it

That doesn't help if getch() isn't supported by the compiler. It's fine in your world of Turbo C, but your code will fail to compile almost everywhere else.

Edited 3 Years Ago by deceptikon

is this code will run on all compiler

#include<stdio.h>

struct Complex
{
  int real,imag;

};

int main()
{
  int flag=0;
  char ch;
  struct Complex x;

   while(flag!=1)
  {
      printf("\nPress E then press Enter to Enter Complex Number");
      printf("\nPress Q then press Enter to quit\n");
      scanf("%c",&ch);

     if(ch=='E'||ch=='e') // if user enter E or e from keyboard
     {
         //clrscr();
         printf("\nPlease enter the two real and imaginary complex numbers (a+jb) :\n");
         printf("\nreal number:");
         scanf("%d",&x.real);
         printf("\nImaginary number:");
         scanf("%d", &x.imag);
     }
     else if(ch=='Q'||ch=='q')  // if user enter Q or q from keyboard
     {
        flag=1;
     }
  };


 return 0;
}

is this code will run on all compiler

All C99-compatible or C++ compilers, sure (due to the // style comments). ;) I'd also nitpick your prompts that don't end in a newline because they might not show up on all systems before scanf() blocks for input. But those are minor issues for strict conformance.

erm i'm using the compiler in quincy 2005?

Quincy is an IDE, not a compiler. It uses MinGW as a back end, which basically amounts to the GCC compiler. GCC supports C99 under that version, as far as I can tell, but you have to turn it on with a switch. I doubt it's turned on by default, so there you go.

Line 19 - scanf("%c",&ch);
This is overkill (reason).

That's a good reason, but I think I can make it more clear how much work is done. This link is an implementation of fgetc() that I wrote (where fgetc() is the back end to getchar()); it's about 10 lines of straightforward and efficient code. Even if you include the definition of fillbuf(), the whole thing comes in at under 50 lines.

This link is the implementation for scanf(), and this link is the format string parsing helper. I'd estimate that all totaled up you're looking at close to 900 lines of code, and it's not exactly trivial. I spent more time writing printf() and scanf() than the rest of the standard library in that project put together. Naturally not all of it will be hit for a simple format string like "%c", but scanf() is undeniably a heavy function compared to getchar().

Edited 3 Years Ago by deceptikon

Comments
Good post.

ok so i tried the code above and it didn't work...all i need is a way to press Q to end the program!

This question has already been answered. Start a new discussion instead.