Jul 15, 2014 at 11:55pm UTC
I am still very new at c++ and coding in general. I am working on an assignment for my class and I think I am running into the "phantom newline" problem. I have already inserted cin.ignore() but the identical(to my knowledge!) char arrays are not qualifying as equal to when I put in the same name. Maybe I have something wrong with my == operator, but I am stumped at this point, and any help will be greatly appreciated. I am only able to use char arrays, not strings.
Relevant code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
#include "stdafx.h"
#include <iostream>
#include <istream>
using namespace std;
class Student
{
private :
long ssn;
char name[80];
public :
Student();
Student(char name[80], long ssn);
Student(Student&);
void setSSN(long );
long getSSN();
void setName(char *);
const char * getName();
bool operator ==(Student&);
Student operator =(Student&);
};
Student::Student()
{
//Default Constructor, no arguments
strcpy_s(name, sizeof (name), "unassigned" );
ssn = 999999999;
}
long Student::getSSN()
{
//Retrieves the Student's SSN
return ssn;
}
const char * Student::getName()
{
//Retrieves the Student's name
return name;
}
void Student::setSSN(long newSSN)
{
//Applies the new SSN to the student
if (newSSN > 0)
ssn = newSSN;
}
void Student::setName(char * newName)
{
//Applies the new Name to the student
if (strlen(newName) > 0)
strcpy_s(name, sizeof (name), newName);
}
bool Student::operator ==(Student& cStu)
{
//Equality operator
if (ssn == cStu.getSSN() && name == cStu.getName())
return true ;
else
return false ;
}
int main()
{
//Creates the 2 students and tests operators
char n[80];
long s;
Student stu1;
Student stu2;
cout << "\nEnter student 1 name: " ;
cin.getline(n, 80); stu1.setName(n);
cout << "Enter student 1 ssn: " ;
cin >> s; cin.ignore(); stu1.setSSN(s);
cout << "\nEnter student 2 name: " ;
cin.getline(n, 80); stu2.setName(n);
cout << "Enter student 2 ssn: " ;
cin >> s; stu2.setSSN(s);
if (stu1 == stu2)
cout << "Those are the same students" << endl << endl << endl;
else
cout << "Those are not the same students" << endl << endl << endl;
return 0;
}
Last edited on Jul 16, 2014 at 12:00am UTC
Jul 16, 2014 at 12:45am UTC
My new line 59 is
if ((ssn == cStu.getSSN()) && (strcmp(name, cStu.getName())=0))
But I am now getting the 'modifiable lvalue' for this line. Am I using strcmp correctly?
Last edited on Jul 16, 2014 at 12:53am UTC
Jul 16, 2014 at 1:53am UTC
strcmp(name, cStu.getName())=0 <- did you mean that to be == ?
Jul 16, 2014 at 1:57am UTC
Ah that was it! Can't believe I overlooked that. Thank you both very much.