Thread: Return variables question

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    10

    Return variables question

    Hi ,

    I am a beginner with C/ C++ ....

    I would like to return a string ...something like this ...I am using VC7 to compile this code......I need to do this in exactly this way bcos it is going to be called by another third party application which will accept only strings ....
    Do I need a pointer or some jazz.z?.....I am unable to declare it as a string ....only char is permitted...


    Code:
    #include <stdio.h>
    ---
    ---
    
    
    string main() 
    {
    conf_str[100] = "Data is correct" ;
    err_str[100] = "Data is incoorect"; 
    
    if ( .....) 
     return conf_str
    else 
      return err_str; 
    
    }
    Please give me some idea as to how to do this
    Thanks

    Mike

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    1) You'll need to use cstrings (character arrays)
    Code:
     my_cstring char[20] = "my cstring";

    2) to pass a cstring into a function, prototype your function to accept a char* pointer.
    Code:
    char* my_function(char*);
    then just call your function:
    Code:
    my_function(my_cstring);
    3) to return a cstring, have your function return a pointer to a cstring.
    Code:
    char* my_function(char* my_cstring)
    {
         return my_cstring;
    }
    Last edited by The Brain; 06-20-2005 at 01:01 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    10
    Hi thanks buddy

    I changed the main to char* and it works just super...

    I will have the interface checked and then see what happens

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    10
    One question

    Can I use the cString concept to pass a string to main ()

    like main(char* str)

    Thanks

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It might be better to stick with one thread.

  6. #6
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Code:
    like main(char* str)
    Not cool. main( ) is like a special function. It's kinda like how the cow is considered sacred in India. Try not to mess around with main() like that. (yes.. main() can accept command line parameters.. but that's the only exception) Instead, you should stick to making function calls inside of main( ).. and just return stuff from your functions to your code inside of main( ).

    Code:
    int main()
    {
         input char[81];
    
         char *answer;
         
         cout << "input something (no spaces)";
    
         cin >> input;
         
         answer = character_count(input);
    
         cout << "\n\nThere are " << answer << " characters";
    
         return 0;
    }

    You can see how main( ) is kinda like what a director is to a movie.. what a conductor is to an orchestra.. main( ) should ideally be mostly all function calls (no algorithmic code) This facilitates object oriented programming / black box design. main( ) should mainly just serve as an identifer for the main entry point into your program (your compiler specifically looks for your main( ) function during compilation)
    Last edited by The Brain; 06-20-2005 at 01:58 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    10
    Hi TheBrain,

    I have another post in the same forum...Maybe everyone will say stick to one thread dude !!....

    Sorry about that ...

    I am trying to do the following..

    Pass a parameter from a third party appl like Oracle forms...

    Code:
     
    /* oracle forms calls my program something like this 
       result_str :=  execute(package ...., C, senddata (str, portlength)....where senddata is the name of my c program 
    
    (I am not worried abt this part of it ....how , why when ...)
    So now my C program should take these 2 vals str and portlength as input...


    something like this

    senddata.c (This is what I have to work on)

    [code]

    main(string str, int prtlen)
    {

    ----

    return result_str;
    }


    Please hellp me on how to go about passing and return params for this ...

    Thanks

  8. #8
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I would make a call to open the program within a program using command line arguments (which is really the only time you are allowed to mess with main() )
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    10
    I didint understand what you meant by this....


  10. #10
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Here is an example of how to properly pass arguments into a program.


    Then you can do stuff like this:
    Code:
    c:\ myprogram.exe addition 1 plus 2 precision 0 allreals

    where you can pass arguments into your program at the command line
    Last edited by The Brain; 06-20-2005 at 02:40 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  11. #11
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by mike11
    I didint understand what you meant by this....

    The Brain's links are what you need.

    Basicly that third party program (#2 lets say) you need to get arguments from main() from, you use that file (#2) to include the file (#1) you need arguments from. Which is explained how to do on the page he gave: program within a program, then you just put the arguments in the program (#1) you are including in your third party program (#2). Which is explained in his next post.

    Hopefully yah get it.. read that page, like he said its the only way youre going to pass a string using the method you keep requesting.

    BTW.. this seems a little inefficient and waste of time for a beginner.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM