Thread: getch() doesn't wait for input

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    2

    Question getch() doesn't wait for input

    I am working on Dev-C++. But the output window closes as soon as the program gets executed. I read through their forums and I tried using getch() but it isn't of any use.

    conn.c

    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <conio.h>
    #include <stdlib.h>
    #include "C:/Program Files/MySQL/MySQLServer5.1/include/mysql.h"
    
    int main(int argc, char **argv)
    {
    
      MYSQL *conn;
      conn = mysql_init(NULL);
    
      if (conn == NULL) {
          printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
          exit(1);
      }
    
      if (mysql_real_connect (
                conn,           /* pointer to connection handler */
                "localhost",    /* host to connect to */
                "root",          /* user name */
                " ",              /* password */
                "test_db",     /* database to use */
                0,               /* port (default 3306) */
                NULL,           /* socket or /var/lib/mysql.sock */
                0 ) == NULL) {
          printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
          exit(1);
      }
    
      if (mysql_query(conn, "create database test_db")) {
          printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
          exit(1);
      }
    
    
      mysql_close(conn);
      printf("Database successfully created.");
      getchar();
      return (0);
    
    }
    any thoughts on why this happens?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Cprogramming.com FAQ > How do I get my program to wait for a keypress?

    You also have exit calls in there that aren't waiting for keypresses either, so it's possible one of your if checks is just failing.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    2
    Yes, there are chances and i have already tried commenting all the exit calls. Rather, I made the program very simple.

    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <conio.h>
    #include "C:/Program Files/MySQL/MySQLServer5.1/include/mysql.h"
    
    
    int main(int argc, char *argv[])
    {
        MYSQL *conn;    /* pointer to connection handler */
        conn = mysql_init ( NULL );
        mysql_real_connect (
                conn,           /* pointer to connection handler */
                "localhost",    /* host to connect to */
                "root",    /* user name */
                " ",     /* password */
                "client_test_db",         /* database to use */
                0,              /* port (default 3306) */
                NULL,           /* socket or /var/lib/mysql.sock */
                0 );            /* flags (none) */
        printf("hello");
        mysql_close ( conn );
    	getch();
        return 0;   
    
    }
    But still it isn't working!! I mean the dev-c++ doesn't retain the output window.

    And in between, thanks for replying.
    Last edited by cathez; 09-07-2011 at 03:55 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program doesn't wait for user input!
    By gaurav_13191 in forum C Programming
    Replies: 8
    Last Post: 07-13-2011, 08:25 AM
  2. getchar() doesn't wait for input
    By cantinero74 in forum C Programming
    Replies: 5
    Last Post: 04-27-2010, 09:46 AM
  3. Wait for input.
    By mjb2287 in forum C++ Programming
    Replies: 10
    Last Post: 09-28-2009, 01:17 AM
  4. Wait for input..
    By guda in forum C Programming
    Replies: 3
    Last Post: 10-09-2007, 01:22 PM
  5. I haven't seen before! getch() doesn't work!
    By StefanA. in forum C++ Programming
    Replies: 8
    Last Post: 02-07-2003, 05:43 AM

Tags for this Thread