1

Please read my code, and help me to debug it. because Dev-C++ finds a lot of errors....

#include<iostream.h>
#include<conio.h>
using namespace std;

struct iWorker{
       double salary;
}

double calSalary(iWorker worker){
    double money = worker.salary;
    return money;
}

int main(){
    iWorker worker;

    cout << "Enter salary: ";
    cin >> worker.salary;

    double salary = calSalary(worker, 0);
    cout << salary;

    getch();
    return 0;
}

and errors:

9: error: new types may not be defined in a return type
9: error: two or more data types in declaration of `calSalary'
 In function `iWorker calSalary(iWorker)':
11: error: conversion from `double' to non-scalar type `iWorker' requested

 In function `int main()':
9: error: too many arguments to function `iWorker calSalary(iWorker)'
20: error: at this point in file
20: error: cannot convert `iWorker' to `double' in initialization

Thank you ...

mrdaliri
  • 7,148
  • 22
  • 73
  • 107
  • 2
    `` predates standardisation of C++ (http://stackoverflow.com/questions/214230/iostream-vs-iostream-h-vs-iostream-h) - 1998!. Any new code definitely shouldn't be using it any books which use it are *horribly* outdated! – Flexo Oct 13 '11 at 19:50
  • `calSalary` just take a single parameter. In your call to the function, why are you passing an extra parameter **0** to the function? Is it a typo ? – Mahesh Oct 13 '11 at 19:54
  • @Mahesh, yes, I want to check how functions work. (and i removed it) – mrdaliri Oct 13 '11 at 20:06
  • 1
    Please, get rid of Dev-C++ as soon as you can. Read the [Dev-C++ tag info](http://stackoverflow.com/tags/dev-c%2b%2b/info). – R. Martinho Fernandes Oct 13 '11 at 20:07
  • @R.MartinhoFernandes, OK, i have installed Dev on my windows, in linux i have qt. but i should install it on windows too. – mrdaliri Oct 13 '11 at 20:12
  • Did you grab a 10quick year old C++ book out of the garbage with a free Dev++ floppy in it? – John Dibling Oct 13 '11 at 22:08

3 Answers3

5

You're missing a ; after your struct declaration:

struct iWorker{
   ...
};  // <- this one here
sth
  • 222,467
  • 53
  • 283
  • 367
3
struct iWorker{
       int id;
       char name[100];
       float hours;
       double salary;
} <--- missing semicolon here ...
jpalecek
  • 47,058
  • 7
  • 102
  • 144
1

Damn, I was too late. But you also have a possible buffer overrun in this line:

cin >> worker.name;

Why don't you use std::string?

znkr
  • 1,746
  • 11
  • 14