2011-01-06 16 views
13

Jestem nowy w programowaniu i próbowaniu napisania nowego programu. Podczas sprawdzania przez mój program zwracany jest kod błędu 1. # QNAN. Próbowałem wyizolować zmienną i szukałem odpowiedzi, ale nie znalazłem żadnych rozwiązań.1. # QNAN error C++

Mój kod:

// This is a program to decide what culvert should be properly used for a specific installation 
// using given measurements and data 
// 
#include <cstdio> 
#include <cstdlib> 
#include <iostream> 
#include <string> 

using namespace std; 
// initializing functions 
double slope_function(); 
double cbasin(); 
// initializing classes: Subdivisions specs 

//intitializing global variables 
double edge_road =0; 
double up_stream =0; 
double down_stream =0; 
double tbm =0.0; 

//double culv_length =0; 
double slope = 0.0 ; 
char street_name[1001]; 
int min_culv = 15; 
double up_strm_culv =0; 
double dwn_strm_culv =0; 


int main (int nNumberofArgs, char* pszArgs[]) 
{ 
    cout<< "This program will allow the surveyor to double check their calculations\n"; 
    cout << "in deciding what size, type, and requirements are allowed for the\n"; 
    cout << "installation of culverts in Terrebonne Parish.\n\n"; 



// begin input 
    cout << "what is the name of the street\nwhere the culverts will be installed: "; 
    cin.getline (street_name,1000); 
    cout << endl; 
    cout << "What is the Benchmark: "; 
    cin >> tbm; 
    cout << endl; 
    cout << "What is the elevation of the edge of the road: "; 
    cin >> edge_road; 
    cout << endl; 
    cout << "What is the up-stream culvert size: "; 
    cin >> up_strm_culv; 
    cout << endl; 
    cout << "What is the culverts up-stream inverted elevation: "; 
    cin >> up_stream; 
    cout << endl; 
    cout << "What is the down-stream culvert size: "; 
    cin >> dwn_strm_culv; 
    cout << endl; 
    cout << "What is the culverts down-stream inverted elevation: "; 
    cin >> down_stream; 
    cout << endl; 
    cout << "What is the length of culvert requested: "; 
    cin >> culv_length; 


    cout << "Your slope is : "; 
    cout << slope_function(); 
    cout << endl; 
    cout << street_name; 
    cout << endl; 
    cout << cbasin(); 


    cout << endl; 
    // wait until user is ready before terminating program 
    // to allow the user to see the program results 
    system ("pause"); 
    return 0; 
} 

// slope function 
double slope_function() 
{ 
    double riseoverrun = 0.0; 
    slope = (up_stream - down_stream)/ culv_length; 
    return slope; 
} 


// Catch Basin function 
double cbasin () 
{ 
    double cb = 0; 
    cb = culv_length/60; 
    cout << endl; 
    cout << "You need "; 
    cout << cb; 
    cout << " catch basins for this job."; 
    cout << endl; 
} 
+1

Kod, który połączyłeś, nie został skompilowany. Ale nawet jeśli to działało, prawdopodobnie dzielisz 0 na 0, co nie jest dozwolone. –

+1

Podałem opis NAN, ale twój aktualny błąd to funkcja, która zwraca podwójną wartość, ale nie ma instrukcji return. – CashCow

Odpowiedz

42

1 # QNAN jest reprezentacją ciąg do "cichej NAN". "NAN" to "nie-liczba" i dotyczy tylko jednostek pływających i podwójnych.

Sieci NAN mogą być bardzo przydatne do reprezentowania wartości "zerowych" (zamiast wybierania prawdziwej liczby i liczenia na to, że numer ten nie będzie Ci potrzebny z uwagi na jego naturalne znaczenie).

Niektóre operacje matematyczne mogą zwrócić NAN, jeśli operacja jest "nieprawidłowa" (np. Wykonanie dziennika o wartości ujemnej).

Można wygenerować QNAN z C++ przy użyciu

double d = std::numeric_limits<double>::quiet_NaN(); 

Każda operacja porównania (==, < = etc) na NAN zwraca false, nawet porównując jego równość sama, z wyjątkiem! = Które zawsze zwraca prawda (nawet w porównaniu do samego siebie).

(Faktyczny błąd w kodzie wydaje się być funkcją, która zwraca wartość podwójną, ale nie zawiera instrukcji return).

+1

Dobra funkcja do określania, czy wartość jest NAN, to: 'szablon bool is_ieee_nan (const VAL i v) {return! (V == v); } ' –

+0

Skąd czerpiesz'! = 'Zawsze zwracając true? –

+0

Brzmi jak bardzo dziwna i bezużyteczna funkcja. Jeśli nie możesz wykryć NAN, to co to jest? –

Powiązane problemy