Thread: errors with arrays and structures

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    255

    errors with arrays and structures

    normally im good at debugging code and fixing errors but im not that good with arrays or structures so sorry if im asking for alot of errors to be fixed for me but its not cuz im lazy im jus not good with arrays or structures sorry aight well id appreciate ur help thanx



    #include <iostream.h>
    #include <fstream.h>
    struct jobInfo
    {
    short code;
    long salary;
    };
    //function prototypes
    void fillArray(ifstream, jobInfo ,short);
    void searchAndDisplay(short, jobInfo[],short);

    void main()
    {
    //declare and intilize varaible
    short searchCode = 0;
    //declare and initialize the array
    jobinfo job[10];

    for(int i = 0; i < 10; i++)
    {
    job[i].code = 0;
    job[i].salary = 0.00;
    }




    //open file for input
    ifstream infile;
    infile.open("codes.dat",ios::in);
    //verify that open was successful
    if (!infile.fail())//open was successful
    {
    //fill arrays with data
    fillArray(infile,jobInfo Job, 10);
    //close file
    infile.close();
    //get a code
    cout<<"enter the code (0 to stop)\n";
    cin>>searchCode;
    cin.ignore(100,'\n');
    while (searchCode != 0)
    {
    //search the array for the code and display the salary
    searchAndDisplay(searchCode, job, 10);
    //get another code
    cout<<"enter the code (0 to stop)\n";
    cin>>searchCode;
    cin.ignore(100,'\n');
    }//end while
    }
    else
    cout<<"error opening file\n";
    //end if
    }//end main function
    void fillArray(ifstream inEmp, jobInfo ,short size)
    {
    //this function fills the arrays with data from a file
    for(short x = 0; x < size; x = x+ 1)
    {
    //enter data from a file into arrays
    inEmp >>employ[x].code;
    inEmp.ignore(1);
    inEmp>>employ[x].salary;
    inEmp.ignore(1);
    }//end for
    }//end of fillArray function
    void searchAndDisplay(short inputCode,jobInfo employ[],short size)
    {
    /*this function searches for the code in the array
    if the code is in the array, the salary is displayed
    if the code is not in the array, a message is displayed*/
    short x = 0;
    char found = 'F';
    while (x<size && found == 'F')
    {
    if(employ[x].code == inputCode)//code is in the array
    {
    cout<<"Salary: "<<employ[x].salary<<"\n\n";
    found = 'T';
    }
    else
    //add 1 to counter variable
    x = x+ 1 ;
    //end if
    }//end while
    if(found == 'F')//code is not in the array
    cout<<"The salary is not the available.\n\n";
    //end if
    }//end of searchAndDisplay function
    //end of code



    --------------------Configuration: array - Win32 Debug--------------------
    Compiling...
    array.cpp
    C:\Windows\Desktop\c++\array.cpp(17) : error C2065: 'jobinfo' : undeclared identifier
    C:\Windows\Desktop\c++\array.cpp(17) : error C2146: syntax error : missing ';' before identifier 'job'
    C:\Windows\Desktop\c++\array.cpp(17) : error C2065: 'job' : undeclared identifier
    C:\Windows\Desktop\c++\array.cpp(17) : error C2109: subscript requires array or pointer type
    C:\Windows\Desktop\c++\array.cpp(21) : error C2109: subscript requires array or pointer type
    C:\Windows\Desktop\c++\array.cpp(21) : error C2228: left of '.code' must have class/struct/union type
    C:\Windows\Desktop\c++\array.cpp(22) : error C2109: subscript requires array or pointer type
    C:\Windows\Desktop\c++\array.cpp(22) : error C2228: left of '.salary' must have class/struct/union type
    C:\Windows\Desktop\c++\array.cpp(35) : error C2275: 'jobInfo' : illegal use of this type as an expression
    C:\Windows\Desktop\c++\array.cpp(4) : see declaration of 'jobInfo'
    C:\Windows\Desktop\c++\array.cpp(35) : error C2146: syntax error : missing ')' before identifier 'Job'
    C:\Windows\Desktop\c++\array.cpp(35) : error C2059: syntax error : ')'
    C:\Windows\Desktop\c++\array.cpp(62) : error C2065: 'employ' : undeclared identifier
    C:\Windows\Desktop\c++\array.cpp(62) : error C2109: subscript requires array or pointer type
    C:\Windows\Desktop\c++\array.cpp(62) : error C2228: left of '.code' must have class/struct/union type
    C:\Windows\Desktop\c++\array.cpp(64) : error C2109: subscript requires array or pointer type
    C:\Windows\Desktop\c++\array.cpp(64) : error C2228: left of '.salary' must have class/struct/union type
    Error executing cl.exe.

    array.obj - 16 error(s), 0 warning(s)
    hooch

  2. #2
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    The problem is, I DO think you're lazy, and I would assume others do too. Simple syntax errors throughout the program, easily fixed...

    I'd be suprised if anyone went through and fixed errors as simple as ';'. I cannot speak for everyone but try a little harder, and maybe you'll see some help.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    hmmm i try not to be lazy but more specifically i was referring to th errors such as:




    C:\Windows\Desktop\c++\array.cpp(17) : error C2109: subscript requires array or pointer type
    C:\Windows\Desktop\c++\array.cpp(21) : error C2109: subscript requires array or pointer type


    i have NOOOOOOOO idea what that means well i kinda do but not any idea how tofix it




    C:\Windows\Desktop\c++\array.cpp(22) : error C2228: left of '.salary' must have class/struct/union type
    C:\Windows\Desktop\c++\array.cpp(35) : error C2275: 'jobInfo' : illegal use of this type as an expression
    C:\Windows\Desktop\c++\array.cpp(4) : see declaration of 'jobInfo'



    that i am totally lost


    so those type of errors more specifically i dunt kno howt to fix
    hooch

  4. #4
    Unregistered
    Guest
    struct jobInfo

    jobinfo job[10];


    look at these to lines. Fix the error. Recompile. see which error statements go away. Learn from your mistake. Remember the line numbers in the error message help you to find the error referred to. They aren't perfect, but they can give a good clue many times.

    ----------------------------------------------

    fillArray(infile,jobInfo Job, 10);

    although not absolutely certain based on posted code the above line almost assuredly is a function call as posted. If so I don''t believe you can declare a new variable in the function call meaning inclusion of the type name is an error as the compiler thinks it is a variable instead of function name. If that is true, then Job should probably be job. Less sure on these concerns though, as the context for use is a bit fuzzy.

  5. #5
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160
    thats a lot of errors, usually you forgot to do ONE specific thing and it accounts for everything, that happens a lot to me...
    Paro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing arrays of structures
    By chrismiceli in forum C Programming
    Replies: 6
    Last Post: 06-26-2003, 06:50 PM
  2. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  3. returning arrays of structures
    By manolo21 in forum C Programming
    Replies: 2
    Last Post: 03-31-2003, 08:09 AM
  4. Newbie Help (Arrays, Structures, Functions,Pointers)
    By tegwin in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 06:29 PM