-4

detailed modifications i need help doing to this code

i need help creating 7 functions to break this code into. By using pass by reference. i can't figure out how to pass the multidimensional arrays between functions.

#include <fstream>
#include <string>
#include <iomanip>
#include <iostream>
#include <cmath>
using namespace std;


int main()
{


const int courseSize = 3;
const int examSize = 6;
int n=0;
int strLength =0;
double scoreTotal =0;
char lettergrade;

string header, name, address, phone, social, course, studentID;
const int letterGrade_A = 90;
const int letterGrade_B = 80;
const int letterGrade_C = 70;
const int letterGrade_D = 60;

int age, yearsAtTXST, numstudents;
double testscore;
double numGrade = 0;
const int minStrLength = 1;
const int maxStrLength = 100;
const int minint = 1;
const int maxint = 100;
const int minCourse = 1;
const int maxCourse = 3;
const int minTest = 1;
const int maxTest = 5;
const double test1 = 0.10;
const double test2 = 0.15;
const double test3 =0.15;
const double test4 = 0.20;
const double final_test = 0.40;
const double maxGrade = 100.0, minGrade = 1.0;

//initialize arrays
string nonNumerical[studsize][strSize];
int numeric1[studsize][numSize];
double numeric2[studsize][courseSize][examSize];
char lettergrades[studsize][courseSize];

cout << fixed << showpoint << setprecision(2);

//opening files
ifstream inputFile;
inputFile.open("Project4_A04314548_Input.txt");
ofstream fout;
fout.open  ("Project4_A04314548_Output.txt");

if( !( inputFile && fout))
{
    cout << "Error opening file.\n";
}


cout << " Enter a number of students: ";
cin >> numstudents;

while( !(numstudents ==3))
{
    cout << " error number of students must be 3. Enter again: ";
    cin >> numstudents;
}

//take in string data from input file
for(int x =0; x< studsize; x++)
{
    for(int y=0; y < strSize; y++)
    {
       if(y >= strSize)
       break;

       getline(inputFile, nonNumerical[x][y]);
       strLength = nonNumerical[x][y].length();
       if(strLength < minStrLength || strLength > maxStrLength)
        {
        fout << " The given string for nonNumerical [" << x << "][" << y <<                                 

   "] is not within the proper range of 1-100 characters." << endl;
        fout << " Please fix the issue and rerun the program for a correct     

  output." << endl << endl;
        continue;
        }
    }
    }

 //above put into input data function

  //take in data from input file
 for(int x=0; x<studsize;x++)
 {
     for(int y=0; y < numSize; y++)
     {
         if(y>=numSize)
            break;

        inputFile >> numeric1[x][y];
        if( numeric1[x][y] < minint || numeric1[x][y] > maxint)
        {
        fout << "The given integer for numerical1[" << x << "][" << y <<   
  "]is not within the proper range of 1-100." << endl;
        fout << " please fix the problem and rerun the program for a correct      
   output." << endl << endl;
        break;
        }

     }
    }

  //above put into data function

   // take in double data from input file
   for( int x1=0; x1<studsize; x1++)
   {
    for(int x2=0; x2 < courseSize; x2++)
    {
        for(int x3=0; x3<(examSize-1);x3++)
        {
            if(x3 >=(examSize-1))
                break;
            inputFile >> numeric2[x1][x2][x3];
            if( numeric2[x1][x2][x3] < minGrade || numeric2[x1][x2][x3] >  
      maxGrade)
            {
                cout << "The given value fro numeric2[" << x1 << "][" << x2 
    << "][" << x3 << "] is not within the proper range of 1-100" << endl
                << "Please fix the issue and rerun the program for a correct   
    output." << endl;
                break;
            }

            else if( x3 == 0)
                scoreTotal += (numeric2[x1][x2][x3]* test1);
            else if( x3== 1)
                scoreTotal += (numeric2[x1][x2][x3]* test2);
            else if( x3 == 2)
                scoreTotal += (numeric2[x1][x2][x3]* test3);
            else if( x3 == 3)
                scoreTotal += (numeric2[x1][x2][x3]* test4);
            else
                scoreTotal += (numeric2[x1][x2][x3]* final_test);
            }

            numeric2[x1][x2][5] = scoreTotal; // final numeric grade
            scoreTotal = 0;

            if(x2 >=courseSize)
                break;
            if((numeric2[x1][x2][5] > maxGrade) || (numeric2[x1][x2][5] < 
   minGrade) )
            {
                cout << "Error in calculating grade. Please fix the issue   
   then rerun the program. ignore broken program!";
                cout << endl << endl;
                continue;

            }

            else if(numeric2[x1][x2][5] >= letterGrade_A)
                lettergrades[x1][x2] = 'A';
            else if(numeric2[x1][x2][5] >= letterGrade_B)
                lettergrades[x1][x2] = 'B';
            else if(numeric2[x1][x2][5] >= letterGrade_C)
                lettergrades[x1][x2] = 'C';
            else if(numeric2[x1][x2][5] >= letterGrade_D)
                lettergrades[x1][x2] = 'D';
            else
                lettergrades[x1][x2] = 'F';

     }
   }

    for(int a1=0; a1< studsize; a1++)
    {   fout << nonNumerical[a1][0]<< endl;
        fout << right << setw(35) << "Name of Student:\t";
        fout << nonNumerical[a1][1] << endl;
        fout << right << setw(35) << "Student ID:\t";
        fout << nonNumerical[a1][2]<< endl;
        fout << right << setw(35) << "Address:\t" ;
        fout << nonNumerical[a1][3] << endl;
        fout << right << setw(35) << "Telephone Number:\t";
        fout <<  nonNumerical[a1][4] << endl;
        fout << right << setw(35) << "Student Soc. Security:\t";
        fout << nonNumerical[a1][5] << endl;
        fout << right << setw(35) << "Age:\t";
        fout << numeric1[a1][0] << endl;
        fout << right << setw(35) << "Number of years at Texas State:\t";
        fout << numeric1 [a1][1] << endl << endl;




    for(int b1=0; b1 <courseSize; b1++)
    {
        fout << right << setw(35) << "Course number:\t";
            fout << nonNumerical[a1][(b1+6)] << endl;

        for(int c1= 0; c1 <(examSize-1); c1++)
        {
        fout << right << setw(32) << "Exam #" << (a1 +1) << ":\t";
            fout << numeric2[a1][b1][c1] << endl;
        }

        fout << right << setw(35) << "Numerical grade:\t";
        fout << numeric2[a1][b1][5] << endl;
        fout << right << setw(35) << "Letter grade:\t";
        fout << lettergrades[a1][b1] << endl;

        if( numeric2[a1][b1][5] < 70)
        {
        fout << right << setw(14) << " Warning Note: Your grade is too low   
   and needs improvements!" << endl << endl;
        }
        else if ( numeric2[a1][b1][5] >= 95)
        {
        fout << right << setw(14) << " Appreciation Note: Congratulations, 
  Your performance is Excellent!" << endl << endl;
        }
        else
        fout << endl;



    }
    fout << endl;

}

inputFile.close();
fout.close();

    return 0;
}
EGHM
  • 2,144
  • 23
  • 37

