structure code help

This is a discussion on structure code help within the C++ Programming forums, part of the General Programming Boards category; Hi I am new to structures and am practicing this code to add numbers from a command line argument to ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    21

    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;
    }

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    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
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    21
    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.

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    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);
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    21
    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.

  6. #6
    sockets mad
    Join Date
    Mar 2002
    Posts
    126
    <edit> forget it, suffering from sleep deprived dumbness </edit>

    -dan
    Last edited by codec; 01-16-2004 at 12:55 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  2. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Structure Within Structure
    By Shakira in forum C Programming
    Replies: 3
    Last Post: 11-04-2003, 03:35 PM
  5. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21