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?
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.
#include <iostream>
#include <fstream>
usingnamespace std;
constint 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=newint[capacity];
*(arr++)=count;
return arr;
}
int main()
{
ifstream F1("q13.dat");
int *arr=newint[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.
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 returntrue instead of setting flag=true). Otherwise, if I was looking for the digit 7 and had the number 717, it would output twice.
#include <iostream>
#include <fstream>
usingnamespace 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;
}