How do I display the output of my C++ class - c++

Hello guys I am trying to create a date class in C++ where it display the default date, displays a short set date, displays a long set date, just display a set year. However, I am trying this display the contents of the class when I input the data but I am getting an error "Use of undeclared identifier 'setDate'
Here is the header file code:
#ifndef dateClass_h
#define dateClass_h
using namespace std;
class DateObj
{
public:
int setDate(int month, int day, int year);
void shortDate(int month, int day, int year);
void longDate(string month, int day, int year);
//return the year
int getYear(int year){
return year;
}
private:
int month;
int day;
int year;
};
#endif /* dateClass_h */
Here is my implementation file code:
#include <stdio.h>
#include <iostream>
#include <string>
#include "dateClass.h"
using namespace std;
//setDate Method
int DateObj::setDate(int month, int day, int year){
//setMonth = month;
//day = setDay;
//year = setYear;
//check to make sure month is between 1 - 12.
if(month < 1 || month > 12){
cout << "This is an invalid Month. Please enter a month that is between 1 and 12: " << endl;
}
//check to make sure day is between 1 -31.
if(day < 1 || day > 31){
cout << "This is an invalid Day. Please enter a day that is between 1 and 31: " << endl;
}
//check to make sure year is greater than zero.
if(year < 0){
cout << "This is an invalid Year. Please enter a year that is greater than Zero" << endl;
}
return setDate(month,day,year);
}
And here is my main program code:
#include <iostream>
#include <string>
#include "dateClass.h"
using namespace std;
int main() {
DateObj myDate;
myDate.setDate(12, 25, 2016); //set the date to Dec 25, 2016.
cout << "The current date is: " << setDate() << endl;
return 0;
}
I will just like to know why I am getting this error and what I will need to fix in my class or main program.
Edit
I got the program to compile butI got the following error "EXC_BAD_ACCESS (code=2, address=0x7fff5f3fffe8) at the return setDate(month, day, year) break point.
#include <iostream>
#include <string>
#include "dateClass.h"
using namespace std;
int main() {
DateObj myDate;
myDate.setDate(12,25,2016); //set the date to Dec 25, 2016.
cout << "The current date is: " << myDate.setDate(12, 25, 2016) << endl;
return 0;
}

There appears to be no implementation for your shortDate function.
You have it in your header file, but you don't appear to have it in your dateClass.cpp file. You need to implement it in your dateClass.cpp file similar to how you did for setDate.
You're getting the error because it can't find any implementation.

The reason you are getting an error is because setDate() is a void function. When you try to use setDate() in the line:
cout << "The current date is: " << setDate() << endl;
you get an error because setDate() needs to return an std::ostream & object or some other data that is convertible. I recommend using setDate(int month, int day, int year) purely to set the data members of your class and create a separate function to actually print out the values.
As an alternative, you could overload setDate() so that it returns an acceptable data type, like so:
#include <iomanip>
//the parameter 'right' is required for chaining together different << when forming output
std::ostream & DateObj::setDate(std::ostream & right) {
//displays the date in mm/dd/yyyy format
//setfill defines a character to fill empty spaces created by setw
right << std::setfill('0') << std::setw(2) << month
<< '/' << std::setfill('0') << std::setw(2) << day
<< '/' << std::setfill('0') << std::setw(4) << year;
return right;
}

Well, for one thing you're calling setDate() inside setDate() in such a way that it will infinitely just call itself. You'll call setDate(), which will call setDate(), which will call setDate() and so on. I'm kind of a noob too, but I'm pretty sure what you're getting is a stack overflow error (Hey, title drop! :D). A stack overflow happens when you allocate too many variables and run out of memory. So because you're calling setDate() infinitely, you are also making infinite versions of the variables in it, and filling up all your memory.
For another, set date doesn't currently actually do anything. It checks to see if you have valid input, then calls itself again. From the name, I assume it's meant to populate your member variables? If that's what it's supposed to do, it should probably look more like this:
int DateObj::setDate(int month, int day, int year){
//check to make sure month is between 1 - 12.
if(month < 1 || month > 12){
cout << "This is an invalid Month. Please enter a month that is between 1 and 12: " << endl;
}
//check to make sure day is between 1 -31.
if(day < 1 || day > 31){
cout << "This is an invalid Day. Please enter a day that is between 1 and 31: " << endl;
}
//check to make sure year is greater than zero.
if(year < 0){
cout << "This is an invalid Year. Please enter a year that is greater than Zero" << endl;
}
//the part that actually sets the variables. Aka, the important bit.
month_ = month;
day_ = day;
year_ = year
}
The trailing underscores on month, day and year are just style things, I like to use them for member data so that I can use those names again as local variables in my functions, which was useful here if you noticed. Once you've put the data into your variables, it should be pretty easy to display everything. Just do something like:
cout << month_ << "/" << day_ << "/" << year_ << endl;
That will output your date to the console in a format something like this: 3/12/2016.

