Thread: C++ printout help

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    13

    C++ printout help

    Code:
    #include < iostream >
    using namespace std;
    int a;
    char ch;
    void test2(char& x, int c){
        int b;
        a = x - 'x';
        cout << a << c;
        b = a+c;
        x--;
        cout << b << x << ch << endl;
        cout << "\n";
        cout << endl;
    }
    int main(){
        int b;
        a = 26;
        b = 31 / a;
        ch = 'z';
        cout << "12345678901234567890\n";
        cout << "a= " << a << " b= " << b << " ch= " << ch << endl;
        test2(ch,a);
        cout << "a= " << a << " b= " << b << " ch= " << ch << endl;
        test2(ch,b);
        cout << "a= " << a << " b= " << b << " ch= " << ch << endl;
    }

    in int main

    test2(ch,a); is placed after the 2nd set of cout data is this just basically telling the function to compute this data at this point in the program?

    And because the function is a void function will the changes made to the variables within the function carry out of the function? in other words will the function permanently alter the variables or will they return to their previous values when the function exits?

    And will 31/a equal anything other then 31/a? how about x- 'x' and a+c sorry I cant remember
    Last edited by C++Newb; 10-28-2009 at 09:40 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There are cout statements in the test2 function itself, which will of course be executed when test2 is run.

    void is irrelevant. Since x is passed by reference, any changes made to x will affect ch back in main. Since c is passed by value, any changes made to c will not affect a in main.

    31/26 = 1, if that's what you're asking. (Well, 1 remainder 5, but you didn't ask for the remainder.)

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    13
    Ok what about x - 'x' and a+c assuming we cant use the asking code or whatever its called because this is part of a practice midterm and we wont be expected to know the asking codes for chars

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    13
    could a +c = d?

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    13
    I don't understand why changes made to x would affect ch I understand reference parameters but aren't they two distinct variables?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by C++Newb View Post
    I don't understand why changes made to x would affect ch I understand reference parameters but aren't they two distinct variables?
    Then you don't understand reference parameters. The point of a reference parameter or reference variable is that there is not, in fact, a distinct variable created. A reference parameter/variable must refer to a previously created variable, and is merely an alias for that variable, not a distinct entity.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by C++Newb View Post
    could a +c = d?
    You don't have a d in your code.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    13
    Ok so in this case their is only one previously created char variable so it is automatically going to reference that. If you had more then one previously created char variables how would you know which one it would reference?

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    13
    Quote Originally Posted by tabstop View Post
    You don't have a d in your code.
    I know but I thought maybe a being the first character and c being the third might add to the fourth d... I know I fail at C++

    Ok so what would that be equal to? Is their anyway to add them with out putting them into an array or a string? or is that what I would want to do?
    Last edited by C++Newb; 10-28-2009 at 10:01 PM.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by C++Newb View Post
    Ok so in this case their is only one previously created char variable so it is automatically going to reference that. If you had more then one previously created char variables how would you know which one it would reference?
    That's why you do the whole (putting things into parentheses) bit -- the bits in parentheses are what get passed to the function. The function knows only what has been passed into it.

    And a+c is just a+c. It is the value of a plus the value of c. In your case, a is a global variable, and it is assigned the value 26. c gets the value passed into it in the function. The first time the value of a is used (again 26); the second time the value of b is used (1).

  11. #11
    Registered User
    Join Date
    Oct 2009
    Posts
    13
    In the function a is assigned x - 'x' is it not? I understand it was initially assigned to 26 before the function was entered. you say c gets the value passed into it I dont see where anything is being passed into c.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by C++Newb View Post
    In the function a is assigned x - 'x' is it not? I understand it was initially assigned to 26 before the function was entered. you say c gets the value passed into it I dont see where anything is being passed into c.
    Code:
    test2(ch,a);
    Still don't think anything is passed into c?

    And yes, in the function a is assigned the value of x - 'x'. x is a reference to the first variable passed in, i.e. 'z', which is the number 122. 'x' is a number, specifically the number 120.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dump array into file in "hex format"
    By pic-o-matic in forum C Programming
    Replies: 5
    Last Post: 01-30-2009, 08:44 AM
  2. 3x3 Matrix printout
    By cisokay in forum C Programming
    Replies: 16
    Last Post: 05-10-2005, 04:33 PM
  3. menu printout
    By yokk in forum C Programming
    Replies: 3
    Last Post: 02-17-2003, 11:14 PM
  4. Pointers and Arrays
    By Sean Gilbride in forum C Programming
    Replies: 3
    Last Post: 03-12-2002, 07:06 PM
  5. How to get the printout from a program?
    By z33z in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2001, 03:37 PM