Can anyone fix the error(S) that are in my code?

Pages: 12
I've basically completed my assignment, and the program shows 0 errors, but when I run it the program doesn't show me the address I have entered, the Employees full name or the Employees job status, can anyone help me find these errors? thanks
Last edited on
In function 'void editFile()': 90:11: warning: ISO C++ forbids taking address of function '::main' [-Wpedantic] In function 'void input()': 128:8: warning: ISO C++ forbids taking address of function '::main' [-Wpedantic] In function 'void searchname()': 164:11: warning: ISO C++ forbids taking address of function '::main' [-Wpedantic] In function 'void searchage()': 200:17: warning: ISO C++ forbids taking address of function '::main' [-Wpedantic] In function 'void searchsalary()': 231:11: warning: ISO C++ forbids taking address of function '::main' [-Wpedantic] In function 'void displayall()': 256:11: warning: ISO C++ forbids taking address of function '::main' [-Wpedantic] In function 'void search()': 291:23: warning: ISO C++ forbids taking address of function '::main' [-Wpedantic] 261:10: warning: unused variable 'age' [-Wunused-variable] 261:15: warning: unused variable 'id' [-Wunused-variable] 263:11: warning: unused variable 'address' [-Wunused-variable] 264:18: warning: unused variable 'salary' [-Wunused-variable] In function 'void deleteFile()': 313:11: warning: ISO C++ forbids taking address of function '::main' [-Wpedantic] 317:14: warning: ISO C++ forbids taking address of function '::main' [-Wpedantic]
Can you break it down in simpler terms, and show me your corrections to my project, please?
Those warnings mean that you can't call main() from within your program. Change the code so that main contains a loop. Add a return statement to the case where the user wants to quit.

The code doesn't work as well as it appears. Let's start with the format of the file. Since an employee's name and address are likely to contain spaces, you should write those on a separate line and read them with getline(). Note that input() allocates just a single character for the address. It should be a string.

Have you learned about structures and classes? You could simplify the code by creating a structure of class for an employee and then creating methods to read, write, and display an employee record.

Yeah but the output just doesn't display the job status or address, the rest of it works fine according to my lecturer. Could you show me your corrections to the original code to get the output functioning properly? Thanks in advance
Start by deleting newemployee.txt to clear out any bad data. Then enter some records, but enter just a single character for the address. If you do this it will work fine.
Last edited on
Can you show me the code for that using my code and editing the parts that need correcting? Thanks
I've tried to correct it and now I can see my Job Status variable in the output, but I can't create space between the answer (Part Time/ Full Time) so it just displays the first word. Meanwhile my address just shows the numbers
 Entire employee database
Name
Age
Salary
Address
ID
Job Status
-----------------------------------------
Kanye 41 $50000 5 7612 Full
Press any key to continue . . .
 
Please re-read the second paragraph of my first comment.
I read it, but wanted to ask to see your edited version because I'm still new to this language? Thanks in advance.
I don't want to create spaces, I just want me to be able to input the full name with a gap between first and second; same for the address and Job Status. If you could show me using my code how you would code it so those features show on the output, that'd be great.
I'm sorry to say that the code still has a bunch of problems. Lets start by just getting it to prompt for a new employee, write the record to the file, and then display all records.

Here are a pair of matching functions to read and write an employee from/to a file:
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
bool readEmployee (istream &is,
                   string &name,
                   int &age,
                   double &salary,
                   string &address, // note! address is a string.
                   int &id,
                   string &jobstatus)
{
    is >> ws;                   // skip any pending whitespace.
    getline(is, name);
    getline(is, address);
    is >> age >> salary >> id >> jobstatus;
    return is.good();
}



bool writeEmployee(ostream &os,
                   const string &name,
                   int age,
                   double salary,
                   const string &address, // note! address is a string.
                   int id,
                   const string &jobstatus)
{
    os << name << '\n';
    os << address << '\n';
    os << age << ' ' << ' '
       << salary << ' '
       << id << ' '
       << jobstatus << "\n\n";
    return os.good();
}

Note that address is a string and salary is a double. You need to be consistent with these types in the rest of the program. The current code usually defines address as a char, and salary is defined as all sorts of things.

1. add these functions to your code.
2. Modify input() to use the right types for salary and address, and to write the variables using writeEmployee()
3. Modify displayAll() to use the right types for salary and address, and to read the records using readEmployee()

Now test the program with this input:
3
Y
1
George Washington
33
1500
Mount Vernon, VA
1234
President
1
Thomas Jefferson
37
1200
Montecello, VA
5678
Writer
2
4


This input starts by clearing the file. Then it inputs 2 records and find finally it displays all records in the file.
where would I place this code? Thanks
And how would I modify input to write variables writeemployee & read records using reademployee? Thank you so much mate. Still so new to C++ so i struggle with some simple aspects of it
These are two new functions. Put them right after main() and before editFile.

You read a record by calling readEmployee(employee, name, age, salary, address, id, jobstatus); where employee is the input stream for the file. To write the a record, call
writeEmployee(employee2, name, age, salary, address, id, jobstatus);

I know I'm being a little bit vague with all this. That's because it's so important to learn to do this stuff yourself. Programming is like football: you learn it by doing it.
Thanks, so where would I put reademployee/writeemployee if the bool code goes between main and editfile? because several errors are displayed about functions not being initialized when I write the code now.
Last edited on
Thanks
Last edited on

EXAMPLE

 Entire employee database
Name
Age
Salary
Address
ID
Job Status
-----------------------------------------
Rashid.Hussein 21 $30000 74.Copperfield.Street 29999 Part-Time
Rashid.Hussein 21 $30000 74.Copperfield.Street 37651 Part-Time
Press any key to continue . . .
Last edited on
Trolling?

You have deleted the ode from the original post, and pasted it again seemingly unchanged, still with calls to main. So that is time wasting == definition of Troll.
No. My teacher told me I could put a period between the names because this is a beginners course for programming.
Pages: 12