So i was typing my code about this payment center using classes, throughout my run i managed to fix the compiler error and once there was no more compiler errors, everytime i run my program and when it reaches the part where it displays the information entered it suddenly crashes. i dont have any idea what might be causing this. i am a newbie at programming btw and i am using Dev C++ (the one with the blue icon)
i am using windows 10 64bit(in case this info is necessary) i basically need to know whats causing this and how to fix it :(
#include <iostream>//i/o
#include <cmath>//math expressions
#include <iomanip>//i/o manipulations
#include <string>//string values
#include <fstream>//to read from a file
constdouble waterrate=17.63; //php per cu.m
constdouble elecrate=8.1081729;//php per kWh
usingnamespace std;
class info
{
protected:
string Lname;
string Fname;
string branch;
string add;
public:
string userinfo()
{
cout<<"\nenter User first name and last name:";
cin>>Fname>>Lname;
cout<<"\nEnter from which branch you are paying: ";
cin>>branch;
cout<<"\nEnter your Home address: ";
cin>>add;
}
void displayUser()
{
cout<<"\nUser name:\t"<<Fname<<" "<<Lname<<endl;
cout<<"\nPayment Branch: "<<branch<<endl;
cout<<"\nUser Address: "<<add<<endl;
}
};
class waterbill//inputs and compus of water
{
private:
double prevmetrRead, currntmetrRead;
public:
double wamnt;
public:
double wcost()
{
cout<<"enter your previous water reading: ";
cin>>prevmetrRead;
cout<<"enter your current water reading: ";
cin>>currntmetrRead;
wamnt=abs(prevmetrRead-currntmetrRead)*waterrate;
cout<<"your total water balance is : "<<wamnt<<" "<<"php"<<endl;
}
};
class electricbill//input and compus of electric bill
{
private:
double prevmetrRead, currntmetrRead;
public:
double eamnt;
public:
double ecost()
{
cout<<"enter your previous Electricity reading: ";
cin>>prevmetrRead;
cout<<"enter your current water reading: ";
cin>>currntmetrRead;
eamnt=abs(prevmetrRead-currntmetrRead)*elecrate;
cout<<"your total water balance is : "<<eamnt<<" "<<"php"<<endl;
}
};
int main()//main function
{
info i;
info j;
i.userinfo();
j.displayUser();
return 0;
}
Well, you don't have to do it with main() because it's a bit special. I was more thinking of the three functions userinfo(), wcost() and ecost(). If you don't want them to return anything you should change the return type to void.
Just to clarify with what Peter87 already said: Getting user input (via cin) is not the same thing as returning a value from the function itself. Change your function's return type to void if you don't have return some_value; in the function itself.
WOW it was as simple as that thank you sir Peter87 your solution worked heres what i did EDIT: it did stop crashing although now it keeps on repeating the functions that i put those returns to haha i celebrated too early
#include <iostream>//i/o
#include <cmath>//math expressions
#include <iomanip>//i/o manipulations
#include <string>//string values
#include <fstream>//to read from a file
constdouble waterrate=17.63; //php per cu.m
constdouble elecrate=8.1081729;//php per kWh
usingnamespace std;
class info
{
protected:
string Lname;
string Fname;
string branch;
string add;
public:
string userinfo()
{
cout<<"\nenter User first name and last name:";
cin>>Fname>>Lname;
cout<<"\nEnter from which branch you are paying: ";
cin>>branch;
cout<<"\nEnter your Home address: ";
cin>>add;
return userinfo();
}
void displayUser()
{
cout<<"\nUser name:\t"<<Fname<<" "<<Lname<<endl;
cout<<"\nPayment Branch: "<<branch<<endl;
cout<<"\nUser Address: "<<add<<endl;
}
};
class waterbill//inputs and compus of water
{
private:
double prevmetrRead, currntmetrRead;
public:
double wamnt;
public:
double wcost()
{
cout<<"enter your previous water reading: ";
cin>>prevmetrRead;
cout<<"enter your current water reading: ";
cin>>currntmetrRead;
wamnt=abs(prevmetrRead-currntmetrRead)*waterrate;
cout<<"your total water balance is : "<<wamnt<<" "<<"php"<<endl;
return wcost();
}
};
class electricbill//input and compus of electric bill
{
private:
double prevmetrRead, currntmetrRead;
public:
double eamnt;
public:
double ecost()
{
cout<<"enter your previous Electricity reading: ";
cin>>prevmetrRead;
cout<<"enter your current water reading: ";
cin>>currntmetrRead;
eamnt=abs(prevmetrRead-currntmetrRead)*elecrate;
cout<<"your total water balance is : "<<eamnt<<" "<<"php"<<endl;
return ecost();
}
};
int main()//main function
{
info i;
info j;
i.userinfo();
j.displayUser();
return 0;
}
#include <iostream>//i/o
#include <cmath>//math expressions
#include <iomanip>//i/o manipulations
#include <string>//string values
#include <fstream>//to read from a file
constdouble waterrate=17.63; //php per cu.m
constdouble elecrate=8.1081729;//php per kWh
usingnamespace std;
class info
{
protected:
string Lname;
string Fname;
string branch;
string add;
public:
string userinfo()
{
cout<<"\nenter User first name and last name:";
cin>>Fname>>Lname;
cout<<"\nEnter from which branch you are paying: ";
cin>>branch;
cout<<"\nEnter your Home address: ";
cin>>add;
return Fname;
return Lname;
return branch;
return add;
}
void displayUser()
{
cout<<"\nUser name:\t"<<Fname<<" "<<Lname<<endl;
cout<<"\nPayment Branch: "<<branch<<endl;
cout<<"\nUser Address: "<<add<<endl;
}
};
class waterbill//inputs and compus of water
{
private:
double prevmetrRead, currntmetrRead;
public:
double wamnt;
public:
double wcost()
{
cout<<"enter your previous water reading: ";
cin>>prevmetrRead;
cout<<"enter your current water reading: ";
cin>>currntmetrRead;
wamnt=abs(prevmetrRead-currntmetrRead)*waterrate;
cout<<"your total water balance is : "<<wamnt<<" "<<"php"<<endl;
return wamnt;
}
};
class electricbill//input and compus of electric bill
{
private:
double prevmetrRead, currntmetrRead;
public:
double eamnt;
public:
double ecost()
{
cout<<"enter your previous Electricity reading: ";
cin>>prevmetrRead;
cout<<"enter your current water reading: ";
cin>>currntmetrRead;
eamnt=abs(prevmetrRead-currntmetrRead)*elecrate;
cout<<"your total water balance is : "<<eamnt<<" "<<"php"<<endl;
return eamnt;
}
};
int main()//main function
{
info i;
info j;
i.userinfo();
j.displayUser();
return 0;
}
well i managed to find my way around it by removing my displayUser() function and putting it all in the userinfo() function, thanks for the helps. heres the code for reference purposes for those who encounter the same problem i have. cheers!
#include <iostream>//i/o
#include <cmath>//math expressions
#include <iomanip>//i/o manipulations
#include <string>//string values
#include <fstream>//to read from a file
constdouble waterrate=17.63; //php per cu.m
constdouble elecrate=8.1081729;//php per kWh
usingnamespace std;
class info
{
protected:
string Lname;
string Fname;
string branch;
string add;
public:
string userinfo()
{
cout<<"\nenter User first name and last name:";
cin>>Fname>>Lname;
cout<<"\nEnter from which branch you are paying: ";
cin>>branch;
cout<<"\nEnter your Home address: ";
cin>>add;
cout<<"\nUser name:\t"<<Fname<<" "<<Lname<<endl;
cout<<"\nPayment Branch: "<<branch<<endl;
cout<<"\nUser Address: "<<add<<endl;
return Fname;
return Lname;
return branch;
return add;
}
/*void info.userinfo()
{
cout<<"\nUser name:\t"<<Fname<<" "<<Lname<<endl;
cout<<"\nPayment Branch: "<<branch<<endl;
cout<<"\nUser Address: "<<add<<endl;
}*/
};
class waterbill//inputs and compus of water
{
private:
double prevmetrRead, currntmetrRead;
public:
double wamnt;
public:
double wcost()
{
cout<<"enter your previous water reading: ";
cin>>prevmetrRead;
cout<<"enter your current water reading: ";
cin>>currntmetrRead;
wamnt=abs(prevmetrRead-currntmetrRead)*waterrate;
cout<<"your total water balance is : "<<wamnt<<" "<<"php"<<endl;
return wamnt;
}
};
class electricbill//input and compus of electric bill
{
private:
double prevmetrRead, currntmetrRead;
public:
double eamnt;
public:
double ecost()
{
cout<<"enter your previous Electricity reading: ";
cin>>prevmetrRead;
cout<<"enter your current water reading: ";
cin>>currntmetrRead;
eamnt=abs(prevmetrRead-currntmetrRead)*elecrate;
cout<<"your total water balance is : "<<eamnt<<" "<<"php"<<endl;
return eamnt;
}
};
int main()//main function
{
info i;
i.userinfo();
return 0;
}
Just to demonstrate... There is a function called sqrt. When you pass a number as argument it will return the square root of that number. In order for the return value to be useful you need to do something with it, e.g. store it in a variable, use it in a calculation, or pass it as argument to another function. There is no point in calling sqrt if you do not use the returned value for anything.
1 2 3
double a = 5;
double b = sqrt(a);
cout << "The square root of " << a << " is " << b << ".\n";
Sometimes you don't want a function to return any values. Instead you just want it to do something and then return, without returning any value. In this case the return value should be void, which means the function will not return any value back to where the function was called from. I think this is the situation that you had in your original program. Instead of adding those return statements you should simply have changed the return types to void. You already had it right for the displayUser() function.