我終於解決了並通過鏈接數組來通過strcmp函數和比較數組值。感謝你的幫助!!希望你們可以參考我的問題和帖子中列出的答案。如何通過函數鏈接數組值C++
向所有相關人員發表意見和建議。欣賞它。
我的解決方案是通過使用二維數組 - 字符而不是字符串+使用strcmp函數來比較值集以達到Lab7的目標。
最佳,
MM
// Marcus Moo Lab 7.cpp
// Full Time Student
// No plagarism
#include <iostream>
#include <string>
using namespace std;
// Global Declarations
const float pointLaw[11] = { 5.0,5.0,4.5,4.0,3.5,3.0,2.5,2.0,1.5,1.0,0.0 };
const char gradeLaw[11][3] = { "A+","A","A-","B+","B","B-","C+","C","D+","D","F" };
// Convert Grade to Point
float gradeToPoint(const char grade[]);
int main()
{
// Header
cout << "\tWelcome to University Of Wollongong" << endl;
cout << "Grade Point Consultation System" << endl;
cout << endl;
// Defining moments
char grade[3];
float outcome;
char intent;
int count;
// Purpose of consultation system
do
{
cout << "Enter your grade: ";
cin >> grade;
outcome = gradeToPoint(grade);
if (outcome == -1) {
cout << "Invalid Grade" << endl;
}
else {
cout << "Your grade point is " << outcome << endl;
}
cout << "Continue?: ";
cin >> intent;
cout << endl;
} while ((intent == 'y') || (intent == 'Y'));
// The end
cout << "All the best" << endl;
}
float gradeToPoint(const char grade[])
{
int count;
float points = -1;
for (count = 0; count < 12; count++)
{
if (strcmp(gradeLaw[count], grade) == 0) {
points = pointLaw[count];
cout << gradeLaw[count] << " = " << grade << endl;
}
}
return points;
}
使用['std :: map'](http://en.cppreference.com/w/cpp/container/map) – NathanOliver
@NathanOliver不是在學校教std :: map? 還有什麼其他方法? –
@NathanOliver無論如何你爲什麼低調回答這個問題?儘管如此,我試圖以更普遍的方式提出這個問題。 –