converting four programs into four functions

closed account (37poizwU)
Here is the problem :

Using FUNCTIONS,combine all four given programs into a single program.
Each part should have its own function (basically you’re converting FOUR programs into FOUR functions). The different functions should be accessible through a menu that resembles the following:
1) Array Review
2) If-Else Review
3) Loop Review 1
4) Loop Review 2
Choose a number:
If you enter the number 1, for example, it should read 5 numbers from numbers.txt and calculate the average (part 1 of the in-class assignment).

Here is what I have so far :

#include<iostream>
#include<fstream>
using namespace std;

//Function prototypes
float getAverage ();
int getScore ();
int getSum ();
int getFactorial();

int main() {


int arrayReview,IfelseReview, loopReview1, loopReview2 ;

arrayReview = 1;
IfelseReview = 2;
loopReview1 = 3;
loopReview2 = 4;


// Dispaly the menu and get a choice.

cout << "1)Array Review\n";

cout << "2)If-Else Review\n";

cout << "3) Loop Review 1\n";

cout << "4) loop Review 2 \n\n";

cout << "choose a number:\n " ;
cin >> arrayReview >> IfelseReview >> loopReview1 >> loopReview2;

return 0;
}

float getAverage ()

{
int num[5];
int temp = 0;
float average = 0;



ifstream infile;


infile.open("numbers.txt");



for(int i = 0; i < 5; i++) {


infile >> num[i];

temp += num[i];


average = temp / 5.0;

cout << "The average is: " << average;

infile.close();
return average;

}

}
int getScore ()
{ int score;

cout << "Enter score: ";


if(score >= 90 && score <= 100)
cout << "Grade: A" << endl;
else if(score >= 80 && score < 90)
cout << "Grade: B" << endl;
else if(score >= 70 && score < 80)
cout << "Grade: C" << endl;
else if(score >= 60 && score < 70)
cout << "Grade: D" << endl;
else if(score >= 0 && score < 60)
cout << "Grade: F" << endl;
else
cout << "INVALID SCORE" << endl;

return score;

}

int sum ()

{
int num;
int sum = 0;


for(int i = 0; i < 5; i++) {
cin >> num;
sum += num;

cout << "Sum: " << sum;
return sum ;
}
}

int getFactorial()

{
int num;
int factorial = 1;
ofstream outfile;

outfile.open("factorial.txt");

cout << "Enter number: ";
cin >> num;

outfile << num << "!" << endl;

while(num != 0) {
factorial *= num;
outfile << num;

if(num != 1)
outfile << " x ";

num--;
}

outfile << " = " << factorial;

outfile.close();
return factorial ;

}


My program has no errors. Can anybody gave me some input on what's missing in my program , in order to get the correct outputs. Thanks!


for example : If you enter the number 1, for example, it should read 5 numbers from numbers.txt and calculate the average (part 1 of the in-class assignment).


Last edited on
How can your program be error free if its not returning the correct results. You mean it compiles right? Do you know the files are opening correctly?
What average do/should you get when you enter 1? Do any of the 4 functions return a correct result?
Just a suggestion:
Why not use switch(case) and put your functions in each case block just as a temporary simplification
Topic archived. No new replies allowed.