hi!!

i made a program to count the max sales in a store, that has for example 10 employees.
for this, i used an struct to have the code of the employee and the sales he made. the point of the code is to show a list of the employees who reached the top sale (for exaple, i have a max sale of $ 3000, n 3 ppl reached that, ill have to show the code of this 3 ppl and the $3000)

#include "stdafx.h"
#include <iostream>


using namespace std;


void ingresar_legajo ();
void ingresar_ventas ();                // prototypes
void comparar_datos ();


struct datos_vendedor
{
	int legajo;              // code of employee
	float total_ventas;     //total sales he made
}vendedor;


int main(int argc, char* argv[])
{
	ingresar_legajo ();          // function to enter the code
	comparar_datos ();          // function to compare the max sales made

	return 0;
}



void ingresar_legajo ()             // function to enter the code
{
	int i;

	for (i = 0; vendedor.legajo!=0, i <= 10; i++)    //employee.code
	{
		cout << " Ingrese legajo: ";      //enter code
		cin >> vendedor.legajo;           //read code

		while (vendedor.legajo == 0)
			ingresar_ventas ();     //enter sales
	}
}



void ingresar_ventas ()        // function to enter sales
{ 
	int j;

	for (j = 0; j <= 10; j++)
	{
		cout << "ingrese total ventas: ";
		cin >> vendedor.total_ventas;         //employee.total_sales (from the struct)
		cout << "\n";
	}
}



void comparar_datos ()        // it compares the sales made to find the max
{
	int aux = 0;
	int max_ventas = 0, vendedor = 0;         //max_sales and the seller
	float total_ventas;

	if (total_ventas > aux)        // 
	{
		aux = total_ventas;
		max_ventas++;
		vendedor++;
	}
	if (total_ventas == aux)
	{
		aux = total_ventas;
		max_ventas++;
		vendedor++;
	}

}

when run this, when i enter a 0, it keeps runnin anyway, stops countin the codes of employees, and start askin for ALL the sales made for all the ppl, eventhough i havent entered them.
and if i dont enter a 0, it keep runnin until 11 employees. not 10.

so, this made me doubt about the for statment

for (i = 0; vendedor.legajo!=0, i <= 10; i++)

i dont know if its better to use a while on this... same conditions that in the for, but in a while...


hope i explained that well...
if not ask to explain it better

Truth to tell, it's a very strange code.
About 11 employees: of course, the loop for (i = 0; vendedor.legajo!=0, i <= 10; i++) repeats 11 times. Take a piece of paper and a pencil... Right: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ;)...
If you want to process 10 employees, where is an array of 10 struct datos_vendedor?
Do you think that you wrote well structured program if your main function body is so "functional":

ingresar_legajo ();    // function to enter the code	
comparar_datos ();   // function to compare the max sales made

?
No, these functions process global data and the reader does not know what they do. Declare processed data in main body then pass them to these functions as arguments.
Please, forget global data declarations as soon as possible: no need in this senseless data globalization...

while (vendedor.legajo == 0)  
  ingresar_ventas ();     //enter sales

the above loop will only run if vendedor.legajo equals zero. In addition, there is no way to change vendedor.legajo from within ingresar_ventas(), so once ingresar_ventas() returns, vendedor.legajo will still be zero, so away you go again, for ever and ever and ever. I would consider using an if statement rather than a while statement in the above snippet. I would also change the logic so that when the if conditional is false, then the for loop stops. That means the logic operator comparing vendedor.legajo to zero would be th same in the for loop statement and in the if conditional.

hi ppl

ive corrected some stuff bout this program, but still havin problems when runnin

the nw code is:

// maximo_ventas.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <vector>


using namespace std;


void ingresar_datos (int numeros[]);
void maximos(int numero[], float ventas);



int _tmain(int argc, _TCHAR* argv[])
{
	int numeros [10];
	float ventas;

	ingresar_datos (numeros);
	maximos(numeros, ventas);

	system ("PAUSE");
	return 0;
}



void ingresar_datos (int numeros[])
{
	int i, numero;
	float ventas =0;

	for (i = 0; i < 10; i++)   //this for isnt countin
	{
		cout << " Ingresa el legajo: ";
		cin >> numero;

		if (numero == 0) break;

		while ((numero != 0) && (i<10))
		{
			cout << " Ingresa el importe total de las ventas que hizo: ";
			cin >> ventas;

			cout << " Ingresa el legajo: ";
			cin >> numero;

			i++;
		}

		if (i == 10) break;
	}
}



void maximos(int numero[], float ventas)
{
	int i;

	for (i = 0; i < 10; i++)
	{
		float aux = 0.0;

		if (ventas >= aux)
		{
			aux = ventas;
			cout << numero;
		}
	}
}

the problem now is that when i make the 10 entries, i reads an extra one, and gives an error on 1 variable.
a bmp with the error is attached

The "used without being initialised" refers to your line 20.
You don't set it to anything, then you pass it to a function.
Answer: initialise it.

> //this for isnt countin
Your while loop is doing the counting - check line 51.
You have two loops, so I guess it goes round one more time than you expect.

hi ppl
ive arrenged some things bout this program, and fixed most errors, but gives me just one error. the code is:

// maximo_ventas.cpp : Defines the entry point for the console application.

//

 

#include "stdafx.h"
#include <iostream>
#include <vector>
 

using namespace std;

 

void ingresar_gente ();
void comparar_datos (int vendedor[]);



