Everything seems to check out in my code; I've checked syntax on the different functions and everything seems to be checking out but I still seem to be getting the error 'No Matching Function for Call to 'MyInput'' in XCode with no explanation. Any help would be appreciated! Code follows.
#include <iostream>
#include <iomanip>
using namespace std;
typedef char ShortName[10];
typedef char LongName[17];
typedef char IDarray[6];
void MyInput(ShortName [],char [], LongName [], float [],int [],IDarray [],int&);
int main(int argc, const char * argv[])
{
const int max = 7;
const int ConstStateTax = .05;
const int ConstFedTax = .15;
const int ConstUnionFees = .02;
ShortName firstname[max];
char MI[max];
LongName lastname[max];
float hourrate[max];
int OTHours[max];
float Gross[max];
float Overtime[max];
float GGross[max];
float StateTax[max];
float FedTax[max];
float UnionFees[max];
IDarray EmployeeID[max];
float Net[max];
MyInput(firstname,MI,lastname,hourrate,OTHours,EmployeeID,max);
return 0;
}
void MyInput(ShortName firstname[],char MI[],LongName lastname[],float hourrate[],int OTHours[],IDarray EmployeeID[],int &max)
{
for(int i = 0;i<=max;i++)
{
cout << "Please enter the first name of employee #" << i+1 << ": ";
cin >> firstname[i];
cout << "Please enter the middle initial of employee #" << i+1 << ": ";
cin >> MI[i];
cout << "Please enter the last name of Employee #" << i+1 << ": ";
cin >> lastname[i];
cout << "What is the ID number of Employee #" << i+1 << "? (6 letters or numbers only): ";
cin >> EmployeeID[i];
cout << "What is the hourly rate of employee #" << i+1 << "? ";
cin >> hourrate[i];
while (hourrate[i] < 0)
{
cout << "Invalid rate, please try again: ";
cin >> hourrate[i];
}
cout << "How many overtime hours does employee #" << i+1 << " have? ";
cin >> OTHours[i];
while(OTHours[i]<0 || OTHours[i] >20)
{
cout << "Invalid Overtime Hours, please re-enter: ";
cin >> OTHours[i];
}
}
}
the function you are trying to call is, which takes int& as last argument
void MyInput(ShortName [],char [], LongName [], float [],int [],IDarray [],int&);
but you pass const int max to it, which have type of const int and can't convert to int&
to fix it, change int& to int for both method declaration and definition
Related
I am looking to remove the space from the name part of my assignment which allows users to put any name with or without space in my product list. for example "TV stand", I assume getline function will help me with it but I couldn't add the getline function into my main. can anyone help me with it?
#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
struct product {
char product_name;
int no_of_purchase;
int no_of_sales;
double purchase_cost;
double selling_price;
double profit_loss;
double percent_profit_loss;
string product_sales;
};
bool Compare(product P1, product P2)
{
return P1.percent_profit_loss > P2.percent_profit_loss;
}
int main()
{
int n;
cout << "Enter the number of the product:";
cin >> n;
cout << "\n";
product Products[n];
for (int i = 0; i < n; ++i) {
cout << "Enter the name of the product: ";
cin >> Products[i].product_name;
cout << "Enter the number of " << Products[i].product_name << " purchased: ";
cin >> Products[i].no_of_purchase;
cout << "Enter the number of " << Products[i].product_name << " sold: ";
cin >> Products[i].no_of_sales;
To be honest I don't understand your question but this might be helpful for you
I changed the product_name data type from char to string because the char data type is not making any sense
#include <iostream>
#include <string>
using namespace std;
struct product {
string product_name;
int no_of_purchase;
int no_of_sales;
double purchase_cost;
double selling_price;
double profit_loss;
double percent_profit_loss;
string product_sales;
};
bool Compare(product P1, product P2)
{
return P1.percent_profit_loss > P2.percent_profit_loss;
}
int main()
{
int n;
cout << "Enter the number of the product:";
cin >> n;
cin.ignore();
cout << "\n";
product Products[n];
for (int i = 0; i < n; ++i) {
cout << "Enter the name of the product: ";
getline(cin, Products[i].product_name);
cout << "Enter the number of " << Products[i].product_name << " purchased: ";
cin >> Products[i].no_of_purchase;
cout << "Enter the number of " << Products[i].product_name << " sold: ";
getline(cin, Products[i].no_of_sales);
cin.ignore();
}
return 0;
}
I'm working on a program in C++ to make schedules for me, since I have a hard time focusing on school work. The code will make priorities of assignments based on the due dates, duration to do the assignments, etc.
Anyway, I've run into a massive problem. I'm trying to assign a class variable's name to a variable! For example:
string a = "assignmentName" Class a(); //a is a variable and it's supposed to put the class's name as assignmentName
If you don't think this way will work, please tell me other solutions!
Also, I'm still a beginner, so please try to explain a little simple!
Here's the code:
#include <string>
#include <iostream>
using namespace std;
class Assignment
{
private:
int duration = 30; //Amount of time per assignment (in minutes)
int due = 1; //Amount of days till its due, if it's one then the assignment is due in 1 days
string name; //Name of assignment
public:
Assignment(int, int, string);
~Assignment();
void setDuration(int duration) {this -> duration = duration;}
void setDue(int due) {this -> due = due;}
void setDue(int name) {this -> name = name;}
int getDuration() { return duration; }
int getDue() { return due; }
string getName() { return name; }
};
Assignment::Assignment (int duration, int due, string name) //Constructor Method
{
this -> duration = duration;
this -> due = due;
this -> name = name;
}
Assignment::~Assignment() //Destructor Method
{
cout << "Assignment Object Destroyed" << endl;
}
int main()
{
int assignmentAmount;
int i;
int userInput1;
int userInput2;
string userInput3;
cout << "Enter assignment amount: ";
cin >> assignmentAmount << "\n" << endl;
for (i = 0; i < assignmentAmount; i++){
cout << "Enter assignment" << i <<"'s " << "duration: ";
cin >> userInput1 << endl;
cout << "Enter assignment" << i << "'s " << "due date in days from now: ";
cin >> userInput2 >> endl;
cout << "Enter assignment" << i <<"'s " << "name: ";
cin >> userInput3 >> endl;
Assignment i(userInput1, userInput2, userInput3); //I is the class's number and it's supposed to be a variable
}
return 0;
}
What you are looking for is an array of Assignment objects, eg:
int main()
{
Assignment *assignments;
int assignmentAmount;
int userInput1;
int userInput2;
string userInput3;
cout << "Enter assignment amount: ";
cin >> assignmentAmount << "\n" << endl;
assignments = new Assignment[assignmentAmount];
for (int i = 0; i < assignmentAmount; i++){
cout << "Enter assignment " << i << "'s duration: ";
cin >> userInput1 << endl;
cout << "Enter assignment " << i << "'s due date in days from now: ";
cin >> userInput2 >> endl;
cout << "Enter assignment " << i <<"'s name: ";
cin >> userInput3 >> endl;
// either:
assignments[i] = Assignment(userInput1, userInput2, userInput3);
// or:
assignments[i].setDuration(userInput1);
assignments[i].setDue(userInput2);
assignments[i].setName(userInput3);
}
// use assignments and assignmentAmount as needed...
delete[] assignments;
return 0;
}
Though, you should use std::vector instead of new[] directly:
#include <vector>
...
int main()
{
std::vector<Assignment> assignment;
int assignmentAmount;
int userInput1;
int userInput2;
string userInput3;
cout << "Enter assignment amount: ";
cin >> assignmentAmount << "\n" << endl;
for (int i = 0; i < assignmentAmount; i++){
cout << "Enter assignment " << i << "'s duration: ";
cin >> userInput1 << endl;
cout << "Enter assignment " << i << "'s due date in days from now: ";
cin >> userInput2 >> endl;
cout << "Enter assignment " << i << "'s name: ";
cin >> userInput3 >> endl;
// either:
assignments.push_back(Assignment(userInput1, userInput2, userInput3));
// or:
assignments.emplace_back(userInput1, userInput2, userInput3);
}
// use assignments as needed...
return 0;
}
Or:
#include <vector>
...
int main()
{
std::vector<Assignment> assignment;
int assignmentAmount;
int userInput1;
int userInput2;
string userInput3;
cout << "Enter assignment amount: ";
cin >> assignmentAmount << "\n" << endl;
assignments.resize(assignmentAmount);
for (int i = 0; i < assignmentAmount; i++){
cout << "Enter assignment " << i << "'s duration: ";
cin >> userInput1 << endl;
cout << "Enter assignment " << i << "'s due date in days from now: ";
cin >> userInput2 >> endl;
cout << "Enter assignment " << i << "'s name: ";
cin >> userInput3 >> endl;
// either:
assignments[i] = Assignment(userInput1, userInput2, userInput3);
// or:
assignments[i].setDuration(userInput1);
assignments[i].setDue(userInput2);
assignments[i].setName(userInput3);
}
// use assignments as needed...
return 0;
}
You can't get variable names as a variable, since a variable is basically like an alias to the data, when your program gets compiled all these names, don't exist anymore.
The compiler does not emit symbol names to the executable (the debug version may have symbol names).
If you want to associate or map symbol names to variables, you will need to declare the variable global and place the address into some kind of table.
Example:
int my_global_int_a;
std::map<std::string, int *> variable_dictionary;
variable_dictionary["my_global_int_a"] = &my_global_int_a;
// Here's how to get the variable
const std::string variable_name = "my_global_int_a";
int * p_variable = nullptr;
p_variable = variable_dictionary[variable_name];
std::cout << *p_variable << std::endl;
You may want to give deep thought to the need of retaining variable names.
I want to call the void getInput function in the main scope. But when I do this, it tells me:
too few argument in function call.
How do I fix this?
The first void function prints the exercises. Then I call it in the next void function called getInput. After that I just want to call it in the main() function.
#include <iostream>;
#include <string>;
using namespace std;
void Exercices()
{
double speed;
int minutes;
cout << "walking: ";
cin >> speed >> minutes;
cout << "running: ";
cin >> speed >> minutes;
cout << "cycling: ";
cin >> speed >> minutes;
}
void getInput(string username)
{
double weight, goal;
string walking, running, cycling;
cout << "Please enter your name: ";
cin >> username;
cout << "Welcome " << username << ", please enter your weight(kg): ";
cin >> weight;
cout << username << ", please enter speed(km/h) and minutes spent in a week for the activities below." << endl;
Exercices();
cout << username << ", please enter your weekly calorie burn goal: ";
cin >> goal;
}
int main()
{
//string user_info;
getInput();
Exercices();
cout << endl;
return 0;
}
As the error suggests,
int main()
{
string user_info;
getInput(user_info);
Exercices();
cout << endl;
return 0;
}
You have to pass a string to the function getInput(string username) since the function definition says it needs one. I hope you will read and try to understand the error message before everything else in the future
I'm new to programming (in general) and C++ (in particular) and currently learning classes and objects.
I've defined the following as an exercise:
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
class X
{
public:
void setNum_1(int);
void setNum_2(int);
void setNum_3(int);
void setNum_4(int);
double getNum_1();
double getNum_2(int num_1);
double getNum_3(int num_1, int num_2);
double getNum_4(int num_1, int num_2, int num_3);
private:
int num_1;
int num_2;
int num_3;
int num_4;
};
int main()
{
X testObject;
int lNum_1 = 0;
int lNum_2 = 0;
int lNum_3 = 0;
int lNum_4 = 0;
cout << endl;
cout << "Please enter an integer: ";
cin >> lNum_1;
cout << "Please enter an integer: ";
cin >> lNum_2;
cout << "Please enter an integer: ";
cin >> lNum_3;
cout << "Please enter an integer: ";
cin >> lNum_4;
testObject.setNum_1(lNum_1);
testObject.setNum_2(lNum_2);
testObject.setNum_3(lNum_3);
testObject.setNum_4(lNum_4);
cout << endl;
cout << "The 1st number returned is: " << testObject.getNum_1() << endl;
cout << "The 2nd number returned is: " << testObject.getNum_2(lNum_1) << endl;
cout << "The 3rd number returned is: " << testObject.getNum_3(lNum_1, lNum_2) << endl;
cout << "The 4th number returned is: " << testObject.getNum_4(lNum_1, lNum_2, lNum_3) << endl;
cout << endl;
return 0;
}
void X::setNum_1(int n_1)
{
num_1 = n_1;
}
void X::setNum_2(int n_2)
{
num_2 = n_2;
}
void X::setNum_3(int n_3)
{
num_3 = n_3;
}
void X::setNum_4(int n_4)
{
num_4 = n_4;
}
double X::getNum_1()
{
return sqrt(num_1);
}
double X::getNum_2(int num_1)
{
return pow(num_2,3);
}
double X::getNum_3(int num_1, int num_2)
{
return num_1 * num_2;
}
double X::getNum_4(int num_1, int num_2, int num_3)
{
return (num_1 + num_2) / num_3;
}
Can anyone offer some guidance on how to modify this class so that it has only one member function with all of the arguments defaulted?
Thanks in advance,
Ryan
Ofcourse there are MANY ways to do this and most of them will be more elegant than what I have done here. But this should give you some ideas.
a. You should use the variables defined in the class
b. If you need to perform certain conditional operations in a function, use a switch case or an if statement. And decide the operation based on a parameter passed.
Again, many ways to make this more elegant, but this should get you started and thinking.
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
class X
{
public:
void setNums(int, int, int, int);
double performOp(int);
private:
int num_1;
int num_2;
int num_3;
int num_4;
};
int main()
{
X testObject;
int lNum_1 = 0;
int lNum_2 = 0;
int lNum_3 = 0;
int lNum_4 = 0;
cout << endl;
cout << "Please enter an integer: ";
cin >> lNum_1;
cout << "Please enter an integer: ";
cin >> lNum_2;
cout << "Please enter an integer: ";
cin >> lNum_3;
cout << "Please enter an integer: ";
cin >> lNum_4;
testObject.setNums(lNum_1,lNum_2,lNum_3,lNum_4);
cout << endl;
cout << "The 1st number returned is: " << testObject.performOp(1) << endl;
cout << "The 2nd number returned is: " << testObject.performOp(2) << endl;
cout << "The 3rd number returned is: " << testObject.performOp(3) << endl;
cout << "The 4th number returned is: " << testObject.performOp(4) << endl;
cout << endl;
return 0;
}
void X::setNums(int n_1, int n_2, int n_3, int n_4)
{
num_1 = n_1;
num_2 = n_2;
num_3 = n_3;
num_4 = n_4;
}
double X::performOp(int n)
{
if(n == 1) return sqrt(num_1);
if(n == 2) return pow(num_2,3);
if(n == 3) return num_1 * num_2;
if(n == 4) return (num_1 + num_2) / num_3;
}
Any help would be greatly appreciated, my program quits as soon as i come out of the menu and try to enter something, been racking my brains trying to figure this out and is very annoying as i cant get anything else done until i fix this problem. i am a bit of a begginer at c++ so dont slate me if its a rookie mistake please haha!
This is the source code, its not yet a completed program just cant figure out whats wrong just now.
Thanks for any help!
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
struct cust
{
int employeeno, deptno;
char fname[10], sname[10], weekend[10];
float hours, othours, rate, otrate, normalpay, otpay, grosspay, netpay, totalni, totaltax, ni, tax;
};
int Menu(int& menuchoice);
void InputRecords(cust c[], int row, int menuchoice);
int Calculations(cust c[]);
int SearchNumber(cust c[], int &row);
int DeleteRecords();
int TotalPay();
int main()
{
struct cust c[100];
int menuchoice, row;
Menu(menuchoice);
if (menuchoice == 1){
system("CLS");
InputRecords(c, row, menuchoice);
}
if (menuchoice == 2){
system("CLS");
SearchNumber(c, row);
}
if (menuchoice == 3){
system("CLS");
DeleteRecords();
}
if (menuchoice == 4){
system("CLS");
}
if (menuchoice == 5){
system("CLS");
exit(5);
}
//Calculations(cust c[]);
}
int Menu(int& menuchoice){
cout << " \n\n\n\n\n 1. Input a Payslip" << endl << endl;;
cout << " 2. Read a Payslip " << endl << endl;
cout << " 3. " << endl << endl;
cout << " 4. " << endl << endl;
cout << " 5. Quit the Program" << endl << endl;
cin >> menuchoice;
}
void InputRecords(cust c[], int row, int menuchoice){
char another;
do{
cout << "Please Enter Their Employee Number: " << endl;
cin >> c[row].employeeno;
cout << "Please Enter Their First Name: " << endl;
cin >> c[row].fname,9;
cout << "Please Enter Their Second Name: " << endl;
cin >> c[row].sname,9;
cout << "Please Enter Their Department Number 1 - 9: " << endl;
cin >> c[row].deptno;
cout << "Please Enter The Hours They Have Worked: " << endl;
cin >> c[row].hours;
if (c[row].hours >= 37.5){
cout << "Please Enter Any Overtime They Have Worked: " << endl;
cin >> c[row].othours;
}
cout << "Please Enter Their Rate of Pay: " << endl;
cin >> c[row].rate;
cout << "Please Enter The Date of the Week End (DD/MM/YYYY): " << endl;
cin >> c[row].weekend, 9;
row++;
cout << endl;
//Putting it in the file.
ofstream timesheetFile("Timesheet.txt", ios::app);
if(timesheetFile.is_open()){
cout << "File has been opened." << endl;
timesheetFile << c[row].employeeno << " " << c[row].fname << " " << c[row].sname << " " << c[row].deptno << " " << c[row].hours << " " << c[row].othours << " " << c[row].rate << " " << c[row].weekend << "\n" << endl;
timesheetFile.close();
}else{
cout << "Error! File is not open." << endl;
}
cout << "Would you like to enter another record? Y/N : ";
cin >> another;
cout << endl << endl;
}while(row<100 && another == 'y');
system("CLS");
main();
}
//read records
int SearchNumber(cust c[], int &row){
//system("CLS");
int empno;
cout << "Enter Employee Number : ";
cin >> empno;
for (int i=0; i < row; i++)
{
if (empno == c[i].employeeno){
system("CLS");
cout << c[i].employeeno << endl << c[i].fname << c[i].sname << endl;
}
}
}
//deleterecords
int DeleteRecords(){
}
//calculations
int Calculations(float normalpay, float& hours, float& rate, float otpay, float otrate, float& othours, float grosspay, float tax, float ni, float netpay, float totalni, float totaltax){
ni = 6.8 / 100;
tax = 12.75 / 100;
otrate = 1.5 * rate;
normalpay = hours * rate ;
otpay = otrate * othours;
grosspay = normalpay + otpay;
totalni = grosspay * ni;
totaltax = tax * grosspay;
netpay = normalpay + otpay - totaltax - totalni;
// cout << totaltax << endl;
//
// cout << totalni << endl;
//
// cout << netpay << endl;
}
int TotalPay(){
}
The problem is here
int main()
{
struct cust c[100];
int menuchoice, row;
Menu(menuchoice);
if (menuchoice == 1){
system("CLS");
InputRecords(c, row, menuchoice);
}
You have not given the variable row a value but you use row when you call InputRecords.
From a look at your code it seems to me that the row variable should be moved to the InputRecords function and initalised to zero there. I can't see why you have the row variable in the main function.
Also I can't see why you pass menuchoice to InputRecords, it doesn't get used there. It all seems a bit random, maybe you should review functions and parameter passing.
Looks like your row variable is never being initialized. Why is this?
It's also good practice to initialize your variables like menuchoice
int Menu(int& menuchoice);
void InputRecords(cust c[], int row, int menuchoice);// declared
int Calculations(cust c[]);
int SearchNumber(cust c[], int &row);
int DeleteRecords();
int TotalPay();
int main()
{
struct cust c[100];
int menuchoice, row; // declared again but never initialized
Menu(menuchoice);
if (menuchoice == 1){
system("CLS");
InputRecords(c, row, menuchoice); // used