struct problem

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2008
Posts: 60
Reputation: gispe is an unknown quantity at this point 
Solved Threads: 0
gispe gispe is offline Offline
Junior Poster in Training

struct problem

 
0
  #1
Aug 22nd, 2008
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)



  1. #include "stdafx.h"
  2. #include <iostream>
  3.  
  4.  
  5. using namespace std;
  6.  
  7.  
  8. void ingresar_legajo ();
  9. void ingresar_ventas (); // prototypes
  10. void comparar_datos ();
  11.  
  12.  
  13. struct datos_vendedor
  14. {
  15. int legajo; // code of employee
  16. float total_ventas; //total sales he made
  17. }vendedor;
  18.  
  19.  
  20. int main(int argc, char* argv[])
  21. {
  22. ingresar_legajo (); // function to enter the code
  23. comparar_datos (); // function to compare the max sales made
  24.  
  25. return 0;
  26. }
  27.  
  28.  
  29.  
  30. void ingresar_legajo () // function to enter the code
  31. {
  32. int i;
  33.  
  34. for (i = 0; vendedor.legajo!=0, i <= 10; i++) //employee.code
  35. {
  36. cout << " Ingrese legajo: "; //enter code
  37. cin >> vendedor.legajo; //read code
  38.  
  39. while (vendedor.legajo == 0)
  40. ingresar_ventas (); //enter sales
  41. }
  42. }
  43.  
  44.  
  45.  
  46. void ingresar_ventas () // function to enter sales
  47. {
  48. int j;
  49.  
  50. for (j = 0; j <= 10; j++)
  51. {
  52. cout << "ingrese total ventas: ";
  53. cin >> vendedor.total_ventas; //employee.total_sales (from the struct)
  54. cout << "\n";
  55. }
  56. }
  57.  
  58.  
  59.  
  60. void comparar_datos () // it compares the sales made to find the max
  61. {
  62. int aux = 0;
  63. int max_ventas = 0, vendedor = 0; //max_sales and the seller
  64. float total_ventas;
  65.  
  66. if (total_ventas > aux) //
  67. {
  68. aux = total_ventas;
  69. max_ventas++;
  70. vendedor++;
  71. }
  72. if (total_ventas == aux)
  73. {
  74. aux = total_ventas;
  75. max_ventas++;
  76. vendedor++;
  77. }
  78.  
  79. }





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

  1. 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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: struct problem

 
0
  #2
Aug 22nd, 2008
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":
  1. ingresar_legajo (); // function to enter the code
  2. 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...
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,681
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 264
Lerner Lerner is offline Offline
Posting Virtuoso

Re: struct problem

 
0
  #3
Aug 22nd, 2008
  1. while (vendedor.legajo == 0)
  2. 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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 60
Reputation: gispe is an unknown quantity at this point 
Solved Threads: 0
gispe gispe is offline Offline
Junior Poster in Training

Re: struct problem

 
0
  #4
Aug 24th, 2008
hi ppl

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

the nw code is:

  1. // maximo_ventas.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <vector>
  7.  
  8.  
  9. using namespace std;
  10.  
  11.  
  12. void ingresar_datos (int numeros[]);
  13. void maximos(int numero[], float ventas);
  14.  
  15.  
  16.  
  17. int _tmain(int argc, _TCHAR* argv[])
  18. {
  19. int numeros [10];
  20. float ventas;
  21.  
  22. ingresar_datos (numeros);
  23. maximos(numeros, ventas);
  24.  
  25. system ("PAUSE");
  26. return 0;
  27. }
  28.  
  29.  
  30.  
  31. void ingresar_datos (int numeros[])
  32. {
  33. int i, numero;
  34. float ventas =0;
  35.  
  36. for (i = 0; i < 10; i++) //this for isnt countin
  37. {
  38. cout << " Ingresa el legajo: ";
  39. cin >> numero;
  40.  
  41. if (numero == 0) break;
  42.  
  43. while ((numero != 0) && (i<10))
  44. {
  45. cout << " Ingresa el importe total de las ventas que hizo: ";
  46. cin >> ventas;
  47.  
  48. cout << " Ingresa el legajo: ";
  49. cin >> numero;
  50.  
  51. i++;
  52. }
  53.  
  54. if (i == 10) break;
  55. }
  56. }
  57.  
  58.  
  59.  
  60. void maximos(int numero[], float ventas)
  61. {
  62. int i;
  63.  
  64. for (i = 0; i < 10; i++)
  65. {
  66. float aux = 0.0;
  67.  
  68. if (ventas >= aux)
  69. {
  70. aux = ventas;
  71. cout << numero;
  72. }
  73. }
  74. }


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
Attached Images
File Type: bmp vendedores.bmp (865.7 KB, 3 views)
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: struct problem

 
0
  #5
