Variables between functions?

This is a discussion on Variables between functions? within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm a little confused and was hoping someone could help me tidy up this problem. I'm trying to write ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    27

    Variables between functions?

    Hi, I'm a little confused and was hoping someone could help me tidy up this problem. I'm trying to write a program which allows the user to input an address, which then prints it on the screen, and finally sends it to a label printer. The problem I've got is that I've just discovered that variables aren't passed between functions.

    I need addressenter() to pass the address to the other functions like menu(), and printlabel()

    How do I do this? I've misunderstood the tutorial I think, so I'd grateful if you could clear it up for me.
    Thanks

    Sorry this is a real shambles, but I can sort it out once I know what to do.

    Code:
    #include <iostream>
    
    using namespace std;
    
    // prototype address enter function
    int menu();
    void printlabel();
    void addressenter();
    
    int main()
    {
    
        cout<<"This program prints single address labels input by the user.\n\n";
        addressenter();
        print();
    
        cin.get();
    }
    
    // post-addressenter menu
    int menu()
    {
            x = 0;
        cout<<"\n\nYour address was: \n\n";
         while (x<y) {
            //Address output here
            cout<<address[x]<<endl;
            x++;
        };
        cout<<"\nThe address has to be truncated to 22 chars, is it correct?\n1. Yes\n2. No\n\n";
        int m;
        cin>>m;
        if ( m == 1 ) {
            cout<<"Print label...";
        }
        else if ( m == 2 ) {
            cout<<"Reenter address";
            addressenter(1);
        }
        else {
            cout<<"Invalid selection, please try again.\n";
            menu();
        };
        return 0;
    }
    
    int addressenter()
    {
        // address enter function
        // str length is 23 as 22+terminating.char "\0"
        string address[7];
        string addressin;
        cout<<"Enter the address to be printed.\n";
        int x = 0;
        int y = 0;
        while (x<7 && y!=1) {
            //Address input here
            cout<<"Line "<< x + 1 <<": ";
            //cin.getline ( address[x], 23, '\n' );
            getline(cin, addressin);
            if (addressin!="") {
            address[x] = addressin.substr(0, 22);
            x++;
            }
            else {
                y=1;
            };
        };
        y = x;
        x = 0;
        cout<<"\n\nYour address was: \n\n";
         while (x<y) {
            //Address output here
            cout<<address[x]<<endl;
            x++;
        };
        menu();
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    if you can see your function signatures

    You are not passing anything to them

    Code:
    int menu();
    void printlabel();
    void addressenter();

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    I can give you an example of variable passing between functions

    try to debug and understand the code

    Code:
    #include <stdio.h>
    
    int sum(const int a, const int b) {
      return a + b;
    }
    
    void swap(int& a, int& b) {
      int temp = a;
      a = b;
      b = temp;
    }
    
    int main() {
      int a = 10;
      int b = 20;
      printf("a == %d || b == %d\n", a, b);
      printf("sum == %d\n", sum(a, b));
      swap(a, b);
      printf("a == %d || b == %d\n", a, b);
    }

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    27
    Ok, thanks, I can see the logic there, but am still struggling with a couple of smaller points.

    The first is what effect does the "int& a" have as opposed to "int a". I saw it didn't work without the &.
    Second, is how do I get it to pass a string array? I tried both of the following and neither worked.

    string address[7] is what I need to pass on to:

    Code:
    int menu(string address, int y)
    { ... }
    
    int menu(string& address, int y)
    { ... }
    from:
    Code:
    menu(address, y);
    Last edited by mikeyp; 11-25-2009 at 06:27 AM.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    The first is what effect does the "int& a" have as opposed to "int a".
    The answer is function(int a) means the vallue from the caller will be copied to the function argument. And when the pointer will be returned to the caller than the, value passed to the function will not be changed.

    But in case of function(int& a) it is passing value by reference, means the it will be the alias of the function variable and the value will be changed when the pointer will return to the caller.

    just google call by value & call by reference.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Regarding this question

    string address[7] is what I need to pass on to
    As the string is the array of characters. So every string element placed at any index will be a char.
    So when you will pass a string with an index, Def it will be a char.

    Code:
    #include <stdio.h>
    
    void print(const char ch) {
      printf("char ==  %c\n", ch);
    }
    
    int main() {
      char arr[] = "Hello";
      print(arr[2]);
      return 0;
    }

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    27
    ok, doing well, I understand that much better now, and I managed to alter the code from the previous post to cout hello, which is good, I now understand that much about needing to pass on a char array, however, I've now established I need to convert my stringarray to a char array to do it.

    Code:
        while (x<7 && y!=1) {
            //Address input here
            cout<<"Line "<< x + 1 <<": ";
            //cin.getline ( address[x], 23, '\n' );
            getline(cin, addressin);
            if (addressin!="") {
            addressin = addressin.substr(0, 22);
            address[x] = addressin.c_str();
            x++;
            }
            else {
                y=1;
            };
    however when I try to build and run I get this...

    C:\Documents and Settings\Michael\My Documents\C++\1label\main.cpp||In function `void addressenter()':|
    C:\Documents and Settings\Michael\My Documents\C++\1label\main.cpp|85|error: incompatible types in assignment of `const char*' to `char[23]'|
    ||=== Build finished: 1 errors, 0 warnings ===|

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    27
    ok, forget the last post, I got this to work... now all I need is to implement it in my original script.

    Code:
    void print(string ch[2]) {
      cout<<ch[0]<<ch[1]<<ch[2];
    }
    
    int main() {
        string arr[3] = {"Sword ", "Armor ", "Shield."};
        cout<< arr[0]<<arr[1]<<arr[2];
        print(arr);
        return 0;
    }
    I'll get back to you if there are any problems.

    Thanks for your help in prodding me in the right direction.
    If they had reps here, I'd +rep you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  2. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  3. Global variables and functions.
    By earth_angel in forum C Programming
    Replies: 6
    Last Post: 07-25-2005, 01:38 PM
  4. Replies: 6
    Last Post: 05-06-2003, 04:08 PM
  5. passing variables to functions?
    By aoe in forum C Programming
    Replies: 12
    Last Post: 06-02-2002, 05:19 PM

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