files

Write your question here.

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
#include <iostream>
#include <fstream>
using namespace std;

bool searchdig(int x,int number)
{
	bool flag;
	while(x!=0)
	{
		int temp=x%10;
		if(temp==number)
		{
			cout<<x;
			flag=true;
		}
		x/=10;
	}
	return flag;
}
int mostcommon(int x,int number)
{
	int mostcommon=0;
	int temp=number;
	if(number>temp)
		int mostcommon=x;
	return mostcommon;
}
int main()
{
	ifstream F1("q13.dat");
	int dig=0;
	int num=0;
	int counter=0;
	while (F1.is_open())
	{
		cin>>dig;
		while(!F1.eof())
		{
			F1>>num;
			if(searchdig(num,dig))
			{
				counter++;
			}
		}
		int mostcommon(num,counter)
		{



		



i have two missions here:
1. to print the numbers in the file which the "dig" i entered is in the number.

2. to print the numbers with the most common dig.

i don't really know how to do so..(2)

there is advice to use helping functions
I don't understand your problem.
Also: your main function ends somewhere in the middle, is the rest not important?


i don't really know how to do so..(2)

You don't know how to do what exactly?
We certainly won't do the work for you, we want to help you solve your problem yourself.
So, where are you stuck?
@Gamer2015

I think this is what he's saying here.

I need to do 2 things:
1. Read a file and print all numbers that contain digit 'X', which is input from user. (Already done)

2. Read a file, find the most common digit 'X' in the file, and print all numbers in the file that contain digit 'X'.

I need help with #2.


@davidm

For #1, you forgot to actually output the number, you just increment counter, which doesn't seem like what you're supposed to do either.

For #2, you should have an array of size 10, that will keep track of the number of times each digit occurs in the file. After you've finished reading the file, see which digit 'X' occurs the most by checking the array.

Use clear() and seekg() to go back to the beginning of the file. Read in the numbers and use your first function to check if 'X' is in the number. If it is, print it out.

http://www.cplusplus.com/reference/ios/ios/clear/
http://www.cplusplus.com/reference/istream/istream/seekg/
Last edited on
fg109

Thanks that exactly what i wanted to ask really.

but who says the array size is 10 it can be bigger\smaller.

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
#include <iostream>
#include <fstream>
using namespace std;



const int capacity=10000;
int findig(int x,int num)//function for first assignment.
{
	int count = 0;
	while(x!=0)
	{
		int temp=x%10;
		if(num==temp)
		{
			cout<<num;
			count++;
		}
		x/=10;
	}
	return count;
}
int *mostcommon(int count)
{
	int *arr=new int[capacity];
	*(arr++)=count;
	return arr;
}
int main()
{
	ifstream F1("q13.dat");
    int *arr=new int[capacity];
	int dig=0;
	int number=0;
	int counter=0;
	int arrlenth=0;
	int mostcom=0;
while(F1.is_open())
{
	while(!F1.eof())
	{
		cin>>dig;
		F1>>number;
		counter=findig(number,dig);  
	}



as you see i am steel trying to find the way ..


For #1, you forgot to actually output the number, you just increment counter, which doesn't seem like what you're supposed to do either.


look at that..

bool searchdig(int x,int number)
{
bool flag;
while(x!=0)
{
int temp=x%10;
if(temp==number)
{
cout<<x;
flag=true;
}
x/=10;
}
return flag;
}
Last edited on
@davidm

The array is size 10 because there are only 10 possible digits (0123456789). And sorry, I didn't notice your output in the function, but I suggest you have that inside main instead (or just return true instead of setting flag=true). Otherwise, if I was looking for the digit 7 and had the number 717, it would output twice.
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
#include <iostream>
#include <fstream>
using namespace std;

int findig(int x,int num)//function for first assignment.
{
	int count = 0;
	while(x!=0)
	{
		int temp=x%10;
		if(num==temp)
		{
			cout<<num;
			count++;
		}
		x/=10;
	}
	return count;
}
int main()
{
	ifstream F1("q13.dat");
   int arr[10];
   int i=0;
	int dig=0;
	int number=0;
	int counter=0;
	int mostcom=0;
for(int i=0;i<10;i++)
{
	cin>>dig;
	while(!F1.eof())
	{
		F1>>number;
		counter=findig(number,dig);  
	}
	arr[i]=counter;
	i++;
	counter=0;
}
for(int i=0;i<10;i++)//it works counting on that my inpute is 0123456789
{
	if(arr[i]>arr[i+1])
		mostcom=i;
}
F1.seekg(0,ios::beg);//im not sure this returns me to the beggining of the file?
while(!F1.eof())
	{
		F1>>number;
		findig(number,mostcom);  //i dont care from the rturnning value
	}
return 0;
}
When the loop on line 32 ends F1 is in an error state. It won't do anything unless you call clear():

http://www.cplusplus.com/reference/ios/ios/clear/
That is not how I meant for you to use the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <sstream>
#include <cctype>

int main()
{
    char c;
    int digits[10] = { 0 };
    std::string str = "1026af79025asfd7962376qwerty179329gfhsa398+2039.7507-602#305076597857@9571459";
    std::istringstream sin(str);
    
    while (sin.get(c))
        if (isdigit(c))
            ++digits[c - '0'];
    std::cout << str << "\n\n";
    for (int i = 0; i < 10; ++i)
        std::cout << i << " occurred " << digits[i] << " times.\n";
    return 0;
}
1026af79025asfd7962376qwerty179329gfhsa398+2039.7507-602#305076597857@9571459

0 occurred 7 times.
1 occurred 3 times.
2 occurred 6 times.
3 occurred 5 times.
4 occurred 1 times.
5 occurred 7 times.
6 occurred 5 times.
7 occurred 10 times.
8 occurred 2 times.
9 occurred 9 times.
Topic archived. No new replies allowed.