| | |
struct problem
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jul 2008
Posts: 60
Reputation:
Solved Threads: 0
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)
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
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
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)
#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
C++ Syntax (Toggle Plain Text)
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
...
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":
?
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...
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":
c Syntax (Toggle Plain Text)
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...
•
•
Join Date: Jul 2005
Posts: 1,681
Reputation:
Solved Threads: 264
C++ Syntax (Toggle Plain Text)
while (vendedor.legajo == 0) ingresar_ventas (); //enter sales
•
•
Join Date: Jul 2008
Posts: 60
Reputation:
Solved Threads: 0
hi ppl
ive corrected some stuff bout this program, but still havin problems when runnin
the nw code is:
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
ive corrected some stuff bout this program, but still havin problems when runnin
the nw code is:
C++ Syntax (Toggle Plain Text)
// 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.
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.
•
•
Join Date: Jul 2008
Posts: 60
Reputation:
Solved Threads: 0
hi ppl
ive arrenged some things bout this program, and fixed most errors, but gives me just one error. the code is:
and the error is:
i dont know how to fix it
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)
// 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:
C++ Syntax (Toggle Plain Text)
--------------------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
Just make it
It also works on MSVC++.
@ iamthwee: Examples on MSDN use that, I think.
c Syntax (Toggle Plain Text)
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.
•
•
Join Date: Jul 2008
Posts: 60
Reputation:
Solved Threads: 0
i changed that n now it says
C++ Syntax (Toggle Plain Text)
--------------------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.
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.
![]() |
Similar Threads
- Struct Problem (C)
- Beginer struct problem! (C)
- Accessing To A Struct With A Given Pointer (C)
- Derived class problem (C)
- problem with my program!! (C++)
- struct error (!?) (C)
- struct type redefinition (C++)
- ** Need Help ** in a small C++ problem (C++)
Other Threads in the C++ Forum
- Previous Thread: Heap Corruption??!! Help!!!
- Next Thread: socket programming
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






