Jump to content

Shuts too down too fast can’t see the end result

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
22 replies to this topic

#1
Dr.NoOne

Dr.NoOne

    Newbie

  • Members
  • PipPip
  • 12 posts
Hi I’m a beginner working on this simple program it does work only I can’t see it. It ends too fast it ask the first two questions but when I do the input it shuts down immediately,
So my question is how do you keep it from shutting down automatically

- C Program

#include <stdio.h>
main ()
{
     int a, b, Q, R;
     printf("Give the first whole number: ");
     scanf("%d", &a);
     printf("Give the second whole number: ");
     scanf("%d", &b);
     Q = a/b;
     R = a%b;
     printf("The quotient is: %d\n", Q);
     printf("The rest is: %d\n", R);
     return 0;
}

Edited by Jordan, 26 September 2008 - 03:33 PM.


#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
You can simply ask a question at the end so that you have to press a key before it will continue/exit.

#include <stdio.h>
main ()
{
     int a, b, Q, R;
char quitVal;

     printf("Give the first whole number: ");
     scanf("%d", &a);
     printf("Give the second whole number: ");
     scanf("%d", &b);
     Q = a/b;
     R = a%b;
     printf("The quotient is: %d\n", Q);
     printf("The rest is: %d\n", R);

printf("Press Enter to exit");
scanf("%d", &quitVal);
     
return 0;
}



#3
Dr.NoOne

Dr.NoOne

    Newbie

  • Members
  • PipPip
  • 12 posts
ah thnx that was a very simple solution

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,709 posts
Or you could just do the following:


#include <conio.h>

//...other includes...


int main(void)

{

 //... other code

printf("Press any key...");

_getch();

return 0;

}


By the way, make sure to declare your main as returning an int.

#5
solartic

solartic

    Learning Programmer

  • Members
  • PipPipPip
  • 95 posts
If you are not using windows the above suggestion might not work since conio.h is not a standard c header in which case u can simply use
#include <stdio.h>//no need for any extra header
int main()
{
 //... other code
printf("Press any key...");
getchar();
return 0;
}


#6
martian

martian

    Newbie

  • Members
  • Pip
  • 9 posts
You can just add system("pause") OR getchar() at the end of the program to stop it from shutting down immediately. But some compilers will quit immediately even if you add getchar() at the end.


#include <stdio.h>

int main ()

{

     //other code...

     printf("The rest is: %d\n", R);

     system ("pause");

     return 0;

}


OR


#include <stdio.h>

int main ()

{

     //other code...

     printf("The rest is: %d\n", R);

     getchar();

     return 0;

}



#7
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
But, system("pause") might not work with all OSs...
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#8
Dr.NoOne

Dr.NoOne

    Newbie

  • Members
  • PipPip
  • 12 posts
it works for me. this helps me out greatly, got a lot of similar problems with other stuff I wrote

#9
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
Good to hear :D
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#10
Fcuknutts

Fcuknutts

    Newbie

  • Members
  • Pip
  • 3 posts
this really helped out ,, ! thanks..

#11
Fcuknutts

Fcuknutts

    Newbie

  • Members
  • Pip
  • 3 posts
but ive tried it on this one ,, it isnt workin,,
niether [ getcha ();] nor [ system("pause")]

here is the skript:

Quote

#include <stdio>
main ()
{
int a=10, b=5;
printf("%d\n",a+b);
printf("%d\n",a-b);
printf("%d\n",a*b);
printf("%d\n",a/b);
printf("%d\n",a%b);
getchar();
return 0;
}


#12
Termana

Termana

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,057 posts
Try this - I fixed the include (its stdio.h not stdio), and made the main function return an int. I haven't tested it but it should be valid and work.
#include <stdio.h>
int main ()
{
int a=10, b=5;
printf("%d\n",a+b);
printf("%d\n",a-b);
printf("%d\n",a*b);
printf("%d\n",a/b);
printf("%d\n",a%b);
getchar();
return 0;
}

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!