2012-01-19 2 views
3

Я установил cygwin на свою машину Windows7, и я запускаю некоторый код на C++. Когда я запускаю этот код:Нет сообщений об ошибках или cout на консоли cygwin

#include <iostream> 
#include <string> 
#include <fstream> 
#include <time.h> 
#include <math.h> 
#include "Pop.h" 
using namespace std; 


int main() { 
    ifstream in; 
    ofstream out; 
    cout << "something"; 
    out.open("res7.txt", ios::out|ios::app); 
    if (out.is_open()) 
    out << "1\n"; 
    else 
    cout << "unable to open res7 file"; 
    out.close(); 
    int seed = time(NULL); 
    out.open("res7.txt", ios::out|ios::app); 
    if (out.is_open()) 
    out << "2\n"; 
    else 
    cout << "unable to open res7 file"; 
    out.close(); 
    out.open("res7.txt", ios::out|ios::app); 
    if (out.is_open()) 
    out << "seed " << seed << '\n'; 
    else 
    cout << "unable to open res7 file"; 
    out.close(); 
    out.open("res7.txt", ios::out|ios::app); 
    if (out.is_open()) 
    out << "3\n"; 
    else 
    cout << "unable to open res7 file"; 
    out.close(); 
    srand (seed); 
    out.open("res7.txt", ios::out|ios::app); 
    if (out.is_open()) 
    out << "4\n"; 
    else 
    cout << "unable to open res7 file"; 
    out.close(); 
    double parameters[4]; 
    int location; 
    in.open("input7.txt", ios::in); 
    out.open("res7.txt", ios::out|ios::app); 
    if (out.is_open()) 
    out << "5\n"; 
    else 
    cout << "unable to open res7 file"; 
    out.close(); 
    return 0; 
} 

Я вижу вывод на консоли (что-то). Но когда я запускаю полный код:

#include <iostream> 
#include <string> 
#include <fstream> 
#include <time.h> 
#include <math.h> 
#include "Pop.h" 
using namespace std; 


int main() { 
    ifstream in; 
    ofstream out; 
    cout << "something"; 
    out.open("res7.txt", ios::out|ios::app); 
    if (out.is_open()) 
    out << "1\n"; 
    else 
    cout << "unable to open res7 file"; 
    out.close(); 
    int seed = time(NULL); 
    out.open("res7.txt", ios::out|ios::app); 
    if (out.is_open()) 
    out << "2\n"; 
    else 
    cout << "unable to open res7 file"; 
    out.close(); 
    out.open("res7.txt", ios::out|ios::app); 
    if (out.is_open()) 
    out << "seed " << seed << '\n'; 
    else 
    cout << "unable to open res7 file"; 
    out.close(); 
    out.open("res7.txt", ios::out|ios::app); 
    if (out.is_open()) 
    out << "3\n"; 
    else 
    cout << "unable to open res7 file"; 
    out.close(); 
    srand (seed); 
    out.open("res7.txt", ios::out|ios::app); 
    if (out.is_open()) 
    out << "4\n"; 
    else 
    cout << "unable to open res7 file"; 
    out.close(); 
    double parameters[4]; 
    int location; 
    in.open("input7.txt", ios::in); 
    out.open("res7.txt", ios::out|ios::app); 
    if (out.is_open()) 
    out << "5\n"; 
    else 
    cout << "unable to open res7 file"; 
    out.close(); 
    if (in.is_open()) { 
    out.open("res7.txt", ios::out|ios::app); 
    if (out.is_open()) 
     out << "6\n"; 
    else 
     cout << "unable to open res7 file"; 
    out.close(); 
    char line[256]; 
    while (!in.getline(line, 256).eof()) { 
     out.open("res7.txt", ios::out|ios::app); 
     if (out.is_open()) 
    out << "7\n"; 
     else 
    cout << "unable to open res7 file"; 
     out.close(); 
     char *input = strtok(line, " "); 
     int i=0; 
     while (input!=NULL) { 
    out.open("res7.txt", ios::out|ios::app); 
    if (out.is_open()) 
     out << "8\n"; 
    else 
     cout << "unable to open res7 file"; 
    out.close(); 
    parameters[i] = atof(input); 
    input = strtok(NULL, " "); 
    i++; 
     } 
     int rnum = rand(); 
     location = rnum%10000; 
     out.open("res7.txt", ios::out|ios::app); 
     if (out.is_open()) 
    out << "9\n"; 
     else 
    cout << "unable to open res7 file"; 
     out.close(); 
     Pop p(parameters[0], parameters[1], 0.2, 0.1, parameters[2], location); 
     out.open("res7.txt", ios::out|ios::app); 
     if (out.is_open()) 
    out << "10\n"; 
     else 
    cout << "unable to open res7 file"; 
     out.close(); 
     double L = exp(-parameters[3]); 
     double FF_mean = p.FF_steady(parameters[3], L, 0.0); 
     out.open("res7.txt", ios::out|ios::app); 
     if (out.is_open()) 
    out << "11\n"; 
     else 
    cout << "unable to open res7 file"; 
     out.close(); 
     out.open("res7.txt", ios::out|ios::app); 
     if (out.is_open()) { 
    out << "number of chromosomes is " << p.all_population[0]->ga->chromosome << " alpha is " << parameters[2] << " cost is " << parameters[1]; 
    out << " a is " << parameters[0] << " u is " << parameters[3] << " location is " << location << " FF steady is " << FF_mean << '\n'; 
     } 
     else 
    cout << "unable to open res7 file FF"; 
     out.close(); 
     out.open("res7.txt", ios::out|ios::app); 
     if (out.is_open()) 
    out << "12\n"; 
     else 
    cout << "unable to open res7 file"; 
     out.close(); 
     if (FF_mean>0.1) { 
    out.open("res7.txt", ios::out|ios::app); 
    if (out.is_open()) 
     out << "13\n"; 
    else 
     cout << "unable to open res7 file"; 
    out.close(); 
    p.insert_O(0.05); 
    out.open("res7.txt", ios::out|ios::app); 
    if (out.is_open()) 
     out << "14\n"; 
    else 
     cout << "unable to open res7 file"; 
    out.close(); 
    double total_mean = p.steady(parameters[3], L, 0.0); 
    double perc = p.perc_O(); 
    out.open("res7.txt", ios::out|ios::app); 
    if (out.is_open()) 
     out << "total steady is " << total_mean << " perc O is " << perc << '\n'; 
    else 
     cout << "unable to open res7 file"; 
    out.close(); 
     } 
    } 
    } 
    else 
    cout << "Unable to open input7 file\n"; 
    in.close(); 
    return 0; 
} 