int _tmain(int argc, _TCHAR* argv[])
{
	int vendedor[10];

	ingresar_gente (); 
	comparar_datos (vendedor);

	system ("PAUSE");

	return 0;
}

 


void ingresar_gente ()
{
	int i = 0, legajo;
	float total_ventas;

	while (i < 10)
	{
		cout << " Ingrese un legajo: ";
        cin >> legajo;

		i++;

		while (legajo != 0)
		{
			cout << " Ingrese la cantidad de ventas hechas: ";
			cin >> total_ventas;
		}
	}

}

 

void comparar_datos (int vendedor[])
{
	float aux, total_ventas;
	int i, legajo;

	for (i = 1; i < 10; i++)
	{
		if (total_ventas >= aux)
		{
			aux = total_ventas;
			cout << legajo;
		}
	}

}

and the error is:

--------------------Configuration: maximo_ventas - Win32 Debug--------------------
Compiling...
maximo_ventas.cpp
C:\Documents and Settings\pm11218\ejercicios c++\maximo_ventas\maximo_ventas.cpp(21) : error C2061: syntax error : identifier '_TCHAR'
Error executing cl.exe.

maximo_ventas.exe - 1 error(s), 0 warning(s)

i dont know how to fix it :S

Member Avatar for iamthwee

It looks like a problem with _TCHAR

why did you change it from int main(int argc, char* argv[])

Just make it

int main(int argc, char *argv[]){

It also works on MSVC++.

@ iamthwee: Examples on MSDN use that, I think.

i changed that n now it says

--------------------Configuration: maximo_ventas - Win32 Debug--------------------
Compiling...
maximo_ventas.cpp
C:\Documents and Settings\pm11218\ejercicios c++\maximo_ventas\maximo_ventas.cpp(66) : warning C4700: local variable 'total_ventas' used without having been initialized
C:\Documents and Settings\pm11218\ejercicios c++\maximo_ventas\maximo_ventas.cpp(69) : warning C4700: local variable 'legajo' used without having been initialized
Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/maximo_ventas.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

maximo_ventas.exe - 2 error(s), 2 warning(s)

Erm..you're using MSVC++ right? What I posted before should work... You might want to get GCC for Windows tho. It doesn't rape the standard as much.

Oh, and post your code again please. Like it is now. May seem tedious, but it gives valuable information.

code is:

// maximo_ventas.cpp : Defines the entry point for the console application.

//

 

#include "stdafx.h"
#include <iostream>
#include <vector>
 

using namespace std;

 

void ingresar_gente ();
void comparar_datos (int vendedor[]);



int _tmain(int argc, char* argv[])
{
	int vendedor[10];

	ingresar_gente (); 
	comparar_datos (vendedor);

	system ("PAUSE");

	return 0;
}

 


void ingresar_gente ()
{
	int i = 0, legajo;
	float total_ventas;

	while (i < 10)
	{
		cout << " Ingrese un legajo: ";
        cin >> legajo;

		i++;

		while (legajo != 0)
		{
			cout << " Ingrese la cantidad de ventas hechas: ";
			cin >> total_ventas;
		}
	}

}

 

void comparar_datos (int vendedor[])
{
	float aux, total_ventas;
	int i, legajo;

	for (i = 1; i < 10; i++)
	{
		if (total_ventas >= aux)
		{
			aux = total_ventas;
			cout << legajo;
		}
	}

}

and the errors are:

--------------------Configuration: maximo_ventas - Win32 Debug--------------------
Compiling...
maximo_ventas.cpp
C:\Documents and Settings\pm11218\ejercicios c++\maximo_ventas\maximo_ventas.cpp(66) : warning C4700: local variable 'total_ventas' used without having been initialized
C:\Documents and Settings\pm11218\ejercicios c++\maximo_ventas\maximo_ventas.cpp(69) : warning C4700: local variable 'legajo' used without having been initialized
Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/maximo_ventas.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

maximo_ventas.exe - 2 error(s), 2 warning(s)

and yes, im usin visual c++ 6.0
now, tinkin.. the version has somethin to do with this or is somethin else?

I said

int main

not

int _tmain

EDIT: That might sound a bit harsh. No hard feelings, it's just what I said.

int _tmain(int argc, char* argv[])

and the errors are:

Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main

You have to #include <tchar.h>

the code is:

// maximo_ventas.cpp : Defines the entry point for the console application.

//

 

#include "stdafx.h"
#include <iostream>
#include <vector>
 

using namespace std;

 

void ingresar_gente ();
void comparar_datos (int vendedor[]);



int _tmain(int argc, char* argv[])
{
	int vendedor[10];

	ingresar_gente (); 
	comparar_datos (vendedor);

	system ("PAUSE");

	return 0;
}

 


void ingresar_gente ()
{
	int i = 0, legajo;
	float total_ventas;

	while (i < 10)
	{
		cout << " Ingrese un legajo: ";
        cin >> legajo;

		i++;

		while (legajo != 0)
		{
			cout << " Ingrese la cantidad de ventas hechas: ";
			cin >> total_ventas;
		}
	}

}

 

void comparar_datos (int vendedor[])
{
	float aux, total_ventas;
	int i, legajo;

	for (i = 1; i < 10; i++)
	{
		if (total_ventas >= aux)
		{
			aux = total_ventas;
			cout << legajo;
		}
	}

}

> local variable 'legajo' used without having been initialized
Pay attention to these as well!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.