A problem with functions

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;
}

	
The system pause thing gave me a compile error. I've heard people say to never use system anything so maybe try something else to pause the system.

If you add #include <conio.h> at the top then you can use getch(); to pause a program until any key is pressed.

I made that change to your program and it compiles and runs now. I'm not sure if the output is right but it runs.

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
59
60
#include<iostream>
#include<fstream>
#include <conio.h>
using namespace std;

void letter(int &a);
void number( double &avg);

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;
    cout << "Press Any Key to exit: ";
    getch();  // press any key to continue
	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;
}
Last edited on
Try this:
In the function number(), try and use x = infile.get() , instead of cin>>x that should point you in the right direction.

Also, #include <cstdlib> // for system()
Topic archived. No new replies allowed.