Я не вижу выхода на консоли. Когда я запускаю код на другой машине, я вижу, что у меня есть исключение с плавающей запятой. Я думаю, я мог бы найти, где это, но я не понимаю, почему я не вижу никакого вывода или ошибки на консоли в cygwin. Может кто-нибудь мне помочь?

+3

Просьба указать код. –

+0

Кроме того, вы используете Cygwin gcc или что-то еще? – ak2

+0

'cout' вызывается только в том случае, если не удалось открыть файл res7? Работает ли 'printf()'? Попробуйте добавить 'cout <<" hello \ n ";' после 'printf()' (в основном где-то он всегда будет вызываться). – hmjd

ответ

0

Вы уверены, что код компилируется? На третьей-последней строке у вас есть

in.close();*/ 

Однако нет метки открытия/*. Я бы удостоверился, что ваш код действительно компилируется, и у вас есть новый .exe. Возможно, вы не можете скомпилировать и запустить старый .exe, у которого нет cout.

Кроме того, вы уверены, что исключений не исключено? Я не могу полностью проверить код, потому что мне не хватает источника для «Pop.h», поэтому я не могу полностью понять, какие ошибки я получу при компиляции. Не могли бы вы попытаться получить начальный cout в верхней части вашей программы и, возможно, предоставить soruce Pop.h, чтобы мы могли проверить его больше.

0

Во-первых, исключение может прекратить программу, даже не предоставив информацию. Cygwin - это попытка объединить окна и unix, и все не всегда работает отлично.

Во-вторых, std::cout - буферизованный поток. Таким образом, каждый operator<< добавляет в буфер до тех пор, пока буфер не заполнит и он будет автоматически выведен, вы явно очистите буфер с помощью cout.flush() или неявно промойте буфер, такой как std::endl.

Итак, вам нужно использовать отладчик или другую систему, чтобы найти ошибку и исправить ее. Вам также необходимо протестировать на нескольких платформах, так как все ведет себя по-разному по-разному - включая разные ошибки и даже появляется на работе.

Смежные вопросы