In this C++ program the function print_nums
doesn't accept
the vector<double>
numbers as an actual prameter when called in the switch_function
and I wanted the add_num
function to accept vector as an argument but I think it will cause the same error!
note that the error mentioned doesn't appear in this code but many errors and warnings appear when I compile this code and when I change double min
to double minn
or double max
to double maxx
I'm lift with the mentioned error, by the way I'm still a beginner in C++ so please explain why I can't use the words min and max ,also the main error of the function print_nums
and any other error or warning.
#include <iostream>
#include <vector>
#include <climits>
#include <cfloat>
using namespace std;
double mean{};
double min=FLT_MAX;
double max=FLT_MIN;
vector<double>numbers{};
char select;
char list();
void print_nums(vector<double>&numbers);
void program_func();
double nums_mean(double);
char switch_function(char);
void display_nums_mean(double);
int main(){
program_func();
return 0;
}
void program_func(){
while(select!='q'&&select!='Q'){
list();
switch_function(select);
}
}
char list(){
cout<<"Please select an Order\n";
cout<<"***********************\n";
cout<<"P-Print numbers\n";
cout<<"A-Add a number\n";
cout<<"M-Display the numbers mean\n";
cout<<"S-Display smallest number\n";
cout<<"L-Display largest number\n";
cout<<"Q-Quit\n";
cout<<"Order " ;
cin>>select;
cout<<endl;
return select;
}
void print_nums(const vector <double> &numbers){
if(numbers.size()!=0)
for(auto value : numbers)
cout<<value<<endl;
else
cout<<"Array is empty"<<endl;
}
void add_num(){
cout<<"How many numbers will be added to the list : ";
unsigned int num_of_nums;
double num;
cin>>num_of_nums;
cout<<endl;
cout<<"add numbers to the list : ";
for(unsigned int i=0;i<num_of_nums;i++){
cin>>num;
numbers.push_back(num);
}
return;
}
double nums_mean(double mean){
double sum{};
for(unsigned int i=0;i<numbers.size();++i){
sum+=numbers.at(i);
}
mean = sum/(numbers.size());
return mean;
}
double small_num(double min){
for(unsigned int i=0;i<numbers.size();++i){
if(min>=numbers.at(i)){
min = numbers.at(i);
}
}
return min;
}
double large_num(double max){
for(unsigned int i=0;i<numbers.size();++i){
if(max<=numbers.at(i)){
max = numbers.at(i);
}
}
return max;
}
void display_nums_mean(double mean){
cout<<"The mean of the numbers is : "<<mean<<endl;
}
void display_nums_min(double min){
cout<<"The minimum number is : "<<min<<endl;
}
void display_nums_max(double max){
cout<<"The maximum number is : "<<max<<endl;
return;
}
char switch_function(char select){
switch(select){
case 'p':
case 'P':
print_nums(numbers);
break;
case 'a':
case 'A':
add_num();
break;
case 'm':
case 'M':
mean=nums_mean(mean);
display_nums_mean(mean);
break;
case 's':
case 'S':
min=small_num(min);
display_nums_min(min);
break;
case 'l':
case 'L':
max=large_num(max);
display_nums_max(max);
break;
case 'q':
case 'Q':
return select;
default:
cout<<"Please Enter a valid character "<<endl;
}
return select;
}