Jump to content

C program problem(application ends too fast)

- - - - -

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

#1
masteri

masteri

    Newbie

  • Members
  • Pip
  • 4 posts
I ran into a problem.


/*primer2_2*/

/*Pirkaz na ekranu zadatog celog broja u raznim oblicima*/


#include <stdio.h>


main()

{    int x;

     

     printf("\nUnesite ceo broj:\t");

     scanf("%d", &x);

     

     printf("\nDecimalnim oblik:\t%d:",x);

     printf("\nOktalni oblik:\t\t%o",x);

     printf("\nHeksadecimalni oblik:\t%x (%X)\n\n",x,x);

     getchar();

     return(0);

}   


That is a code that i wrote but when i compile it and run it i type a number to see Decimal, Hexadecimal and Octal number of any number that i typed previously but application ends too fast.
Where did i made mistake?
Thnks for Help

#2
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
This is the goddamn 3rd thread I post about this problem....
In C, I see? Then, nothing wrong, bad compiler :p Which compiler, btw?

Try system("pause") instead of getchar(). That might do it, but getchar() is better.
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#3
masteri

masteri

    Newbie

  • Members
  • Pip
  • 4 posts
Thanks it works I use Dev C++ Version 4.9.9.2

#4
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
That's not a bad compiler really, but getchar() won't get you there :p

In C++ there are other methods, see the other threads about this..
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#5
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
scanf() isn't catching the newline, which then goes to getchar(). Use fgets() and sscanf():
/*primer2_2*/
/*Pirkaz na ekranu zadatog celog broja u raznim oblicima*/

#include <stdio.h>
#include <stdlib.h>
main()
{    int x;
     
     printf("\nUnesite ceo broj:\t");
     char * s = malloc(100);
     fgets(s,100,stdin);
     sscanf(s,"%d", &x);
     free(s);
     
     printf("\nDecimalnim oblik:\t%d:",x);
     printf("\nOktalni oblik:\t\t%o",x);
     printf("\nHeksadecimalni oblik:\t%x (%X)\n\n",x,x);
     getchar();
     return(0);
}
That should work.

#6
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
Or 2 getchars, etc...
Those input buffers are annoying, IMO..
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#7
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
All you need to do is always use line-by-line input.

#8
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
I guess, but it's one of those things I'd rather not have to worry about when making a program, eh? It's sort of fun too, though..
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#9
BINNY88

BINNY88

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
type a new header #include<conio.h> & replace getchar() with getch();,it worked well with the modification.

#10
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
I think conio is windows only?
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#11
BINNY88

BINNY88

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
Nope its not winows only header.Actually its part of ANSI c.getch() works only if this header is present.

#12
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
Allright, I wasn't sure :p
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa