這是我的程序。它應該佔用2個分數,並在建模結構時將它們加在一起。我遇到了一些麻煩。我收到了一些錯誤消息。複製這段代碼,看看你能否爲我正確調試它並解釋你做了什麼。我很困難!非常感謝你!在分數方案中使用結構有問題
#include <cstdlib>
#include <iostream>
#include <math.h>
/*
Name: Fraction
Author: Grant Birkinbine
Date: 13/10/14 17:33
Description: Takes two fractions and outputs them in different ways
*/
using namespace std;
//Struct
struct fraction{
int numer, denom ;
};
//Function to print fractions
void displayfractionadd (fraction a) {
cout << "Addition: " << add.numer << "/" << add.denom << endl;
}
//Function to get fraction info
fraction newfraction() {
fraction a1;
cout << "Enter numerator: " ;
cin >> a1.numer ;
cout << "Enter denomerator: " ;
cin >> a1.denom ;
return a1;
}
//Function to add fractions
fraction addition(fraction a1, fraction a2) {
fraction add;
add.denom = (a1.denom * a2.denom) ;
add.numer = a1.numer * a2.denom + a1.denom * a2.numer;
return add;
}
//Main
int main(int argc, char *argv[])
{
cout << "Fraction 1 = " ;
cout << endl;
fraction fraction1 = newfraction();
fraction fraction2 = newfraction();
cout << endl;
cout << "Solutions: " ;
fraction newadd = addition(fraction a1, fraction a2) ;
cout << endl;
displayfractionadd (fraction a);
system("PAUSE");
return EXIT_SUCCESS;
}
如果您收到錯誤消息,請發帖。 – lxg 2014-10-19 23:05:02
'複製這段代碼,看看你能否正確地爲我調試它'怎麼樣?包括你的錯誤信息。至少告訴我們它們是運行時還是編譯時間... – John3136 2014-10-19 23:17:58
我得到的錯誤是編譯器錯誤。我在第23行得到一個錯誤,聲明'add'未聲明。第58行的錯誤指出'a1'和'a2'未聲明。第60行還有一個錯誤,指出'a'也未聲明。 – Alphabert 2014-10-20 00:32:47