Hello ,write a complete C++ program that contains two functions:
Letters( ): that read 10 letters from file and return the number of upper ones.
Numbers(): that read 10 numbers from file and return their average.
In main, call these two functions to print the number of upper letters and average of numbers.
Note: - Pass the file stream to both functions by reference
- Then try to define stream as global
(in.txt)
and this is the file that i want to read A b c d e f r e r I
78 95 74 85 96 78 85 96 85 74
but it dose not work , the program reads only the character and do not read the
numbers .. if any one can help me . Thanks a lot .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#include<iostream>
#include<fstream>
void letter(int &a);
void number( double &avg);
using namespace std;
int main ()
{
ifstream infile;
int x;
double avg;
letter(x);
number(avg);
cout<<"the number of upeer ones is "<<x<<endl;
cout<<"the average of the 10 number is "<<avg<<endl;
system("pause");
return 0;
}
void letter(int &a)
{
char b;
ifstream infile;
infile.open("in.txt");
int i=1;int up=0;
while(i<=10)
{
infile>>b;
if((b>='A')&&(b<='Z'))
up++;
i++;
}
a=up;
}
void number( double &avg)
{
int x;
ifstream infile;
infile.open("in.txt");
int i=1;
int sum=0;
while (i<=10){
infile>>x;
sum=sum+x;
i++;
}
avg=sum/10.0;
}
|