3 Answers3

1

Instead of using array

std::string nonNumerical[studsize][strSize];

use std::vector.

std::vector<std::vector<std::string>> nonNumerical(studsize, std::vector<std::string(strSize));

Then, you can divide your code into as many functions as you need and pass the vectors to the functions.

void function1(std::vector<std::vector<std::string>>& nonNumerical)
{
   ...
}

and use it from main as:

int main()
{
    ...

    std::vector<std::vector<std::string>> nonNumerical(studsize, std::vector<std::string(strSize));

    ...

    function1(nonNumerical);

    ...
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

first of all c++ send arrays by reference . your question is not clear ... but if I have get it right, you should send two dimensional arrays like this :

void f1(int a[][20]) ;

just the point is that , you must specify second dimension .

shayan
  • 361
  • 3
  • 14
-1

You can pass any size and dimension of array with pointers like this:

myfunction1(string *nonNumerical) 
{
    string something=nonNumerical[0][0];

}

But don't forget: You can't ask the array's size directly. You must pass the size with the array somehow.

Cleb
  • 25,102
  • 20
  • 116
  • 151
Bady
  • 56
  • 8
  • Good luck passing `string array[10][10];` into that function. – user4581301 Nov 17 '15 at 21:31
  • What is your problem with it? please Explain, if you started to criticize and downwote. – Bady Nov 18 '15 at 20:37
  • Ignoring the lack of a return type and assuming `string *` is meant to be `string **` because the alternatives are utterly wrong, `string array[10][10]` will not decay into `string **`, so you cannot make the function call. The best you can do is `void myfunction1(std::string nonNumerical[][10])`. A 1D array will decay to a pointer. A 2D will decay to a pointer to an array, not to a pointer (or a pointer to pointer). – user4581301 Nov 18 '15 at 22:22
  • I don't know, where you get this, but I use this formula a while, and working perfectly in one or two or three dimension arrays, without any problem. Only limitation is, you need to send the array correct size, like: `void myfunction(string *nonNumerical, int xsize, int ysize, int zsize)` – Bady Nov 19 '15 at 15:06
  • What compiler and version are you using? This has been the law of the land for a very long time. – user4581301 Nov 19 '15 at 21:00
  • I use Visual studio 2010 Express, and c++11. – Bady Nov 19 '15 at 22:06
  • VS2010 pro: function call 'myfunction1' : cannot convert parameter 1 from 'std::string [10][10]' to 'std::string *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast` `string something=nonNumerical[0][0];`: 'initializing' : cannot convert from 'char' to 'std::basic_string<_Elem,_Traits,_Ax>' Which makes sense. The first [0] indexes the `string *` and the second [0] accesses the string and retrieves the first character. The only way to make this work is with some really shady casting. – user4581301 Nov 19 '15 at 22:45
  • because you need to pass the first element of the array like: `myfunction1(array[0][0],10,10)` . You now, pointers. It's easy, you pass the start point of the array, and go trough it. Or, maybe you never heard about them? – Bady Nov 21 '15 at 22:23
  • Stop and think about what you are doing here. You are discarding the type checking and then trying to put it back together again inside the function. This gives you extra opportunities to get the typing wrong. And you are getting it wrong `array[0][0]` needs to be`&array[0][0]` to do what you want. Then inside the function `nonNumerical` is off by a level of indirection. It is a `string *` not a `string **` so `nonNumerical[0]` is a string and `nonNumerical[0][0]` is a character. This will compile with a warning, but the result is wrong. – user4581301 Nov 22 '15 at 01:44
  • It working perfectly in my code, I can convert arryas to vectors with it. Maybe my machine is wrong, and if You define String array[10][10] then you get an one dimensional aray each of element size 10 caracter, and only my pc make a two dimensional array. After all, he want to pass a multidimensional array to an another function. I show them one solution. It a little bit dangerous? Maybe, but if he pay attention a little bit, it will work fine. but, all right, You win. I'm not going to help anybody ever. – Bady Nov 22 '15 at 15:32