help with pointer

Write your question here.

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

    while (true) {
        char item_Name[MAX_CHAR] = {'\0'};
        cout << "Please, enter name of item you would like "
        << "to purchase"
        << endl;
        if (cin.getline (item_Name, MAX_CHAR, '\n')) {
            break;
        }
        else
            cout << "Invalid Input: please only enter strings ";
            cin.clear();
            cin.ignore(MAX_CHAR, '\n');

        }
        cin.ignore(MAX_CHAR, '\n');

    while (true) {
        double item_Cost = 0.0;
        cout << "Please, enter the cost of " << item_Name
        << " :"
        << endl;
        if (cin >> item_Cost) {
            break;
        }
        else
            cout << "Invalid Input: please only enter numbers ";
        cin.clear();
        cin.ignore(MAX_CHAR, '\n');

    }
    cin.ignore(MAX_CHAR, '\n');


it seems my ide is complaining about line 20 "undeclared identifier "
cout << "Please, enter the cost of " << item_Name

I am thinking a pointer could solve this.
but not sure how to use them in this case.



Looks like you have mismatched braces. The brace at line 15 visually appears to be part of the if-else, but actually it is closing the while loop which began at line 2.

But your question regarding item_Name, this is declared as a local variable within the first while loop. When you try to refer to it at line 20, it has gone out of scope (no longer exists) because of the previous closing brace.

Two suggestions, take care to match the opening and closing braces carefully (currently they are wrong) and if necessary move the declaration of item_Name to a higher level so that it remains in scope.
Last edited on
Yea. I saw that also.

When I updated the code to

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
    char item_Name[MAX_CHAR] = {'\0'};
    while (true) {
        cout << "Please, enter name of item you would like "
        << "to purchase"
        << endl;
        if (cin.getline (item_Name, MAX_CHAR, '\n')) {
            break;
        }
        else
            cout << "Invalid Input: please only enter strings ";
            cin.clear();
            cin.ignore(MAX_CHAR, '\n');

        }
        cin.ignore(MAX_CHAR, '\n');

    while (true) {
        double item_Cost = 0.0;
        cout << "Please, enter the cost of item: " << item_Name
        << endl;
        if (cin >> item_Cost) {
            break;
        }
        else
            cout << "Invalid Input: please only enter numbers ";
        cin.clear();
        cin.ignore(MAX_CHAR, '\n');

    }
    cin.ignore(MAX_CHAR, '\n');


the problematic variable is not printing to common output.

EDIT: going factor both of these blocks so this is the other issue, that may not be jumping out at you just by my code and explanations. Hence the reason for pointers. I could just pass the variable by value but I want to try for a pointer here.
Last edited on
I still think in the amended code the braces are mismatched. From the context it looks like there should be an open brace after the else at line 9, and then another closing brace to complete the while loop.

Sorry, I don't understand the rest of the question.
never mind...
i need to get the len of the char array.
to print. thank you.
Topic archived. No new replies allowed.