While loop in Math::getGradeTotal(string continuegrade) gives error - c++

So essentially what I'm trying to do here is have the user put in their grade information and then in a specific function ask if them if they would like to continue putting in grades. When I try this however I get an error with the while loop so I'm not quite sure what I'm doing wrong. I have provided all the code so that you guys can get a better understanding of what I'm trying to do. I get the error at "Math::getGradeTotal(string continuegrade)" and it says in the compiler
These are the errors they're spitting out in the compiler
Line 23: error: prototype for 'int Math::getGradeTotal()(std::_cxxll::string)' does not math any in class
Line 23: error: candidate is std::_cxxll::string Math::getGradeTotal()
#include <iostream>
#include <string>
#include "Math.h"
using namespace std;
string student_name;
double test_grade;
int main()
{
string continuegrade;
double gradeinput;
char courseinput;
cout << "Please enter students name: " << endl;
cin >> student_name;
cout << "Please select what course you would like to enter grades (1-
Math, 2-Science): "<< endl;
cin >> courseinput;
cout << "Please insert grade:" << endl;
cin >> gradeinput;
Math Student1(gradeinput, continuegrade);
if (courseinput == '1')
{
cout << "Grade put in:" << Student1.getGrade() << endl;
Student1.getGradeTotal();
}
if (courseinput == '2')
{
cout << "**You have selected Science**" << endl;
}
return 0;
}
Header File
#include <iostream>
#include <string>
using namespace std;
#ifndef MATH_H
#define MATH_H
class Math
{
public:
//default constructor
Math();
//overload constructor
Math(double , string);
//destructor
~Math();
//accessor functions
double getGrade();
//getGrade - gets the input of the grade
string getGradeTotal();
//getGradeTotal - takes the input and adds onto itself
private:
//Member variables
double new_gradeinput;
string new_continuegrade;
};
#endif // MATH_H
.CPP Source File
#include "Math.h"
Math::Math()
{
new_gradeinput = 0.0;
//ctor
};
Math::Math(double gradeinput, string continuegrade)
{
new_gradeinput = gradeinput;
//new_continuegrade = continuegrade;
};
Math::~Math()
{
};
double Math::getGrade()
{
return new_gradeinput;
}
Math::getGradeTotal(string continuegrade)
{
new_continuegrade = continuegrade;
while ((new_continuegrade == 'Y') || (new_continuegrade == 'y'))
{
cout << "Would you like to continue" << endl;
cin >> new_continuegrade;
}
/*cout << "Grade total: " << new_gradeinput << endl;
return new_gradeinput;*/
}
I know this is kind of long but any and all help will be greatly appreciated.

Related

Im getting 2 errors i cant resolve; 1 invalid use of member (did you forget the '&' ?) and recipe for target 'main.o' failed

When I compile the code I get an error on line 47:
[Error] invalid use of member (did you forget the '&' ?)
I’m unsure why, since I’m trying add score to the total_score.
The second issue is an error saying recipe for target 'main.o' failed. This is not in my code but brings up a new tab labeled makefile.win
here is my code:
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
class Student
{
public:
void student_name();
void quiz_score();
void total_score();
void average_score();
private:
int the_number_of_quizs;
int sum_of_scores;
double score_average;
};
void Student::student_name()
{
string name;
cout << "Please enter your name" << endl;
cin >> name;
cout << endl;
}
void Student::quiz_score()
{
cout << "What was your score on the quiz?: " << endl;
int score;
cin >> score;
total_score += score;
the_number_of_quizs++;
}
void Student:: average_score()
{
score_average= sum_of_scores/ the_number_of_quizs;
}
void Student:: total_score()
{
cout << "Total score: " << sum_of_scores << endl;
}
int main ()
{
Student student1;
student1.quiz_score();
student1.student_name();
student1.total_score();
student1.average_score();
return 0;
}
you have this void total_score()
total_score is a function returning void, that is why the following is invalid:
total_score += score;
I suspect you meant to use:
sum_of_scores += score;
If at all I understand what you were tryna do, then it seems your major problem was coming from the line you had:
total_score += score;
Running that will flag you this:
main.cpp:38:9: error: invalid use of member function ‘void Student::total_score()’ (did you forget the ‘()’ ?)
total_score += score;
^~~~~~~~~~~
It seems you wanna add the previous score of the student to the current score he/she has. If my assumptions were right, then I think total_score shouldn't be what you need, you will need sum_of_scores
This should end you up with the below as your new code:
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
class Student
{
public:
void student_name();
void quiz_score();
void total_score();
void average_score();
private:
int the_number_of_quizs;
int sum_of_scores;
double score_average;
};
void Student::student_name()
{
string name;
cout << "Please enter your name" << endl;
cin >> name;
cout << endl;
}
void Student::quiz_score()
{
cout << "What was your score on the quiz?: " << endl;
int score;
cin >> score;
sum_of_scores += score; //This is where you're correction is.
the_number_of_quizs++;
}
void Student:: average_score()
{
score_average= sum_of_scores/ the_number_of_quizs;
}
void Student:: total_score()
{
cout << "Total score: " << sum_of_scores << endl;
}
int main ()
{
Student student1;
student1.quiz_score();
student1.student_name();
student1.total_score();
student1.average_score();
return 0;
}
And when you run that, you should be able to get this:

