Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Any one good with C?  (Read 958 times)

zarmazarma

  • Bay Watcher
    • View Profile
Any one good with C?
« on: July 16, 2009, 09:10:10 pm »

I figure some one is, however you only need to be half decent to figure out my problem >.>. I've got a program (this-)

#include <stdio.h>
#include <unistd.h>
main()
{
      printf("How much iron would you like to purchase? (80ql)\n");
     
      int ch1;
     
      getchar();
     
     
      ch1 = getc( stdin );
     
      double y;
      y = ch1*2.5;
     
      printf("%d\n", y);
     
     
      return(0);
     
      }

Which I've complied and tested, no bugs come up in the debugger (I'm using dev C++), but, the program closes immediately after I press enter. This is what happens when you run the program-

How much iron would you like to purchase?(80ql)
/number goes here/*enter*
/number comes out here/

After I click enter however, it closes before I get a chance to see it. I'm using the consol to run it, so it's set to automatically close. I was told I need to put a sleep() function in, but I'm not quite sure how to do this. Anyone know what to do?
Logged

Mr Tk

  • Bay Watcher
  • Would you like a mint? It's only waffer thin.
    • View Profile
Re: Any one good with C?
« Reply #1 on: July 16, 2009, 11:40:54 pm »

sleep(n) where n is the number of milliseconds you want it to sleep for.

Or at the very end you could get input again which means it will do nothing until you give it that extra input.

Also try system("pause"). This may or may not work.

Finally go start->run and type in cmd. Type the location and name of the exe you want to run and it shouldn't disappear on you.
Logged
First ten minutes of play I ate my loincloth and then got some limbs torn off by a super friendly rat. Thumbs up from me.

zchris13

  • Bay Watcher
  • YOU SPIN ME RIGHT ROUND~
    • View Profile
Re: Any one good with C?
« Reply #2 on: July 17, 2009, 12:10:57 am »

System("pause");
Should save you there.
Logged
this sigtext was furiously out-of-date and has been jettisoned

zarmazarma

  • Bay Watcher
    • View Profile
Re: Any one good with C?
« Reply #3 on: July 17, 2009, 12:46:06 am »

Hmm... both sleep(/number/) and system("pause"); have both came out with the same "system undeclared (first use this function)" when I try to compile it. Sleep comes out as "Sleep undeclared (first use this function)"

Code: [Select]
#include <stdio.h>
#include <unistd.h>
main()
{
      printf("How much iron would you like to purchase? (80ql)\n");
     
      int ch1;
     
      getchar();
     
     
      ch1 = getc( stdin );
     
      double y;
      y = ch1*2.5;
     
      printf("%d\n", y);
     
     
      return(0);
      system("pause");
      }

I've tried system pause both before and after return, it's supposed to be before return right?
Logged

Mr Tk

  • Bay Watcher
  • Would you like a mint? It's only waffer thin.
    • View Profile
Re: Any one good with C?
« Reply #4 on: July 17, 2009, 11:41:30 pm »

First off system("PAUSE") will never happen because it's after the return statement, so you want to put if before.

Also what I forget was you need to include stdlib.h.
Logged
First ten minutes of play I ate my loincloth and then got some limbs torn off by a super friendly rat. Thumbs up from me.

Little

  • Bay Watcher
  • IN SOVIET RUSSIA, LITTLE IS YOU!
    • View Profile
Re: Any one good with C?
« Reply #5 on: July 18, 2009, 12:54:25 am »

I'm good with letters! I got a Gold Star last week since I was able to say all 26 correctly!

I'm special!  ;D
Logged
Blizzard is managed by dark sorcerers, and probably have enough money to bail-out the federal government.

zarmazarma

  • Bay Watcher
    • View Profile
Re: Any one good with C?
« Reply #6 on: July 18, 2009, 03:43:37 am »

I'm good with letters! I got a Gold Star last week since I was able to say all 26 correctly!

I'm special!  ;D

...

Alright, I figured it would need to be before it (as I mentioned.) I'll try including that.
Logged