vendredi 1 avril 2016

C++ How to pass arrays to function then have it called to display the values of the arrays?

Hello everyone Im having a very hard problem doing what I need this code to do for my c++ assignment!

The desire I want my code to do is have the function InputValue() fill up the arrays I have made; Which it does perfectly! Then pass those arrays to a function called displayinfo() which will display the contents of the array when called in the main() function!

The problem I have ran into is when I try to call on displayinfo(sem1array) with the array I want to pass ultimately all of them, it comes up with a syntax error!

How should I pass the contents of the various arrays sem1array,sem1coursetypes, sem1coursetitles,... into the function that displays the contents and then how should I call on the function to display the contents within main() function?

And another huge question I have is; Is there another way of writing or declaring an array other than array (right open arrow)char,7(left open arrow) sem1array; like the way I have declared all the arrays in my code?

I have put stars on all the parts where i'm having the problem! I left the other code in so you see how the arrays are filled and what i'm working with.

#include<iostream>
#include<array>
#include<iomanip>
#include<string>
using namespace std;

void setCourseName(string);
void InputValue();


int main()
{
    InputValue();

    ***displayinfo();***



}

void InputValue()
{
    char semester;
    string courseTitle;
    string courseType;
    int credits;
    string lettergrade;
    double gpa;

    array<char, 7> sem1array;
    array<string, 7> sem1coursetitles;
    array<string, 7> sem1courseTypes;
    array<int, 7> sem1credits;
    array<string, 7> sem1lettergrades;


    for (int counter = 0; counter < sem1array.size(); counter++)
    {
        bool valid1 = false;
        bool validCredits = false;
        bool valid3 = false;
        bool valid4 = false;
        while (valid1 == false)
        {
            cout << "Enter Semester Number" << endl;
            cin >> semester;
            if (semester == '1' || semester == '2')
            {
                cout << "valid input" << endl;
                sem1array[counter] = semester;
                valid1 = true;
            }
            else
            {

                cout << "Invalid Input! RE ENTER SEMESTER NUMBER EITHER 1 OR 2!" << endl;
            }
        }
            cout << "Enter Course Title: " << endl;
            cin.ignore();
            getline(cin, courseTitle);
            if(courseTitle.size() <= 25)
                sem1coursetitles[counter] = courseTitle;
            else
            {
                cout << "Course Name Cannot Be More Than 25 Characters; Course name limited to first 25 characters!" << endl;
                courseTitle = courseTitle.substr(0, 25);
                sem1coursetitles[counter] = courseTitle;
            }

        while (valid3 == false)
        {
            cout << "Enter Course Type: Regular, AP, or Honors" << endl;

            getline(cin, courseType);
            if (courseType == "Regular" || courseType == "AP" || courseType == "Honors")
            {
                sem1courseTypes[counter] = courseType;
                valid3 = true;
            }
            else
            {
                cout << "Invalid Input! RE ENTER COURSE TYPE EXACTLY HOW IT APPEARS EITHER Regular, AP, or Honors!" << endl;
            }
        }
        while(validCredits == false)
        {
            cout << "Enter Credits Earned For Course: **Can Either Be 1-4 Credits**" << endl;
            cin >> credits;
            if (credits == 1 || credits == 2 || credits == 3 || credits == 4)
            {
                sem1credits[counter] = credits;
                validCredits = true;
            }
            else
            {
                cout << "Invalid Output! Must Enter Number 1-4 For Credits Earned!" << endl;
            }
        }
        while (valid4 == false)
        {
            cout << "Enter Letter Grade; Capital Letter Followed By A Plus + or Minus - If There Is One!Example: A+, A-, A, B+..." << endl;
            cin.ignore();
            getline(cin, lettergrade);
            if (lettergrade == "A+")
            {
                gpa = 4.0;
                sem1lettergrades[counter] = lettergrade;
                valid4 = true;
            }
            else if (lettergrade == "A")
            {
                gpa = 4.0;
                sem1lettergrades[counter] = lettergrade;
                valid4 = true;
            }
            else if (lettergrade == "A-")
            {
                gpa = 3.7;
                sem1lettergrades[counter] = lettergrade;
                valid4 = true;
            }
            else if (lettergrade == "B+")
            {
                gpa = 3.3;
                sem1lettergrades[counter] = lettergrade;
                valid4 = true;
            }
            else if (lettergrade == "B")
            {
                gpa = 3.0;
                sem1lettergrades[counter] = lettergrade;
                valid4 = true;
            }
            else if (lettergrade == "B-")
            {
                gpa = 2.7;
                sem1lettergrades[counter] = lettergrade;
                valid4 = true;
            }
            else if (lettergrade == "C+")
            {
                gpa = 2.3;
                sem1lettergrades[counter] = lettergrade;
                valid4 = true;
            }
            else if (lettergrade == "C")
            {
                gpa = 2.0;
                sem1lettergrades[counter] = lettergrade;
                valid4 = true;
            }
            else if (lettergrade == "C-")
            {
                gpa = 1.7;
                sem1lettergrades[counter] = lettergrade;
                valid4 = true;
            }
            else if (lettergrade == "D+")
            {
                gpa = 1.3;
                sem1lettergrades[counter] = lettergrade;
                valid4 = true;
            }
            else if (lettergrade == "D")
            {
                gpa = 1.0;
                sem1lettergrades[counter] = lettergrade;
                valid4 = true;
            }
            else if (lettergrade == "F")
            {
                gpa = 0.0;
                sem1lettergrades[counter] = lettergrade;
                valid4 = true;
            }

            else
            {
                cout << "Invalid Input! Please Re-Enter Letter Grade! Example: A+, A-, A, B+..." << endl;
            }
        }
    }

    ***displayinfo(sem1array);***

}
***void displayinfo(char b[])***
{
    for (int k = 0; k < ***sem1array.size()***; k++)
    {
        cout << "Semester: " << sem1array[k] << "           " << "Course: " << sem1coursetitles[k] << endl;

    }

Aucun commentaire:

Enregistrer un commentaire