Error expected constructor, destructor, or type conversion before '(' token

I have searched a ton of threads and cannot find a solution to this error. It occurs on line 8.
The BranchStaff.cpp file is as follows. It acts as a parent class for another class.
#include "BranchStaff.h"
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
BranchStaff::BranchStaff(userIDIn, passwordIn)
:userID(userIDIn), password(passwordIn)
{
menuChoice = 0;
over = false;
while (!over) {
cout << "=======================================================" << endl;
cout << "| Teller Terminal System - Branch Staff |" << endl;
cout << "=======================================================" << endl;
cout << "1) Client and Account Management" << endl;
cout << "2) Change password" << endl;
cout << "3) Exit"
cout << "\tPlease choose an option: ";
cin >> menuChoice;
while (menuChoice != 3 && menuChoice != 2 && menuChoice != 1) {
cout << "\tPlease enter a valid option: " << endl;
cin >> menuChoice;
}
switch (menuChoice) {
case 1:
clientManagement()
break;
case 2:
passwordChange()
break;
case 3:
exit();
}
}
}
void BranchStaff::changePassword() {
}
void BranchStaff::clientManagement() {
}
The .h file is as follows
#ifndef BRANCHSTAFF_H
#define BRANCHSTAFF_H
#include <string>
#include <iostream>
using namespace std;
class BranchStaff
{
public:
BranchStaff();
BranchStaff(string userIDIn, string passwordIn);
protected:
void clientManagement();
void changePassword();
private:
string userID;
string password;
int menuChoice;
bool over;
};
#endif // BRANCHSTAFF_H
Possibly due to not including data types in the implementation. Try
BranchStaff::BranchStaff(string userIDIn, string passwordIn)
I would also suggest passing in the strings by reference as using them in the initialization list should copy them.
BranchStaff::BranchStaff(const string& userIDIn, const string& passwordIn)
BranchStaff::BranchStaff(string userIDIn, string passwordIn)

Undefined reference in menu driven program with two function headers [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 6 years ago.
I am new to coding in C++, and trying to teach myself for class.
I need to create a menu driven program that will execute two functions, one to see if a string is a palindrome and the other to see find the greatest common denominator of two numbers.
I have a main.cpp, a GCD.cpp, a palindrome.cpp, a GCD.h and a palindrome.h. When I compile on the command line I get the following error:
/tmp/ccVf007n.o: In Function 'main': main.cpp: (.test+0x75): undefined reference to euclid(int, int);
collect2: error: ld returned 1 exit status.
My code blocks are:
main.cpp
#include <iostream>
#include "palindrome.h"
#include "GCD.h"
#include <string>
using namespace std;
void showChoices();
int x,y;
int main() {
int choice;
do
{
showChoices();
cin >> choice;
switch (choice)
{
case 1:
cout << "Palindrome Program.";
main();
break;
case 2:
cout << "Greatest Common Denominator Program.";
euclid(x,y);
break;
case 3:
break;
}
}while (choice !=3 );
}
void showChoices(){
cout << "Menu" << endl;
cout << "1. Palindrome Program" << endl;
cout << "2. Greatest Common Denominator Program" << endl;
cout << "3. Exit" << endl;
}
GCD.cpp
#include <iostream>
using namespace std;
int euclid (int*, int*);
int main() {
int a, b;
cout << "A program to find GCD of two given numbers.";
cout << "\n\nEnter your choice of a number: ";
cin >> a;
cout << "\nEnter your choice of another number: ";
cin >> b;
cout << "\n\nProcessing with Euclid method";
cout << "\nThe GCD of " << a << " and " << b << " is " << euclid(a, b);
return 0;
}
int euclid ( int *x, int *y) {
if ( x % y == 0 )
reutrn y;
else return euclid ( y, x%y );
}
palindrome.cpp
#include<iostream>
using namespace std;
int main(){
char string1[20];
int i, length;
int flag = 0;
cout << "Enter a string: ";
cin >> string1;
length = strlen(string1);
for(i=0;i < length ;i++){
if(string1[i] != string1[length-i-1]){
flag = 1;
break;
}
}
if (flag) {
cout << string1 << " is not a palindrome" << endl;
}
else {
cout << string1 << " is a palindrome" << endl;
}
system("pause");
return 0;
}
GCD.h
#ifndef PROJ4_GCD_H
#define PROJ4_GCD_H
int euclid (int, int);
#endif //PROJ4_GCD_H
palindrome.h
#ifndef PROJ4_PALINDROME_H
#define PROJ4_PALINDROME_H
char string1[20];
int i, length;
int flag = 0;
#endif //PROJ4_PALINDROME_H
I appreciate all input and help. Thanks,
In the header you define
int euclid (int, int);
but in the .cpp file you implement
int euclid (int*, int*);
so when you invoke it as gcd(a, b) the linker won't find the integer version. Get rid of the pointers.
PS: In the switch you are invoking main(), you need instead to call palindrome():
switch (choice)
{
case 1:
cout << "Palindrome Program.";
// main();
palindrome();

'Vehicle' is not valid template type argument for parameter '_Ty'

VS keeps giving me the error mentioned in the title for every function for a class project (at least, all of the ones in Vehicle.h), and I can't figure it out no matter how hard I try. It doesn't seem to be due to any circular definitions, but maybe the answer is simple. The project is supposed to be based on four files (two header files and two .cpp's); I'll attach them below.
Vehicle.h:
#include <iostream>
#include <fstream>
#include <string>
#include<vector>
using namespace std;
//Declaring Dealer class
class Dealer
{
public:
Dealer();
int getDealerNum();
void setDealerNum(int dealerNumber);
private:
int dealerNumber;
Dealer *dealer;
};
//Declaring Vehicle class
class Vehicle
{
public:
Dealer *dealerType;
Vehicle(string VIN, string make, string model, int year, double price);
Vehicle();
string getVIN();
string getMake();
string getModel();
int getYear();
double getPrice();
void setVIN(string VIN);
void setMake(string make);
void setModel(string model);
void setYear(int year);
void setPrice(double price);
friend Vehicle;
private:
string VIN;
string make;
string model;
int year;
double price;
};
Functions.h (only one function included, to save some space):
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include<string>
#include<vector>
using namespace std;
//Declaring name for input and output file
ofstream outfile;
ifstream infile;
void displayInventory(vector<Vehicle>& vehicles)
{
int i = 0;
char query = 'a';
int j = vehicles.size();
//Simple loop to display the inventory, with a pause function to wait for user exit
while (query != 'x'&&query!='X')
{
for (i; i < j; i++)
{
cout << "Vehicle #" << i + 1 << endl;
cout << "Vin: " << vehicles[i].getVIN << endl;
cout << "Make: " << vehicles[i].getMake << endl;
cout << "Model: " << vehicles[i].getModel << endl;
cout << "Year: " << vehicles[i].getYear << endl;
cout << "Price: $" << vehicles[i].getPrice << endl << endl;
}
cout << endl << "Enter 'x' to return to main menu" << endl;
cin >> query;
cout << endl;
}
}
Main.cpp:
#include <iostream>
#include <fstream>
#include <string>
#include<vector>
#include "Thomas-PA3 Functions.h"
#include "Thomas-PA3 Vehicle.h"
using namespace std;
void displayInventory(vector<Vehicle>& vehicles);
void addInventory(vector<Vehicle>& vehicles);
void deleteInventory(vector<Vehicle>& vehicles);
void updateInventory(vector<Vehicle>& vehicles);
void sortInventory(vector<Vehicle>& vehicles);
void searchInventory(vector<Vehicle>& vehicles);
void writeInventory(vector<Vehicle>& vehicles);
void refreshInventory(vector<Vehicle>& vehicles);
int main()
{
char menu='0';
vector<Dealer>dealers;
vector<Vehicle>vehicles;
while(menu!='8')
{
cout << "Welcome to the vehicle management menu." << endl;
cout << "Please select an option from the following list:" << endl;
cout << "1: Display vehicles" << endl;
cout << "2: Add a vehicle" << endl;
cout << "3: Update a vehicle" << endl;
cout << "4: Delete a vehicle" << endl;
cout << "5: Sort inventory by VIN" << endl;
cout << "6: Search inventory by Make" << endl;
cout << "7: Read inventory from file (will overwrite any changes)" << endl;
cout << "8: Write inventory to file and exit" << endl;
cin >> menu;
switch(menu)
{
// Call to appropriate functions based upon user decision
case '1': displayInventory(vehicles);
break;
case '2': addInventory(vehicles);
break;
case '3': updateInventory(vehicles);
sortInventory(vehicles);
break;
case '4': deleteInventory(vehicles);
break;
case '5': sortInventory(vehicles);
break;
case '6': searchInventory(vehicles);
break;
case '7': refreshInventory(vehicles);
break;
case '8': sortInventory(vehicles);
writeInventory(vehicles);
break;
//Error/incorrect input checking
default: cout << endl << "Please make a valid selection" << endl << endl;
}
}
return 0;
}
Vehicle.cpp:
#include <iostream>
#include <fstream>
#include <string>
#include<vector>
#include "Thomas-PA3 Vehicle.h"
#include "Thomas-PA3 Functions.h"
using namespace std;
//Default constructor for Dealer
Dealer::Dealer()
{
dealerNumber = 0;
}
//Function to get dealer number
int Dealer::getDealerNum()
{
return dealerNumber;
}
void Dealer::setDealerNum(int dealerNumber)
{
cout << "Please input the new dealer number" << endl;
cin >> dealerNumber;
}
//Custom constructor for Vehicle
Vehicle::Vehicle(string VIN, string make, string model, int year, double price)
{
cout << "Vehicle data is initialized" << endl;
}
//Default constructor for Vehicle
Vehicle::Vehicle()
{
VIN = "0";
make = "";
model = "";
year = 0;
price = 0;
}
//Functions to return member variables
string Vehicle::getVIN()
{
return VIN;
}
string Vehicle::getMake()
{
return make;
}
string Vehicle::getModel()
{
return model;
}
int Vehicle::getYear()
{
return year;
}
double Vehicle::getPrice()
{
return price;
}
//Functions to set member variables
void Vehicle::setVIN(string VIN)
{
cout << "Please input the vehicle's VIN #" << endl;
cin >> VIN;
}
void Vehicle::setMake(string make)
{
cout << "Please input the vehicle's make" << endl;
cin >> VIN;
}
void Vehicle::setModel(string model)
{
cout << "Please input the vehicle's model" << endl;
cin >> VIN;
}
void Vehicle::setYear(int year)
{
cout << "Please input the vehicle's year" << endl;
cin >> VIN;
}
void Vehicle::setPrice(double price)
{
cout << "Please input the vehicle's price" << endl;
cin >> VIN;
}
You include "Functions.h" before "Vehicles.h". Therefore, when the compiler sees vector<Vehicle>, it doesn't yet know that Vehicle will be defined as a class later.
C++ is compiled in three stages. First, the preprocessor runs and executes #include statements and the like. This is done once per .cpp file, and the result is fed to the real compiler. The compiler then compiles this result line by line. Finally, the linker glues everything together.
The important stage here is the middle one. Because each .cpp file is compiled in isolation, top to bottom, you put the necessary headers at the top. And if one header needs another, you put one above the other. To make this easy, it can be useful to #include one header in the other. After all, all the #include statements are executed. Everything ends up in the .cpp file eventually.
One final remark: You generally need to protect yourself against double #includes. There's a second preprocessor mechanism for that: surround your .h file with
#ifndef VEHICLE_H
#define VEHICLE_H
// Real contents of Vehicle.h go here, including any other #include statement
#endif
If you now write
#include "vehicle.h"
//.. other stuff
#include "vehicle.h"
the preprocessor will see the second statement, and note that VEHICLE_H was already defined, so the second inclusion is not needed. Spelling note: it's all uppercase to prevent confusion with class Vehicle and other similar names, and you can't use a . there so the suffix is _H.

Object is initializing to unwanted value

I have been working on a trivial assignment to get used to coding. I am designing an ATM machine and at the moment it is composed of 2 classes:
BankAccount.cpp
Constructor for different types of account
Only has balance as a member
Transaction.cpp
Performs a method on the BankAccount (i.e make deposit, make withdrawl & get balance)
Problem: BankAccount is automatically initialized to a balance of 10 which is undesired. So for example, if I made a checking account and chose to deposit $10, balance would print out $20.
//BankAccount.h
//This class will simply take in a bank account
//with a balance, other classes will use a bank account object
//such as saving/checkings and perform operations on the
//balance
#ifndef BANK_ACCOUNT_H
#define BANK_ACCOUNT_H
class BankAccount {
private:
float balance;
public:
BankAccount ();
float getBalance ();
void makeDeposit ();
void makeWithdrawl ();
};
#endif
//BankAccount.cpp
#include "BankAccount.h"
#include <iostream> //remove once done *not to self
using namespace std; //remove once done *note to self
BankAccount::BankAccount() {
balance = 0.00;
}
float BankAccount::getBalance() {
return balance;
}
void BankAccount::makeDeposit() {
cout << "How much would you like to deposit: ";
float deposit_value;
cin >> deposit_value;
balance += deposit_value;
}
void BankAccount::makeWithdrawl() {
cout << "How much would you like to withdrawl: ";
float withdrawl_value;
cin >> withdrawl_value;
balance -= withdrawl_value;
}
//Transaction.h
#ifndef TRANSACTION_H
#define TRANSACTION_H
class Transaction {
private:
BankAccount m_bao;
public:
Transaction(BankAccount&);
void displayOptions();
void printReciept();
};
#endif
//Transaction.cpp
#include "BankAccount.h"
#include "Transaction.h"
#include <iostream>
using namespace std;
Transaction::Transaction(BankAccount& bao) {
m_bao = bao;
}
void Transaction::displayOptions() {
cout << "\nPlease make a choice\n\n";
cout << "1: Make Deposit\n";
cout << "2: Make Withdrawl\n";
cout << "3: Check Balance\n";
int choice;
cin >> choice;
switch (choice) {
case 1:
m_bao.makeDeposit();
break;
case 2:
m_bao.makeWithdrawl();
break;
case 3:
m_bao.getBalance();
break;
}
}
void Transaction::printReciept() {
cout << "Current balance is now: " << m_bao.getBalance() + '\n';
}
int main () {
BankAccount checking;
Transaction q(checking);
q.displayOptions();
q.printReciept();
}
I am sure the answer is right in front of my eyes, but my brain is just fried right now. I will continue to look for the solutions and let you guys know if my problem has been solved yet.
[EDIT]
Alright, now I am trying to make it so that the customer can choose to perform transactions on either Checking or Savings account. Currently I got it looking like this in my main():
int main () {
BankAccount checking(0.00);
BankAccount savings(0.00);
Transaction c(checking);
Transaction s(savings);
for(int i = 0; i < 10 ; i++) {
cout << "Make an option" << endl;
cout << "1. Checking " << endl;
cout << "2. Savings" << endl;
int choice;
cin >> choice;
if (choice == 1) {
c.prompt();
c.printReciept();
}
else {
s.prompt();
s.printReciept();
}
}
}
It works fine, but I would like to make this process more OOP-alized, if that makes sense :)
One option I was trying to look into was making a prompt function which would belong to Transaction.cpp. This would do everything that is done in main, except initializing the objects of course.
Your problem is this line:
cout << "Current balance is now: " << m_bao.getBalance() + '\n';
Which the compiler sees as:
cout << "Current balance is now: " << (m_bao.getBalance() + '\n');
'\n' is 10 as an int, so you get this:
cout << "Current balance is now: " << (m_bao.getBalance() + 10);
You probably meant to do this:
cout << "Current balance is now: " << m_bao.getBalance() << '\n';
Remember that in C++, + almost always means "add these two numbers".