Results 1 to 8 of 8

Thread: C Problem, program exits too fast

  1. #1
    Senior Member
    Join Date
    Jul 2003
    Posts
    1,457
    Thanks
    0
    Thanked
    0 times in 0 posts

    C Problem, program exits too fast

    Code:
    /* B Jeater 2005, Program for inputting 2 numbers 
    and displaying the result of their multiplication */
    
    
    #include <stdio.h> /*Standard Library Included*/
    
    int main()
    
    {
    	int Interger1;	/*First Number to be Input*/
    	int Interger2;	/*Second Number to be Input*/
    	int Result;		/*Result of the Multiplication of the Input Numbers*/
    
    	printf ("This Program Will Multiply 2 Numbers Together,\n Please Enter your First Number: ");
    	scanf ("%d", &Interger1);	/*Enters First Number*/
    	printf ("Please Enter your Second Number: ");
    	scanf ("%d", &Interger2);	/*Enters Second Number*/
    	
    	Result = Interger1 * Interger2;	/*Multiplies the Numbers Together*/
    
    	printf ( "\n\n%d", Interger1);
    	printf ( " Multiplied by ");
    	printf ( "%d", Interger2);
    	printf ( " Equals ");
    	printf ( "%d", Result);
    	printf ( "\n");
    
    	cin.get();
    
    	return 0;
    
    }
    When i run that piece of code, it waits for the 2 inputs, then it works out the correct number, but doesn't give me time to see the answer before closing the command prompt . I thought that the cin.get would stop the program until I hit another key, but it didn't

    Can someone help me out?

  2. #2
    Treasure Hunter extraordinaire herulach's Avatar
    Join Date
    Apr 2005
    Location
    Bolton
    Posts
    5,618
    Thanks
    18
    Thanked
    172 times in 159 posts
    • herulach's system
      • Motherboard:
      • MSI Z97 MPower
      • CPU:
      • i7 4790K
      • Memory:
      • 8GB Vengeance LP
      • Storage:
      • 1TB WD Blue + 250GB 840 EVo
      • Graphics card(s):
      • 2* Palit GTX 970 Jetstream
      • PSU:
      • EVGA Supernova G2 850W
      • Case:
      • CM HAF Stacker 935, 2*360 Rad WC Loop w/EK blocks.
      • Operating System:
      • Windows 8.1
      • Monitor(s):
      • Crossover 290HD & LG L1980Q
      • Internet:
      • 120mb Virgin Media
    easiest solution is to just run the program from a caommand prompt.

  3. #3
    Ex-MSFT Paul Adams's Avatar
    Join Date
    Jul 2003
    Location
    %systemroot%
    Posts
    1,926
    Thanks
    29
    Thanked
    77 times in 59 posts
    • Paul Adams's system
      • Motherboard:
      • Asus Maximus VIII
      • CPU:
      • Intel Core i7-6700K
      • Memory:
      • 16GB
      • Storage:
      • 2x250GB SSD / 500GB SSD / 2TB HDD
      • Graphics card(s):
      • nVidia GeForce GTX1080
      • Operating System:
      • Windows 10 x64 Pro
      • Monitor(s):
      • Philips 40" 4K
      • Internet:
      • 500Mbps fiber
    For temporary debugging "wait for keypress" code I think I used to use:
    Code:
    #include <conio.h>
    ...
    while (!kbhit());
    Although kbhit() might be one of those Borland & Microsoft specific functions (not ANSI C).

    I think it's some issue with the keyboard buffer not being empty, so get() functions return a result immediately - I think there's a function for clearing the buffer manually to avoid this problem.
    Last edited by Paul Adams; 16-10-2005 at 04:20 PM.
    ~ I have CDO. It's like OCD except the letters are in alphabetical order, as they should be. ~
    PC: Win10 x64 | Asus Maximus VIII | Core i7-6700K | 16GB DDR3 | 2x250GB SSD | 500GB SSD | 2TB SATA-300 | GeForce GTX1080
    Camera: Canon 60D | Sigma 10-20/4.0-5.6 | Canon 100/2.8 | Tamron 18-270/3.5-6.3

  4. #4
    Bigger than Jesus Norky's Avatar
    Join Date
    Feb 2005
    Posts
    1,579
    Thanks
    1
    Thanked
    8 times in 8 posts
    Try adding a do loop around the whole thing for a "Try again? y/n" sort of affair

  5. #5
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,168
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts
    This is actually upto the shell as to what to do, some guy claimed i failed my BIO first round exams because i didn't put some yes/no wait on loop at the end.

    If you run it via CMD, then command will retain the output for you.

    Its not considered the "done thing" to wait for the output in code, all standard utils dont (eg ipconfig)

    If your doing some marking of code with lotsa exe's, it would possible to tell windows to asociate .exe with a program of your making, which quickly looks at the code to see if its a console program, and if so run's it via cmd, if not runs it normally. Might be a handy tool to make some time (add's to list).
    throw new ArgumentException (String, String, Exception)

  6. #6
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts
    Whack a getch(); in there?
    To err is human. To really foul things up ... you need a computer.

  7. #7
    www.5lab.co.uk
    Join Date
    Sep 2003
    Posts
    6,406
    Thanks
    1
    Thanked
    0 times in 0 posts
    just run it from a command prompt.. winkey-r...cmd
    hughlunnon@yahoo.com | I have sigs turned off..

  8. #8
    Theoretical Element Spud1's Avatar
    Join Date
    Jul 2003
    Location
    North West
    Posts
    7,508
    Thanks
    336
    Thanked
    320 times in 255 posts
    • Spud1's system
      • Motherboard:
      • Gigabyte Aorus Master
      • CPU:
      • 9900k
      • Memory:
      • 16GB GSkill Trident Z
      • Storage:
      • Lots.
      • Graphics card(s):
      • RTX3090
      • PSU:
      • 750w
      • Case:
      • BeQuiet Dark Base Pro rev.2
      • Operating System:
      • Windows 10
      • Monitor(s):
      • Asus PG35VQ
      • Internet:
      • 910/100mb Fibre
    or....

    assuming your using Visual Studio to do this, just run it by pressing F5, should wait before closing

    ie run it in debug mode ;p works great


    Temp work around is to add in a scanf or something but its a dirty way try running in debug mode first, only real diff between compiling/running that way to a release on is that VS generates a tonne of cr@p to go with it, but unless you have a 2meg hard disk its not an issue

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. XFX 6800GT Problem
    By nvisage in forum SCAN.care@HEXUS
    Replies: 45
    Last Post: 07-08-2006, 12:28 AM
  2. Possible PSU/Mobo problem?
    By Hybrid in forum Help! Quick Relief From Tech Headaches
    Replies: 2
    Last Post: 05-01-2005, 11:11 PM
  3. SNG45 v2 posting problem
    By weezerboy25 in forum PC Hardware and Components
    Replies: 1
    Last Post: 29-12-2004, 11:28 PM
  4. My PC problem.
    By Anders in forum PC Hardware and Components
    Replies: 21
    Last Post: 21-09-2003, 02:36 AM
  5. Replies: 4
    Last Post: 19-09-2003, 08:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •