-
structure code help
Hi I am new to structures and am practicing this code to add numbers from a command line argument to numbers given using structures and functions. If anyone can help I would appreciate it thanks. Here is my code im almost done at it compiles but i get a wrong output, my output say for 2 numbers entered in cmd line arguments(parameters) are 5 and 6. when added to 3 and 4, my output should be 10.
Code:
#include <iostream>
#include <stdlib.h>
using namespace std;
struct complex
{
double real, imag, real1, imag1;
};
void value(double *pt1, double &pt2)
{
complex num;
*pt1 = num.real + num.real1;
pt2 = num.imag + num.imag1;
}
int main(int argc, char *argv[])
{
complex number;
double val1;
double val2;
number.real=3;
number.imag=4;
number.real1 = atof(argv[1]);
number.imag1= atof(argv[2]);
value(&val1, val2);
cout << val1 << endl;
cout << val2 << endl;
system("PAUSE");
return 0;
}
-
You don't ever set the values for num in the function...You should pass number into the function:
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
struct complex
{
double real, imag, real1, imag1;
};
void value(double *pt1, double &pt2, complex num)
{
*pt1 = num.real + num.real1;
pt2 = num.imag + num.imag1;
}
int main(int argc, char *argv[])
{
complex number;
double val1;
double val2;
number.real=3;
number.imag=4;
number.real1 = atof(argv[1]);
number.imag1= atof(argv[2]);
value(&val1, val2,number);
cout << val1 << endl;
cout << val2 << endl;
system("PAUSE");
return 0;
}
PS cstdlib is the updated header for stdlib.h
-
Thank you very much JaWiB. It ran perfectly after this, i just have quick question for you though, why is number passed? thanks once again I appreciate it.
-
You instantiate and assign value for number in main() and you want to pass that number with its value attached to it to a function.
Code:
complex number;
number.real=3;
number.imag=4;
number.real1 = atof(argv[1]);
number.imag1= atof(argv[2]);
value(&val1, val2,number);
-
thanks you guys, just curious are most you software programmers and engineers and you guys seem to have a great knowledge. I am currently a comp. engineering student starting my second semester of advanced C++, its still hard to get totally comfortable, my first semester we learned functions, arrays, ptrs, macros, etc. Now we will begin structures, how long does it take for most people to really get the hang of it. I looked at lot of other people's programs on the board and they are amazing. Thanks once again guys. Cheers.
-
<edit> forget it, suffering from sleep deprived dumbness :) </edit>
-dan