I have been working with this for a while and researching what I need when I need it. I have been using the website our professor provided us with to compile and execute the program. http://www.compileonline.com/compile_cpp11_online.php. The website has been working ok so far. However, now when I compile and execute the program, it executes but I get no feedback or errors so I'm not sure what is wrong or how to fix it.
I would test it with a different compiling program but I'm having a hard time getting any kind of c++ compiler installed.
I was wondering if anyone would be able to take a look and see what is going on. Let me know if the code is unclear.
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <iterator>
usingnamespace std;
class Person
{
string first;
string last;
int age;
public:
Person(string fn, string ln, int a);
string getFname() { return first; }
string getLname() { return last; }
int getAge() {return age; }
booloperator<(Person p2);
};
Person::Person(string fn, string ln, int a)
{
first = fn;
last = ln;
age = a;
}
/**************************************
* This part is to decide if a person is less than another
* Person. Then return a boolean value.
*
* ***********************************/
bool Person::operator<(Person p2){
if(age < p2.getAge()){
string first1 = first;
string first2 = p2.getFname();
string last1= last;
string last2= p2.getLname();
int i = 0;
while(i < first1.length() && i < first2.length()){
if(first1[i] < first2[i]){
returntrue;
i++;
}
}
i = 0;
while(i < last1.length() && i < last2.length()){
if(last1[i] < last2[i]){
returntrue;
i++;
}
}
}
elsereturnfalse;
}
/********************************
* The program itself reads from an input.txt file and is to sort
* the entries first by age from youngest to oldest and then alphabetically
* by first name then last name. The program is then to print out the vector
* containing the sorted Person entries.
*
* *****************************/
int main()
{
string fname;
string lname;
string tempAge;
int age;
ifstream myFile ("input.txt");
vector<Person> myVector;
vector<Person>::iterator myIt = myVector.begin();
cout << "one";
while(getline(myFile, fname)){
getline(myFile, lname);
getline(myFile, tempAge);
age = atoi(tempAge.c_str());
fname.erase(fname.length()-1); //Using an online compiler so getline grabs the \r character too
lname.erase(lname.length()-1);
Person p1 = Person(fname,lname,age); //Use this as a placeholder for the current Person being created.
if(myVector.size() == 0){ //To add the first element to the vector.
myVector.insert(myIt,p1);
}
else{ //Loop used to add the rest of the list to the vector.
for(int i = 0; i < myVector.size(); i++){
if(p1 < myVector[i]){
myVector.insert(myIt + i, p1);
myIt = myVector.begin();
}
}
}
//Supposed to go through the vector and print out each entry's information.
for(int i = 0; i < myVector.size(); i++)
cout << myVector[i].getFname() << " " << myVector[i].getLname() << ": " << myVector[i].getAge() << '\n';
}
myFile.close();
return 0;
}
Sample text file:
John
Doe
24
Mary
Waters
32
Jill
Uphill
28
Jack
Felldown
16
I threw in a cout << "one" to try and test it. I meant to take those out. It doesn't even print "one" out for me. The goal is to print out the whole vector of Persons though.
Note: If you want a compiler and IDE go to http://www.codeblocks.org/downloads/26 and download the one which is written "codeblocks-13.12mingw-setup-TDM-GCC-481.exe". It is a nice IDE and includes a compiler.
Yeah. It must be the online compiler just not giving me any feedback. (Which I thought was the problem.) I know the output isn't correct right now but I need to get a IDE/compiler on my computer and I am having hell. I'm not good with installation paths and all the IDE's I download are super complicated to set up to me.
||=== Build: Debug in Person (compiler: GNU GCC Compiler) ===|
||Warning: .drectve `-aligncomm:"___hexdig_D2A",5' unrecognized|
:crt1.c||undefined reference to `__chkstk_ms'|
:glob.c||undefined reference to `__chkstk_ms'|
glob.c||undefined reference to `__chkstk_ms'|
glob.c||undefined reference to `__chkstk_ms'|
glob.c||undefined reference to `__chkstk_ms'|
glob.c||more undefined references to `__chkstk_ms' follow|
||=== Build failed: 6 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
I'm totally new to all this IDE/compiler installation process. I began my coding career* using Java programming. It's a cinch to setup compared to this. I have to do this one in C++ though.
That error sounds very familiar...though I can't remember when I last saw it or what the problem actually was (or how I fixed it).
I would guess either an incompatible library (e.g. mixing libraries from different compilers or different versions of MinGW) or a messed-up installation.
Did you have MinGW previously installed on your system?
I did at home from troubleshooting. Currently at work where I received the error above. I am using netbeans now and seems to be going fine, but when I run the program with line 94:
age = atoi(tempAge.c_str());
IT NEVER FINISHES THE RUN. I let it run for 30 mins! I take it out with comments and let it establish it's own age and it runs... somewhat ok. Still having trouble with the vectors if anyone wants to try and solve those.
EDIT: Performing an atoi conversion OUTSIDE of the while loop seems to work but I really need it converting every age that comes in.
EDIT2: Found that the while loop is only iterating once. I have no idea why.