I recently started learning c++ in school in the visual studio environment. I am not going to be working on a program on the side at home where I have a mac so I want to start using xcode. I was able to locate a c++ program on someones blog that gives me a good template to start modifying. I first tested the program as in visual studio and as expected it runs just fine. I have no started trying to get it to run in xcode but it is giving me some throwback errors. Specifically I get the following error:
1 2
libc++abi.dylib: terminating with uncaught exception of type std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >
(lldb)
and it directs me to line 91 of the .cpp file here:
#include "User.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
usingnamespace std;
User::User(){
}
string User::getFn() const{
//POST: return first name
return fn;
}
string User::getLn() const{
//POST: return last name
return ln;
}
string User::getID() const{
//POST: return ID
return id;
}
string User::getUserid() const{
//POST: return userid
return userid;
}
string User::getPassword() const{
//POST: return password
return password;
}
void User::setFn(string input){
//PRE: takes input of firstname
//POST: sets the first name after checking errors
string errorMsg;
for (int k = 0; k < input.length(); k++)
if (input[k]==' ')
{ errorMsg = "Name cannot contain a blank.";
throw errorMsg;
}
for (int k = 0; k < input.length(); k++)
if (input[k]>='0' && input[k]<='9')
{ errorMsg = "Name cannot contain a digit.";
throw errorMsg;
}
fn = input;
}
void User::setLn(string input){
//PRE: takes input of lastname
//POST: sets the last name after checking errors
string errorMsg;
for (int k = 0; k < input.length(); k++)
if (input[k]==' ')
{ errorMsg = "Name cannot contain a blank.";
throw errorMsg;
}
for (int k = 0; k < input.length(); k++)
if (input[k]>='0' && input[k]<='9')
{ errorMsg = "Name cannot contain a digit.";
throw errorMsg;
}
ln = input;
}
void User::setID(string ID){
//PRE: takes input of ID number
//POST: sets the id number
id = ID;
}
void User::setUserid(string ln, string id){
//PRE: takes last name and Id
//POST: returns the first 6letters of last name and last 2 of id number
userid = ln.substr(0, 6) + id.substr(id.size() - 2);
}
void User::setPassword(string input){
//PRE: takes input of password
//POST: sets the password for that users after error checking
string errorMsg;
if((input.length() < 6 || input.length() > 10 )){
errorMsg = "Password must be between 6 and 10 charaters";
throw errorMsg;
}
for (int k = 0; k < input.length(); k++){
if (std::string::npos == input.find_first_of("0123456789"))
{
errorMsg = "Password must contain a digit";
throw errorMsg;
}elseif (std::string::npos == input.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
{
errorMsg = "\nPassword must contain a Uppercase letter";
throw errorMsg;
}
}
password = input;
}
void User::changePassword(string oldpass, string newpass){
//PRE: takes in old password and the new password
//POST: sets the New password for user
User u;
if(u.password == oldpass){
cout << "\nPlease enter new password";
cin >> newpass;
u.setPassword(newpass);
}
}
At first glance it appears to me that the program doesn't like the password listed in the .txt file, like it is too short or too long. I've dismissed that though because it is exactly the same text file I was using on Windows. My next guess is maybe there are some libraries that xcode doesn't include by default but Visual studio does. If that is the case I don't know which ones or how to even find that out.
The website with all of the c++ code is located here:
I've been trying to do some debugging of my own and it seems like the problem might actually be related to the password file. I added a cout for the password length like this around the global lines of 89 - 94:
1 2 3 4 5 6
if((input.length() < 6 || input.length() > 10 )){
cout << input.length() << endl;
getchar();
errorMsg = "Password must be between 6 and 10 charaters";
throw errorMsg;
}
And it spits back that the length is 0. Odd, so I am wondering now if somehow xcode isn't opening the file properly? I have the text file containing the passwords in the same directory as the .cpp and .h files.
The only place I see setPassword being called is on line 119 inside of changePassword. However, the argument you pass here is one that you're getting from standard input (generally, the user at the keyboard) on line 118. An external file doesn't have anything to do with it.
Right. So when the program starts up there is a text file of some generic user names, user ids, and passwords that are supposed to be read. Then a console window will appear where there is a prompt to enter the user id. Once that is input then the password must be input. Then a screen comes up where the user can change first name, last name, password, or quit. The problem is this code does just that when run in Visual Studio but when run in xcode it breaks and I cannot even get the login prompt. Hopefully that clarifies.