Aug 24th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 60
Reputation: gispe is an unknown quantity at this point 
Solved Threads: 0
gispe gispe is offline Offline
Junior Poster in Training

Re: struct problem

 
0
  #6
Aug 26th, 2008
hi ppl
ive arrenged some things bout this program, and fixed most errors, but gives me just one error. the code is:

  1. // maximo_ventas.cpp : Defines the entry point for the console application.
  2.  
  3. //
  4.  
  5.  
  6.  
  7. #include "stdafx.h"
  8. #include <iostream>
  9. #include <vector>
  10.  
  11.  
  12. using namespace std;
  13.  
  14.  
  15.  
  16. void ingresar_gente ();
  17. void comparar_datos (int vendedor[]);
  18.  
  19.  
  20.  
  21. int _tmain(int argc, _TCHAR* argv[])
  22. {
  23. int vendedor[10];
  24.  
  25. ingresar_gente ();
  26. comparar_datos (vendedor);
  27.  
  28. system ("PAUSE");
  29.  
  30. return 0;
  31. }
  32.  
  33.  
  34.  
  35.  
  36. void ingresar_gente ()
  37. {
  38. int i = 0, legajo;
  39. float total_ventas;
  40.  
  41. while (i < 10)
  42. {
  43. cout << " Ingrese un legajo: ";
  44. cin >> legajo;
  45.  
  46. i++;
  47.  
  48. while (legajo != 0)
  49. {
  50. cout << " Ingrese la cantidad de ventas hechas: ";
  51. cin >> total_ventas;
  52. }
  53. }
  54.  
  55. }
  56.  
  57.  
  58.  
  59. void comparar_datos (int vendedor[])
  60. {
  61. float aux, total_ventas;
  62. int i, legajo;
  63.  
  64. for (i = 1; i < 10; i++)
  65. {
  66. if (total_ventas >= aux)
  67. {
  68. aux = total_ventas;
  69. cout << legajo;
  70. }
  71. }
  72.  
  73. }



and the error is:

  1. --------------------Configuration: maximo_ventas - Win32 Debug--------------------
  2. Compiling...
  3. maximo_ventas.cpp
  4. C:\Documents and Settings\pm11218\ejercicios c++\maximo_ventas\maximo_ventas.cpp(21) : error C2061: syntax error : identifier '_TCHAR'
  5. Error executing cl.exe.
  6.  
  7. maximo_ventas.exe - 1 error(s), 0 warning(s)


i dont know how to fix it
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: struct problem

 
0
  #7
Aug 26th, 2008
It looks like a problem with _TCHAR

why did you change it from int main(int argc, char* argv[])
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 375
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: struct problem

 
0
  #8
Aug 26th, 2008
Just make it

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

It also works on MSVC++.

@ iamthwee: Examples on MSDN use that, I think.
Last edited by Clockowl; Aug 26th, 2008 at 10:32 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 60
Reputation: gispe is an unknown quantity at this point 
Solved Threads: 0
gispe gispe is offline Offline
Junior Poster in Training

Re: struct problem

 
0
  #9
Aug 26th, 2008
i changed that n now it says


  1. --------------------Configuration: maximo_ventas - Win32 Debug--------------------
  2. Compiling...
  3. maximo_ventas.cpp
  4. C:\Documents and Settings\pm11218\ejercicios c++\maximo_ventas\maximo_ventas.cpp(66) : warning C4700: local variable 'total_ventas' used without having been initialized
  5. C:\Documents and Settings\pm11218\ejercicios c++\maximo_ventas\maximo_ventas.cpp(69) : warning C4700: local variable 'legajo' used without having been initialized
  6. Linking...
  7. LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
  8. Debug/maximo_ventas.exe : fatal error LNK1120: 1 unresolved externals
  9. Error executing link.exe.
  10.  
  11. maximo_ventas.exe - 2 error(s), 2 warning(s)
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 375
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: struct problem

 
0
  #10
Aug 26th, 2008
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.
Last edited by Clockowl; Aug 26th, 2008 at 10:44 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC