Need help reading fstream and convert it into int and sort it

closed account (GybDjE8b)
So I'm having trouble reading the file and converting it into an array. I thought if i was declaring the random number and saving it into the array and the file it would save the number in two spot but it doesn't
so now i need to be able to read the array by month, day , year so i am able to sort it with radix sort

3. What is the fastest way to sort input.txt based on birthdays, i.e convert input.txt into output.txt ? Try atleast two of quicksort, heapsort, counting sort, radix sort.

4. What is the fastest way to sort output.txt based on ids, i.e convert ouput.txt into input.txt ? Try atleast two of quicksort, heapsort, counting sort, radix sort.


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
#include <iostream>
#include <fstream>
#include <stdlib.h> 

using namespace std;
struct date
{
    int month,year,day;
};

int main()
{
    const int n = 10;
    int i = 0;
    int j = 0;
    fstream bday;
    int month,day,year;
    date array[n];
    bday.open("Input.txt");
    
    for(i = 0; i < n; i++)
    {
        month = rand()%12 + 1;
        year = rand()%500 + 1501;
        day = rand()%30 + 1;
        bday << month << day << year;
        cout << i << " : " << month << " , " << day << " , " << year << endl;
        bday >> array[j].month >> array[j].day >> array[j].year;
    }
    
    bday.close();
  
    for(int i = 0; i < n;i++)
    {
        cout << array[i].month;
    }
  return 0;
}
Last edited on
Open file for output.
Write to the file.
Close the file.

Open file for input.
Read from the file.
Close the file.

Use separate loops for reading/writing.

When you are writing, you need to put some sort of separator (probably a space) between fields.
closed account (GybDjE8b)
Hmmmm Cause basically my code need to randomize an input file read it from there to change it into a integer so that i can sort it and then put it into the output file
Topic archived. No new replies allowed.