function linked list validation help

This is a discussion on function linked list validation help within the C++ Programming forums, part of the General Programming Boards category; hi everyone, can someone tells me whats wrong with this piece of code. i cant figured it out. i get ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    19

    function linked list validation help

    hi everyone, can someone tells me whats wrong with this piece of code. i cant figured it out. i get an error: lastname was not declared in this scope in structure.h (in function: newEntry @ line } while (!checkString(lastname, 1));
    i will be grateful if someone can help.

    here is the part of my structure.h
    Code:
    #include <iomanip>
    
    struct contact
    {
        char lastname[15];
        char initial[2];
        char firstname[15];
        char street[20];
        char city[15];
        char state[15];
        char country[20];
        char telephone[15];
        char email[20];
        contact *next;
    };
    
    void newEntry()
    {
        contact *root; //This will not changeor we lose the list in memory
        contact *conductor; //This will point to each node as it moves downthe list
    
        root = new contact; //Sets the root to point to something actually
        root -> next = 0; //Otherwise it will not work well
    
    
        char continueNewEntry = 'Y';
    
        do
        {
            clearScreen();
            showTitle();
            centerString("NEW Contact");
            centerString("--------------------------");
    
            do
            {
                cout << "Last Name: ";
                cin >> root -> lastname;
            } while (!checkString(lastname, 1));
    
            do
            {
                cout << "Initial: ";
                cin >> root -> initial;
            } while (!checkString(initial, 2));

    here is part of my validation.h
    Code:
    bool checkString(char* str, int n)
    {
        unsigned int i;
    
        if (n == 1)
        {
            if (strlen(str) == 0 || strlen(str) > 15)
            {
                cout << "Field is limited to 15 characters & must not be empty!";
                return false;
            }
    
            else
                return true;
        }
    
        if (n == 2)
        {
            if (strlen(str) > 2)
            {
                cout << "Field is limited to 2 characters!";
                return false;
            }
    
            else
                return true;
        }
    Here is my header file for main.cpp

    #include <iostream>
    #include <string.h>
    #include <fstream>
    #include "ui.h"
    #include "structure.h"
    #include "validation.h"

    Thank you in advance friends

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    684
    lastname is a member of contact struct and you need to point which struct instance you mean:
    Code:
    } while (!checkString(root->lastname, 1));
    Like the line before:
    Code:
    cin >> root -> lastname;
    I never put signature, but I decided to make an exception.

  3. #3
    Chi! whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    8,165
    The closest thing you have to lastname in the structure.h file is in contact. Take for example, the root contact: you access it like
    root->lastname

    When you do it in the call for checkString, you typed this:
    while (!checkString(lastname, 1));
    This refers to a "lastname" variable different from the ones in contact.

    So, that explains the error.

    Why do you have program code in an h file? Most of this belongs in a cpp file.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    19
    Thank you kmdv. will try it tomorrow since its kind late here.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    19
    @whiteflags i was in a hurry. behind schedule. will arrange the code later. thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function for linked list
    By satty in forum C Programming
    Replies: 4
    Last Post: 08-04-2010, 10:06 AM
  2. Help with linked list function
    By schmidtc in forum C Programming
    Replies: 7
    Last Post: 07-13-2010, 05:25 AM
  3. Cannot function C++ Linked List
    By xmr-mackayx in forum C++ Programming
    Replies: 6
    Last Post: 04-30-2008, 06:31 AM
  4. Linked list function
    By JFonseka in forum C Programming
    Replies: 7
    Last Post: 04-07-2008, 05:57 AM
  5. linked list noob - function create new list
    By dukysta in forum C Programming
    Replies: 5
    Last Post: 07-06-2007, 08:16 AM

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