Related

C++ sum of digits of each element in a column

I am new to C++, trying to import dates into a program, adding up digits of day, month, year resp and writing back to txt.
input data
sl.no name day month year
1 Rob 15 05 2019
2 Tim 12 06 2002
Desired output data in txt
sl.no name day month year
1 Rob 6 5 3
2 Tim 3 6 4
I have been able to import data from a txt file and also add the digits in day but it does not repeat forward. what am i doing wrong ?
sample code
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream theFile("data.txt");
int id,day,month,year,daysum=0,monthsum=0, yearsum=0;
string name;
while (theFile >> id >> name >> day >> month >> year)
{
cout << id << ", "<< name <<", "<< day<<", "<<month <<", "<< year<<","<< endl;
}
while (day > 0)
{
daysum = daysum + (day % 10);
day = day / 10;
cout << daysum << endl;
}
I am no expert . but have been in your spot a few months ago.. break down the problem into smaller steps..
My approach..
Pseudo Code:
Ditch the header
Create a function for adding the digits
Read data from file
Use a loop to run through each element of the every column and use the function created
Store results in a variable
Output variable to a new text file
comment if there is a specific area where you are stuck..
Try this to reduce it to single digits.. knit to other parts of your code..
#include <iostream>
using namespace std;
int main()
{
long long num;
cout << "Enter a number: ";
cin >> num;
int sum = 0;
while (1)
{
sum += (num % 10);
num /= 10;
if (0 == num)
{
if (sum > 9)
{
num = sum;
sum = 0;
}
else
{
cout << "Answer: ";
cout << sum << endl;
return 0;
}
}
};
return 0;
}
you are reading the file and data wrong,
you need to discard the header (sl.no name day month year)
and then accumulate the daysum while reading the file progressively one row after the other until the end...

My Payroll program outputs the wrong calculation for the total of salary

I am currently working on a prototype payroll system.
Every time I put the required information for the calculations, it always outputs the wrong information (I always check with a calculator).
It doesn't have any errors or warnings so I think the problem is how I format the code.
Can someone please teach me how to fix this problem?
Note: The program is incomplete.
#include <iostream>
#include <vector>
#include <fstream>
#ifdef _WIN32
#include <windows.h>
#define SYSERROR() GetLastError()
#else
#include <errno.h>
#define SYSERROR() errno
#endif
struct Employees//Not in use yet
{
std::string first_name;
std::string last_name;
};
class Salary_variables
{
public:
long days = 0;
long overtime = 0;
long basic_pay = 537;
long overtime_pay = 80.1375;
long regular_salary = days * basic_pay;
long compute()//constructor for determining the employee's salary
{
if (overtime == 0)
{
return regular_salary;
}
else if (overtime >= 0)
{
long overtime_salary = overtime * overtime_pay;
return regular_salary + overtime_salary;
}
}
};
int main()
{
Employees employee;
std::cout << "Type in employee's full name:\n";
std::cin >> employee.first_name >> employee.last_name;
Salary_variables variables;
std::cout << "Type in total amount of days worked:\n";
std::cin >> variables.days;
std::cout << "Type in total amount of overtime hours worked:\n";
std::cin >> variables.overtime;
std::cout << "Total salary: " << variables.compute() << "\n";
return 0;
}
I can see 2 big mistakes in your code, but without examples of input and output it is possible I missed more.
The biggest error is that you are using long - which is an integer type!
You need to use double so that the decimal part of the salary will be preserved in the calculation.
For example, using long 10 hours of overtime would be worth 80 * 10 = 800.
But, with the correct calculation using double, 80.1375 * 10.0 = 801.375.
Also, as others already mentioned, you must do the multiplication inside the compute function.
Member variable initialization happens only once when you create an instance of the class.
So your code multiplies the salary by 0 on line Salary_variables variables;.
If you change the days variable afterwards, nothing will happen.
The below initialization should be done inside compute function:
long regular_salary = days * basic_pay;
Otherwise, it will initialize the variable with default values when creating class variable.

