confusion on why the void function won't recognize my declared variables

So my code is as below.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <string>
using namespace std;

void display();
void display2();
int main()
{
    char footer, header;
    int footerNum, headerNum;
  string name, saddress, caddress;

    cout<<"Enter the character to be used in the header: "<<endl;
    cin>>header;
    cout<<"Enter the number of characters to be printed in the header line: "<<endl;
    cin>>headerNum;



    cout<<"Enter full name: "<<endl;
    getline(cin, name);
    cout<<"Enter street address: "<<endl;
    getline(cin,saddress);
    cout<<"Enter city address: "<<endl;
    getline(cin,caddress);
    cout<<"Enter the character to be used in the footer: "<<endl;
    cin>>footer;
    cout<<"Enter the number of characters to be printed in the footer line: "<<endl;
    cin>>footerNum;
    display();

    cout<<name<<endl;
    cout<<saddress<<endl;
    cout<<caddress<<endl;
    display2();


}
void display()
{
    {


    while(int=headerNum);

    }

    {
        cout <<header;
    }
    cout<<endl;
}

void display2()

    {
    {



    while(int=footerNum);
    }
    {
        cout <<footer;
    }

        cout<<endl;
    }


So, my question is whether or not the void function is interfering with my declared variables. I believe the while loop should work as is with the declared variables inputed value? Right? So I'm at a loss so far. Any pointers would be great.
Your variables exist only in the scope of main because that's where you've declared them. Your functions have no clue of their existence.

You can either declare them globally before main (bad practice, but usually ok for beginners with very small programs) or pass them into the function as arguments.
Last edited on
iHutch105 wrote:
You can either declare them globally before main (bad practice, but usually ok for beginners with very small programs)
You can also let your kids eat candy but then a few months later restrict their access to it.
iHutch is right about the scope of the variables you've declared. You should declare them outside of main to give them global scope. There are other ways to deal with this (for example you can use the "static" keyword so they don't use automatic storage) but to keep things simple, just declare them outside of main.

There are some other challenges with your code. I couldn't compile it initially because the conditional statements in your while loops won't work. For one, "int" isn't a variable (nor can it be) so you can't set it equal to your "headerNum" or "footerNum" variables. Use a temporary integer variable instead. Also, you want to use the conditional "is equal" operator (==) instead of the assignment operator (=) in your while loops. Using the assignment operator is a common error. I've tweaked your code a bit and chose to use the less than operator instead of the is equal operator for simplicity.

One additional note, C++ input queue often has problems when you change the input from an integer to trying to get string input. It will compile, but the program may improperly print out the saddress line before the user can input the name. One way to deal with this is to use the cin.get() function to get the next space (some also use this with cin.clear()). It's a bit of a nuance but should clear up the error of you see it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <string>
using namespace std;

void display();
void display2();

char footer, header;            // declared variables globally.
int footerNum, headerNum;       // declared variables globally.

int main()
{

    string name, saddress, caddress;

    cout<<"Enter the character to be used in the header: ";  // I took out the endl for aesthetic reasons
    cin>>header;
    cout<<"Enter the number of characters to be printed in the header line: ";
    cin>>headerNum;


    cin.get();          // use cin.get() to help clear the input to prevent problems transitioning from int input to string input.
    cout<<"Enter full name: ";    // I took out the endl for aesthetic reasons
    getline(cin, name);
    cout<<"Enter street address: ";
    getline(cin,saddress);
    cout<<"Enter city address: ";
    getline(cin,caddress);
    cout<<"Enter the character to be used in the footer: ";
    cin>>footer;
    cout<<"Enter the number of characters to be printed in the footer line: ";
    cin>>footerNum;
    display();

    cout<<name<<endl;
    cout<<saddress<<endl;
    cout<<caddress<<endl;
    display2();


}
void display()
{
    int x = 0;             // there is no variable called "int" (and it's not legal), so declare a temporary variable for your while loop.
    while(x < headerNum)   // no ";" after whlie loop condition
    {
        cout << header;
        x++;                // use the increment operator to count.
    }
    cout<<endl;
}

void display2()
{
    int x = 0;          // same as with display().
    while(x < footerNum)  // no ";" after whlie loop condition
    {
        cout <<footer;
        x++;
    }
    cout<<endl;
}


todricos wrote:
You should declare them outside of main to give them global scope.
No. Global variables are deprecated. Static variables are too.

The variables should be passed as parameters to the functions.
Last edited on
LB wrote:
You can also let your kids eat candy but then a few months later restrict their access to it.

Let them enjoy it while they're young. They'll mature to the point where they can make their own informed decision on the matter. ;-)
My point is, you should teach them how to make the better decision right from the start, rather than let them do what they want unaware of the consequences and then suddenly put up a brick wall for no apparent reason.
Yes, declaring the variables globally. Thank you. I never thought of that.
Topic archived. No new replies allowed.