2015-02-10 44 views
-2
//*********************************************************** 

#include <iostream> 
#include <iomanip> 
#include <cstdlib> 

// function prototypes 
void intOutput(); 
void floatingPointOutput(); 
void intMathOperations(int rows, int b, int width); // int math demonstration 
void writeHeaderLine(int width); 
void writeMathLine(int a, int b, int width); 

using namespace std; 

int main() 
{ 
int a, b, width, rows; 

cout << "\nProject 1: Math and Functions"; 
cout << "\n"; 
cout << "\n"; 
cout << "\nProject 1 Start."; 
cout << "\nZack Cunningham"; 
cout << "\n"; 
cout << "\nInteger Output Demo:"; 
cout << "\n"; 

intOutput(); 
floatingPointOutput(); 
intMathOperations(rows, b, width); // int math demonstration 
writeHeaderLine(width); 
writeMathLine(a, b, width); 

cout << "\n"; 
cout << "\nProject 1 End."; 
cout << "\n"; 

const int FIELD_WIDTH = 10; 
intMathOperations(12, 5, FIELD_WIDTH); 

return EXIT_SUCCESS; 
} 

void intMathOperations(int rows, int b, int width){ 
cout << "\n"; 
cout << "\nInteger Math Operations Demo:"; 
cout << "\n"; 
writeHeaderLine(width); 
cout << "\n"; 
for (int a = 0; a < rows; ++a){writeMathLine(a, b, width); 
} 
} 


void writeHeaderLine(int width){ 
cout << "\n"; 
cout << setw(width) << "a"; 
cout << setw(width) << "b"; 
cout << setw(width) << "a * b"; 
cout << setw(width) << "a/b"; 
cout << setw(width)<< "a % b"; 
} 

void writeMathLine(int a, int b, int width){ 
cout << setw(width) << a; 
int rows; 
for (int a = 0; a < rows; ++a){writeMathLine(a, b, width); 
} 
} 

void floatingPointOutput(){ 
double a = 2000; 
double b = 3; 
double c = a/b; 
cout << "\n" << a << "/" << b << " = "; 
cout << "\n" << c; 

cout << setprecision(10); 
cout << "\n" << setw(20) << c; 
cout << scientific; // scientific notation 
cout << "\n" << setw(20) << c; 
cout << fixed; // standard decimal notation 
cout << "\n" << setw(20)<< c; 
cout << left; // left justify 
cout << "\n" << setw(20) << c; 
cout << right; 

// right justify (default) 
cout << "\n" << setw(20) << c; 
cout << setprecision(6); // return to default 
cout << "\n" << setw(20) << c; 
cout << "\n"; 
} 

// function calls 
void intOutput(){ 
cout << "\nInteger Output Demo:"; 
cout << "\n"; 
int a = 12; 
int b = 12345678; 
cout << "\n....5...10...15...20"; // spacing info 
cout << "\n"; 
cout << "\n" << setw(20) << a; 
cout << "\n" << setw(20) << b; 
cout << "\n"; 
cout << "\n" << setw(4) << a; 
cout << "\n" << setw(4) << b; 
cout << left; // left justified 
cout << "\n"; 
cout << "\n" << setw(20) << a; 
cout << "\n" << setw(20) << b; 
cout << right; // right (default) justified 
cout << "\n"; 
} 

這是我所有的程序代碼,而且我已經完成了它的工作。最後沒有錯誤,但它是一個空白的程序,只是在無限循環中運行。我不確定該怎麼做,如果有人能幫忙,我會很感激。謝謝。程序運行時出現空白窗口?

+2

歡迎來到StackOverflow!我建議在發佈之前儘可能簡化您的程序,並確保您的縮進是正確的。你也應該澄清你是一個空白程序的意思。你真的認爲它不輸出任何東西,包括「項目1:數學和函數」? – 2015-02-10 20:46:01

+0

請僅包含與您的問題相關的代碼,沒有人真的想要通讀您的整個程序 – 2015-02-10 20:46:16

+1

您可能想要提高編譯器的警告級別,並注意未初始化的變量警告。 – 2015-02-10 20:46:21

回答

2

至少你需要解決這個問題。您定義的變量rows

int a, b, width, rows; 

但是你沒有初始化rows,你用它在這裏,

intMathOperations(rows, b, width); // int math demonstration 

其在for循環的結束條件使用rows。這是個問題。

爲了解決這個問題,我建議你打開所有的警告這樣

g++ -Wall your_code.cpp 

並確保你瞭解每一個警告你並修復必要的。

+0

他沒有初始化'b'和'width',他只創建局部變量 – Machtl 2015-02-10 20:54:04

+0

這是其中之一編譯器能夠指出的很多問題。 – 2015-02-10 20:55:37

+0

是的,你是對的,還有幾個變量需要初始化。 – 2015-02-10 20:55:52

相關問題