calculate x years for savings with deposition and revenue C++

I'm sitting with a challenging homework in C++ and would be really thankful for some help here!
My program need to calculate how many years it will take for an optional yearly deposition with revenue to reach a specific savings limit.
I just can't see what's wrong and have tried debugging with no help.
It doesn't really help neither that i'm totally new to C++ and MVS 2015.
I don't know if it's the math or the programming itself that is wrong.
Static typing is foreign to me since I usually use python.
Also VS don't give much information and the program just stops after asking for revenue input.
Any suggestions?
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
int deposit;
int max_savings;
double revenue;
double change_factor;
double year = 0;
double geometric_sum;
cout << "Choose a yearly deposition:\n";
cin >> deposit;
cout << "Set your max saving-goal:\n";
cin >> max_savings;
cout << "set a revenue in percent:\n";
cin >> revenue;
change_factor = 1 + (revenue / 100);
geometric_sum = ((double)deposit * (pow(change_factor, year) - 1)) / (change_factor - 1);
while (geometric_sum < max_savings)
year++;
cout << "Your saving-goal will be in " << year << " years!" << endl;
cout << "Your account balance will then be " << geometric_sum << " dollars!" << endl;
return 0;
}
pow(change_factor, year) - 1
year is set to 0. Any value at the power of 0 is 1. 1 - 1 = 0. Basically you are multiplying with 0.

How do I call a function from another .cpp file into the file where my int main() is? [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
So my program requires me to make a function in one file, and call it into another.
I have one file called convertdays.cpp like this:
#include <iostream>
int convertdays(int, int, int);
int convertdays(int month, int day, int year)
{
int date;
date = year * 1000 + month * 100 + day;
return date;
}
Then I have another file where my int main() stuff is, like this:
#include <iostream>
using namespace std;
int main()
{
int day, month, year;
cout << "Please enter the month: " << endl;
cin >> month;
cout << "Please enter the day: " << endl;
cin >> day;
cout << "Please enter a year: " << endl;
cin >> year;
convertdays(month, day, year); //convertdays stays in red though.
//Still need to add a couple of more things but not necessary right now.
system("Pause");
return 0;
}
How do I make this work where I can keep all of my functions in another file and call them in when I need them?
Make a file called "convertdays.h", containing the function declaration:
int convertdays(int, int, int);
This is called a header file.
Then at the top of main.cpp:
#include "convertdays.h"
(It's a good idea to put the same thing at the top of convertdays.cpp, though not strictly necessary.)
Then when you build the executable, link main.o and convertdays.o.

C++ argv[] not being passed fully

// Type the determine year in the command line as an argument.
// This program then prints the months and days for that year.
//Known Error argv[1] = the first digit of year,
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
void printYear(int year);
int _tmain(int argc, char *argv[]){
string str;//varible used to exit the program
if (argc == 1 ){//checks to see if the years were inputted corrected
std::cout << "Please input Year in the command line. Exiting.." << std::endl;
cout << "Please type anything to continue..." << endl;
cin >> str;
return 1;
}
int Year = 1982;
int numYears = argc-1;
cout << "Number of Argments Loaded : " << numYears << endl;
for (int x = 1; x <= numYears; x++){
Year = atoi(argv[x]);
cout << "Year : " << argv[x] << endl;
cout << "Year is " << Year << endl;
printYear(Year);
}
cout << "Please type anything to continue..." << endl;
cin >> str;
return 0;
}
I'm currently learning C++ and this is one of my assignments. I just spent a better half of a day looking into this to no avail.
printYear() has been tested with numerous years and is functional. The only remanding error left is with argv[]. It only returns the first digit of the year inputted, which is fine if you want to research years 0-9. Any tips or trick you guys mind passing me? (I'm using Microsoft Visual Studio fyi)
Command line
calender.exe 1982
returns with
Number of Arguments Loaded : 1
Year : 1
Year is 1
Repetitive code I know but I'm troubleshooting.
The problem is _tmain. If you have unicode enabled it tries to give you wide (UTF-16) characters, so every other character will be \0. To fix this you want to call it main instead.
It seems that the arguments are passed as UNICODE strings however you process them in the program as ASCII strings.