税率
我设法使该程序从输入文件中读取并正确显示给定主题中最便宜的教科书的费用,但是却给我错误地显示了哪本教科书最便宜。该程序始终说textbook1最便宜,即使textbook2或textbook3最便宜。我感觉我可能不正确地传递了参数。下面是我的代码和程序输出的附件。任何帮助都将不胜感激。谢谢。
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
void print_output(int text_num, double t1, double t2, double t3, double tot,
int num, double cheap_one);
using namespace std;
int main() {
// define variables
string store_name;
string subject;
// define variables
ifstream inData;
ofstream outData;
string Biology, Chemistry, English, Computer, Mathematics;
double text1, text2, text3;
double total1;
double small;
double text_num = 1;
double num = 1;
cout << fixed << showpoint << setprecision(2);
// print titles here like dereks bookstore and the subjects plus
text/cheapest
cout << "Derek's Bookstore" << endl;
cout << endl;
cout << "Subject\t" << setw(5) << " Text 1\t" << "Text 2\t" << "Text 3\t"
<< "Total\t" <<
" Cheapest/Amount\t" << endl;
cout << endl;
inData.open("first_project_data.txt");
if (!inData) {
cout << "\nCannot open input file." << endl;
system("PAUSE");
return 0;
}
inData >> subject;
while (inData) {
//cout << "\n\n**at beginning" << subject << endl << endl;
inData >> text1 >> text2 >> text3;
// calculate totals
total1 = text1 + text2 + text3;
// find out the cheapest book (use if statement )
small = text1;
if (text1 > text2)
small = text2;
if (small > text3)
small = text3;
// call the print function
print_output(text_num, text1, text2, text3, total1, num, small);
text_num++;
inData >> subject;
}
// output the last total line
cout << "Totals " << "100.00" << "\t" << "105.00" << " 110.00" << "
315.00" << " ****N/A****" << endl;
cout << endl;
//close files
inData.close();
outData.close();
system("PAUSE");
return 0;
}
void print_output(int subject, double t1, double t2, double t3, double tot,
int num, double cheap_one) {
char text_name[9], subject_name[12];
switch (subject) {
case 1: strcpy_s(subject_name, "Biology");
break;
case 2: strcpy_s(subject_name, "Chemistry");
break;
case 3: strcpy_s(subject_name, "English");
break;
case 4: strcpy_s(subject_name, "Computer");
break;
case 5: strcpy_s(subject_name, "Mathematics");
break;
}
switch (num) {
case 1: strcpy_s(text_name, "text1");
break;
case 2: strcpy_s(text_name, "text2");
break;
case 3: strcpy_s(text_name, "text3");
break;
}
cout << setw(12) << left << subject_name << t1 << "\t" << t2 << "\t" << t3
<< "\t" << tot << "\t\t"
<< text_name << "/$ " << cheap_one << endl;
cout << endl;
}
答案 0 :(得分:0)
街区
if (text1 > text2)
small = text2;
if (small > text3)
small = text3;
还需要更新num
,否则您将始终传递num = 1
,并且switch(num)
中的print_output
语句将始终选择case 1
。您可以通过将上面的块更改为
if (text1 > text2) {
small = text2;
num = 2;
}
if (small > text3) {
small = text3;
num = 3;
}