943,829 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1420
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 22nd, 2008
0

struct problem

Expand Post »
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)



C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 6
Solved Threads: 0
Junior Poster in Training
gispe is offline Offline
74 posts
since Jul 2008
Aug 22nd, 2008
0

Re: struct problem

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...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Aug 22nd, 2008
0

Re: struct problem

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Aug 24th, 2008
0

Re: struct problem

hi ppl

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

the nw code is:

C++ Syntax (Toggle Plain Text)
  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, 8 views)
Reputation Points: 6
Solved Threads: 0
Junior Poster in Training
gispe is offline Offline
74 posts
since Jul 2008
Aug 24th, 2008
0

Re: struct problem

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 26th, 2008
0

Re: struct problem

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

C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 6
Solved Threads: 0
Junior Poster in Training
gispe is offline Offline
74 posts
since Jul 2008
Aug 26th, 2008
0

Re: struct problem

It looks like a problem with _TCHAR

why did you change it from int main(int argc, char* argv[])
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Aug 26th, 2008
0

Re: struct problem

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.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Aug 26th, 2008
0

Re: struct problem

i changed that n now it says


C++ Syntax (Toggle Plain Text)
  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)
Reputation Points: 6
Solved Threads: 0
Junior Poster in Training
gispe is offline Offline
74 posts
since Jul 2008
Aug 26th, 2008
0

Re: struct problem

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.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Heap Corruption??!! Help!!!
Next Thread in C++ Forum Timeline: socket programming





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC