Use of undeclared identifiers even though class is created in c++ - c++

I have created a class ShowTicket coded as follows in a header file:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class ShowTicket {
public:
//function that returns true if sold status is true and false if it doesnt.
bool is_sold(void){
if (sold_status == true){
return true;
}
else{
return false;
}
}
//function that sets sold_status to true
void sell_seat(void){
sold_status = true;
}
//prints row, seat number and sold status in casual terms
string print_ticket(void){
ostringstream sout;
if(sold_status == true){
sout<<row<<" "<<seat_number<<"sold";
}
else{
sout<<row<<" "<<seat_number<<"available";
}
return sout.str();
}
//initilizes variables in constructor
bool sold_status;
const char* row;
const char* seat_number;
//constructor
ShowTicket(const char* row, const char* seat_number, bool sold_status){
sold_status = false;
}
};
I am using a main file with the following code to test this class
#include <iostream>
#include <string>
#include <sstream>
#include "showticket.h"
using namespace std;
int main () {
ShowTicket myticket1(“AA”,”101");
ShowTicket myticket2(“AA”,”102”);
if(!myticket1.is_sold())
myticket1.sell_seat ();
cout << myticket1.print_ticket() << endl;
cout << myticket2.print_ticket() << endl;
return 0;
}
I am receiving multiple "Use of undeclared identifier" errors and "Non-ASCII characters are not allowed outside of literals" errors and I do not know how to fix them.
Any help is greatly appreciated.

Related

Problem printing CHAR array in C ++ language

I try to print an array and I can not. Does anyone have any idea why ?
In the Main.cpp file there is of course a function call.
My code:
Game.cpp:
#include "Game.h"
Game::Game() {
char example[] = "PASS";
}
bool Game::PrintArray() {
cout << example[0] << endl;
return true;
}
Game.h:
#include <iostream>
#include <array>
#include <iostream>
#include <string>
using namespace std;
#ifndef GAME_H_
#define GAME_H_
class Game {
private:
char example[];
public:
Game();
bool PrintArray();
};
#endif /* GAME_H_ */
Your code has three problems:
1) Array example in Game.h is a static array of zero length (so you can't add characters there)
Solution: make a const pointer to an array of characters
2) Inside the Game's constructor you create a NEW variable example, do not affect the variable in the Game.h -> your variable in the class just don't updated
solution: example = "PASS";
3) In func Game::PrintArray you are printing only first character
Solution: cout << example << endl;
Game.h:
class Game {
private:
const char* example;
public:
Game();
bool PrintArray();
};
Game.cpp:
Game::Game() {
example = "PASS";
}
bool Game::PrintArray() {
cout << example << endl;
return true;
}
But even more correct solution is to use std::string. Then you don't have to worry about allocated/unallocated memory:
Game.h:
class Game {
private:
std::string example;
public:
Game();
bool PrintArray();
};
Game.cpp:
Game::Game() {
example = "PASS";
}
bool Game::PrintArray() {
cout << example << endl;
return true;
}

If else statement on bool in C++

Basically, I have 3 functions
The first and second functions is to check whether ignition is true or false.
The third function basically is to check if ignition is on, the speed cannot be greater than 65, if the speed is greater than 65, it will "Fix" that speed at 65.
Whereas, if the ignition is turn off, the speed will be 0.
However,
in my code, I did a if else statement.
When I print the part where ignition is turn off,
the value I got is 65. It suppose to be 0.
May I know what's wrong with my code?
car.h
#ifndef car_inc_h
#define car_inc_h
#include <iostream>
#include <string>
using namespace std;
class Car {
bool isIgnitionOn;
int speed;
public:
void turnIgnitionOn();
void turnIgnitionOff();
void setSpeed(int);
void showCar();
};
#endif
car.cpp
#include <iostream>
#include <string>
#include "Car.h"
using namespace std;
void Car::turnIgnitionOn() {
this->isIgnitionOn = true;
}
void Car::turnIgnitionOff() {
this->isIgnitionOn = false;
};
void Car::setSpeed(int speed) {
if (isIgnitionOn == true) {
if (speed >= 65) {
this->speed = 65;
}
else {
this->speed = speed;
}
}
else if (isIgnitionOn == false){
this->speed = 0;
}
};
void Car::showCar() {
if (isIgnitionOn == true) {
cout << "Ignition is on." << endl;
cout << "Speed is " << speed << endl;
}
else if (isIgnitionOn == false) {
cout << "Ignition is off" << endl;
cout << "Speed is " << speed << endl;
}
};
main.cpp
#include <iostream>
#include <string>
#include "Car.h"
using namespace std;
int main() {
Car myCar;
myCar.turnIgnitionOn();
myCar.setSpeed(35);
myCar.showCar();
myCar.setSpeed(70);
myCar.showCar();
myCar.turnIgnitionOff();
myCar.showCar();
return 0;
}
speed is never reset to 0. You can add this->speed=0 in turnIgnitionOff which is more logical afterall.

C++ Error: No Member in Class?

I'm working on C++, and this is just a very basic program, but I'm still getting an error.
The error message is:
'class secondary' has no member named 'getting'.
Why is this? It works for my void setting, but not for getting? What am I doing wrong here?
main.cpp
#include <iostream>
#include <string>
#include "secondary.h"
using namespace std;
int main(){
secondary s;
int scale;
cout << "On a scale of 1-10, how awesome are you?" << endl;
cin >> scale;
cout << endl;
s.setting(scale);
cout << s.getting();
return 0;
}
secondary.h
#ifndef SECONDARY_H
#define SECONDARY_H
#include <string>
class secondary
{
public:
void setting(int x);
string getting();
};
#endif // SECONDARY_H
secondary.cpp
#include "secondary.h"
#include <iostream>
#include <string>
using namespace std;
void secondary::setting(int x){
factor = x;
}
string secondary::getting(){
string result;
if(factor < 3){
result = "You have a very low self esteem.";
}elseif(factor > 3){
if(factor > 7){
result = "You have a very high self esteem."
}else{
result = "You have a medium self esteem."
}
}
return result;
}
private factor;
Actually, looking at this again, and deeper, this code has many issues (semicolons missing at key points and the private int definition should have been in the header file, not the cpp file 9t(private is its own section, see below):The problem, from what I can see, s has not yet been instantiated yet, do so and the operation should work correctly.
Please also note that when factor was defined in the cpp file, it was defined at bottom, it should actually be defined before any use of the variable to be defined (in the header file is better meet with common/conventional coding standards).
Please check this tested code:
main.cpp
#include <iostream>
#include <string>
#include "secondary.h"
using namespace std;
int main(){
secondary s;
int scale;
cout << "On a scale of 1-10, how awesome are you?" << endl;
cin >> scale;
cout << endl;
s.setting(scale);
cout << s.getting();
return 0;
}
secondary.h
#ifndef SECONDARY_H
#define SECONDARY_H
#include <string>
class secondary
{
public:
void setting(int x);
std::string getting();
private: // Note: this is how you do private
int factor; // This is the definition with type int, missing in original
};
#endif // SECONDARY_H
secondary.cpp
#include "secondary.h"
#include <iostream>
#include <string>
using namespace std;
void secondary::setting(int x){
factor = x;
}
string secondary::getting(){
string result;
if (factor < 3){
result = "You have a very low self esteem.";
}else if(factor > 3){
if (factor > 7){
result = "You have a very high self esteem.";
}
else{
result = "You have a medium self esteem.";
}
}
return result;
}

Sorting a private element of a class [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to sort an integer that is declared in the private
I am having trouble on how to sort the int id element in the class,
here is where i would put my functions for the class
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#include "student.h"
#define cap 100
//Student constructor- empty
student::student()
{
id = 0;
first = "null";
last = "null";
age = 0;
gpa = 0.0;
}
//Student deconstructor
student::~student()
{
}
bool student::get(istream &in)
{
in >> id >> first >> last >> age >> gpa;
return(in.good());
}
void student::put(ostream &out)
{
out << id << first << last << age << gpa;
}
bool student::operator>(student)
{
if(student[i].id>student[i+1].id)
return true;
else
return false;
cout << student[i].id;
}
bool student::operator<(student)
{
if(student[i].id<student[i+1].id)
return true;
else
return false;
}
void student::sort_array()
{
int j,temp;
for(j=0;j<cap-1;j++)
{
//if out of position switch the out of align number
if(student[i].id<student[i+1].id)
{
temp = student[i].id;
student[i].id = student[i+1].id;
student[i+1].id = temp;
}
}
}
This is my file, and my main code that would display the and call functions
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
#include "student.h"
#define cap 100
void main()
{ string s;
class student student[cap],l;
int i;
fstream f;
i=0;
cout << "Enter the file name: "; //Display to enter the file name
cin >>s;
f.open(s.data(),ios::in);
if(!f.is_open())
cout << "could not open file";
while(i<cap && !f.eof())
{
student[i].get(f);
//Display if okay
if(f.good())
{
student[i].sort_array();
i++;
cout << i;
}
}
f.close();
}
this is my class code, and class file
#include <iostream>
using namespace std;
class student
{
public:
student(); //Constructor without parameters
student(int,string,string,int,float); //Constructor with parameters
~student(); //Deconstructors
bool get(istream &); //Input
void put(ostream &); //Output
bool operator==(student);
bool operator>(student);
bool operator<(student);
bool operator==(int);
bool operator>(int);
bool operator<(int);
int read_array(string,int);
void sort_array();
private:
int id,age;
float gpa;
string last,first;
};
The common technique is to provide comparison operators, <, >, <=, >=, == and !=, for a class that can be compared. Most sort algorithms require that items in the container can be "less than comparable".
Look up the Boost library as it has tools that will define all comparison operators when only less-than and equality are defined.
In your design, you will need a container for your students. Use std::sort or your preferred sorting function on the container. If this is homework, you may want to put the container in main() along with the sorting logic.
Also, either search the web for "C++ overloading operators" or define your own method. If you are clever, you can design your class to have different comparison functions to allow sorting by the different fields.

Error for pointer to member in C++

Header file
#include <iostream>
#include <cstring>
const unsigned MaxLength = 11;
class Phone {
public:
Phone(const char *phone) {
setPhone(phone);
}
void setPhone(const char Phone[ ]);
const char* getPhone();
private:
char phone[MaxLength+1];
};
Cpp file
#include "Phone.h"
#include <iostream>
#include <ctype.h>
#include <cstring>
using namespace std;
bool checkNum(char num[]);
void Phone::setPhone(const char Phone[ ]) {
strncpy(phone, Phone, MaxLength);
phone[MaxLength] = '\0';
}
const char* Phone::getPhone() {
return phone;
}
int main() {
Phone i1("12345678901");
cout << i1.getPhone() << endl;
if (checkNum(i1.getPhone))
cout << "Correct" << endl;
else
cout << "Invalid Wrong" << endl;
}
bool checkNum(char num[]) {
bool flag = true;
if (isdigit(num[0]) == 0)
flag = false;
return flag;
}
When I tried to compile, I get this error:
error C3867: 'Phone::getPhone':
function call missing argument list;
use '&Phone::getPhone' to create a
pointer to member
I'm getting an error on this line "if (checkNum(i1.getPhone))". I created a Phone object and what I am trying to do is use the function checkNum to see if the first index of the array is a number. Am I referencing the object wrong? Should I use indirect selection operator instead? Any idea what I am doing wrong?
Thanks
You are missing a pair of parentheses after getPhone in if (checkNum(i1.getPhone)); it should be if (checkNum(i1.getPhone())).
The line:
if (checkNum(i1.getPhone))
should be
if (checkNum(i1.getPhone()))