Nach eingabe Fehlermeldung: Das Programm hat einen Fehler festgtestellt und...

FBIagent

Erfahrenes Mitglied
Wie im Betreff schon gesagt: Wenn ich mein kompiliertes Programm starte beendet es
mit einem Fehler nach der eingabe. Hier der code:

main.cpp
Code:
#include <iostream>
#include <windows.h>
#include <mysql/mysql.h>
#include <conio.h>
#include "conection.h"

using namespace std;

char *msg[5];
char *input_str;
int input_int;
con_info conection;
MYSQL my_sql;

void init()
{
  msg[0] = "Hostname/IP";
  msg[1] = "Port";
  msg[2] = "Username";
  msg[3] = "Password";
  msg[4] = "Database";

  for(int i=0;i<5;i++)
  {
    cout << msg[i] << ": ";
    
    switch(i)
    {
      case 0:
        cin >> input_str;
        conection.set_host(input_str);
        input_str = NULL;
      case 1:
        cin >> input_int;
        conection.set_port(input_int);
        input_int = 0;
      case 2:
        cin >> input_str;
        conection.set_user(input_str);
        input_str = NULL;
      case 3:
        cin >> input_str;
        conection.set_pass(input_str);
        input_str = NULL;
      case 4:
        cin >> input_str;
        conection.set_db(input_str);
        input_str = NULL;
    }
  }
  
  if(mysql_real_connect(&my_sql,conection.get_host(),conection.get_user(),conection.get_pass(),conection.get_db(),conection.get_port(),NULL,0))
  {
    cout << "Verbindung wurde hergestellt.";
    Sleep(1000);
  }
  else
  {
    cout << "Verbindung konnte nicht hergestellt werden!";
    cout << conection.get_host() << "\n";
    cout << conection.get_port() << "\n";
    cout << conection.get_user() << "\n";
    cout << conection.get_pass() << "\n";
    cout << conection.get_db() << "\n";
  }
}

int main(int argc, char *argv[])
{
  init();
  getch();
  return 0;
}
conection.h:
Code:
// the class
class con_info
{
private:
  char* host;
  int port;
  char* user;
  char* pass;
  char* db;
public:
  void set_host(char* data);
  void set_port(int data);
  void set_user(char* data);
  void set_pass(char* data);
  void set_db(char* data);
  char* get_host();
  int get_port();
  char* get_user();
  char* get_pass();
  char* get_db();
};

// set infos
void con_info::set_host(char* data)
{
  host = data;
}

void con_info::set_port(int data)
{
  port = data;
}

void con_info::set_user(char* data)
{
  user = data;
}

void con_info::set_pass(char* data)
{
  pass = data;
}

void con_info::set_db(char* data)
{
  db = data;
}

// get infos
char* con_info::get_host()
{
  return host;
}

int con_info::get_port()
{
  return port;
}

char* con_info::get_user()
{
  return user;
}

char* con_info::get_pass()
{
  return pass;
}

char* con_info::get_db()
{
  return db;
}

Woran könnte es liegen das ich bei oder nach der eingabe ( cin >> input_str; ) diesen
Fehler bekomme?

Ich würde gerne den datentyp 'String' benutzen anstatt 'char*' aber damit hatte ich
nur ärger.

THX im Vorraus
MFG FBIagent

EDIT:
Für char* jetzt den Typ String genommen. Jetzt habe ich einen Fehler:
mysql_real_connect();
will nen char* haben.
Wie bekomm ich einen std::string in einen char*?
 
Zuletzt bearbeitet:
Code:
char *input_str;
cin >> input_str;

Was passiert da wohl wenn der Inputstream in einen nicht initialisierten Zeiger reinschreibt?

Abhängig davon obs debug oder release gebaut ist ist input_str entweder 0 oder undefiniert. Beides heisst, Du schreibst die Eingabe irgendwohin in den Speicher ohne diesen vorher zu allozieren.
 
msg[0] = "Hostname/IP";
msg[1] = "Port";
msg[2] = "Username";
msg[3] = "Password";
msg[4] = "Database";

ich weis ja nicht aber is das so korekt du machst da nen vektor mit einem elemnt und fügst mehrere zeichen ein.

( kenn mcih mit windows programmierung nicht so aus )
 
Naja auf jeden fall läufts jetzt mit Typ String (Stichwort c_str())

main.cpp:
Code:
#include <iostream>
#include <windows.h>
#include <mysql/mysql.h>
#include <conio.h>
#include "conection.h"

using namespace std;

string msg[5];
string input_str;
int input_int;
con_info conection;
MYSQL my_sql;

void init()
{
  msg[0] = "Hostname/IP";
  msg[1] = "Port";
  msg[2] = "Username";
  msg[3] = "Password";
  msg[4] = "Database";

  for(int i=0;i<5;i++)
  {
    cout << msg[i] << ": ";
    
    switch(i)
    {
      case 0:
        cin >> input_str;
        conection.set_host(input_str);
        input_str = "";
        break;
      case 1:
        cin >> input_int;
        conection.set_port(input_int);
        input_int = 0;
        break;
      case 2:
        cin >> input_str;
        conection.set_user(input_str);
        input_str = "";
        break;
      case 3:
        cin >> input_str;
        conection.set_pass(input_str);
        input_str = "";
        break;
      case 4:
        cin >> input_str;
        conection.set_db(input_str);
        input_str = "";
        break;
    }
  }
  
  system("cls");
  if(mysql_real_connect(&my_sql,conection.get_host().c_str(),conection.get_user().c_str(),conection.get_pass().c_str(),conection.get_db().c_str(),conection.get_port(),NULL,0))
  {
    cout << "Verbindung wurde hergestellt.";
    Sleep(1000);
  }
  else
  {
    cout << "Verbindung konnte nicht hergestellt werden!\n\n";
    cout << conection.get_host() << "\n";
    cout << conection.get_port() << "\n";
    cout << conection.get_user() << "\n";
    cout << conection.get_pass() << "\n";
    cout << conection.get_db() << "\n";
  }
}

int main(string argv[])
{
  init();
  mysql_close(&my_sql);
  getch();
  return 0;
}

conetion.h:
Code:
#include <iostream>
using namespace std;

// the class
class con_info
{
private:
  string host;
  int port;
  string user;
  string pass;
  string db;
public:
  void set_host(string data);
  void set_port(int data);
  void set_user(string data);
  void set_pass(string data);
  void set_db(string data);
  string get_host();
  int get_port();
  string get_user();
  string get_pass();
  string get_db();
};

// set infos
void con_info::set_host(string data)
{
  host = data;
}

void con_info::set_port(int data)
{
  port = data;
}

void con_info::set_user(string data)
{
  user = data;
}

void con_info::set_pass(string data)
{
  pass = data;
}

void con_info::set_db(string data)
{
  db = data;
}

// get infos
string con_info::get_host()
{
  return host;
}

int con_info::get_port()
{
  return port;
}

string con_info::get_user()
{
  return user;
}

string con_info::get_pass()
{
  return pass;
}

string con_info::get_db()
{
  return db;
}
 
Zurück