code abut session and time

need a dev c++ program does analyze an archive with the time of use from a platform of web.
the archive have just 2 columns,first one identifier the user and the second time of his log in.
the same user can make a lot of sesion.
we know the total user less than 1709

1 the user with more log on of work
2 make a archive with the total time of each user
3 the user with more total time use(take into consideration every log in)

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  #include<iostream>
#include<fstream>
#include <stdlib.h> //Atoi change string to int
using namespace std;

int main(){
	int usuarios[1708]; //Arreglo de 1708 usuarios
	int sesiones[1708];
	for(int i=0;i<1709;i++){
		usuarios[i]=0;
		sesiones[i]=0;  //every user start with 0 minutes and 0 sign
	}
	ifstream fi("tiempoespacios.txt"); //read archive  tiempo.txt	
	string aux; //temporaly save the line
	string aux2="fffff"; //save temporaly the element of the actually line
	int contador_auxiliar; //knew where end the element
	int id_usuario;
	int duracion_usuario;
	string temporal;
	while (!fi.eof() ){
		getline(fi,aux);
		aux2="ffffff";
		for(int i=0;i<10;i++){
			if(aux[i]!=' ' & aux[i]!='	') aux2[i]=aux[i];
			else{
				contador_auxiliar=i+1; //find where end the user
				break;
			}			
		}
		char tmp[contador_auxiliar];
		for(int j=0;j<contador_auxiliar;j++){
			tmp[j]=aux2[j];
		}
		id_usuario=atoi(tmp);
		aux2="ffffff";
		for(int k=0; k<10; k++){
			aux2[k]=aux[contador_auxiliar+k];
		}
		for(int l=0; l<5; l++){
			if(aux2[l]!='f') contador_auxiliar=l;
		}
		char tmp2[contador_auxiliar];
		for(int m=0;m<contador_auxiliar+1;m++){
			tmp2[m]=aux2[m];
		}
		duracion_usuario=atoi(tmp2);
		
		usuarios[id_usuario]=usuarios[id_usuario]+duracion_usuario;
		sesiones[id_usuario]=sesiones[id_usuario]+1;
		//cout<<"Usuario: "<<id_usuario<<" Sesion: "<<usuarios[id_usuario]<<endl;
	}
	
	int user_sesiones=0;
	int user_sesiones_i=0;
	for(int i=0; i<1708; i++){
		if(sesiones[i]>user_sesiones){
			user_sesiones=sesiones[i];
			user_sesiones_i=i;
		}
	}
	cout<<"El usuario con mayor cantidad de sesiones es: "<<user_sesiones_i<<endl;
	
	
	ofstream fo("resultados.txt");
 	for(int i=0; i<1709; i++){
 		if(usuarios[i]==NULL) continue;
 		fo<<"El usuario "<<i<<" tuvo "<<usuarios[i]<<" minutos de sesion."<<endl;
	 }
	 
	int mayor_duracion=0;
	int mayor_duracion_u=0;
	for (int i=0; i<1709; i++){
	 	if(usuarios[i]==NULL) continue;
	 	if(usuarios[i]>mayor_duracion) {
	 		mayor_duracion=usuarios[i];
	 		mayor_duracion_u=i;
	 	}
	}
	cout<<"El usuario con mayor duracion en sesiones es: "<<mayor_duracion_u<<endl;
	fo.close();	
	fi.close();
	return 0;
}

[Warning] NULL used in arithmetic [-Wpointer-arith]

sorry about my bad english hope can hlep me with this i dont understand how to make it work well at dev c++
Last edited on
There are a number of problems with the above code. The warning about the use of NULL is straightforward to fix, use 0 instead, since the values are integers and 0 agrees with the initial values assigned at the start.

But there are other problems. Sometimes the loop uses 1708
 
    for(int i=0; i<1708; i++){
and other times it uses 1709
 
    for(int i=0; i<1709; i++){

The value of 1709 is an error, it will attempt to access an array element which is beyond the array boundary. C and C++ will allow this kind of error with no warning or error message. One way to reduce problems is to use a defined constant, set it once at the start and use it consistently throughout.

Other problems. Non-standard variable-length arrays:
 
    char tmp[contador_auxiliar];
which is not legal in ISO standard C++

Also the code is too complex for a fairly simple task of reading a pair of integers.

Also. Do not loop on eof(). This is almost always wrong and can give unexpected results.

Simplified code, assuming the file is well-formed with just two values per line:
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
#include <iostream>
#include <fstream>

using namespace std;

const int SIZE = 1709; // we know the largest userid  is less than 1709

int main()
{    
    int usuarios[SIZE] = { 0 }; // array structure of SIZE users
    int sesiones[SIZE] = { 0 }; // every user starts with 0 minutes and 0 sessions
        
    ifstream fi("tiempoespacios.txt"); // read archive  tiempo.txt   

    int id_usuario;
    int duracion_usuario;
    
    while ( fi >> id_usuario >> duracion_usuario )
    {
        usuarios[id_usuario] = usuarios[id_usuario] + duracion_usuario;
        sesiones[id_usuario] = sesiones[id_usuario] + 1;
    }
    
    // find user with most sessions
    
    int user_sesiones   = 0;
    int user_sesiones_i = 0;
    
    for (int i=0; i<SIZE; i++)
    {
        if (sesiones[i] > user_sesiones)
        {    
            user_sesiones   = sesiones[i];
            user_sesiones_i = i;
        }
    }
    cout<<"El usuario con mayor cantidad de sesiones es: " << user_sesiones_i << endl;
    
}


Note: SIZE = 1709. This allows an array with subscripts in the range 0 to 1708 inclusive.
Last edited on
Topic archived. No new replies allowed.