C++: No output on eclipse console - c++

Hello and thanks in advance for helping,
I've the problem that I don't see any output on my eclipse console (on linux ubuntu 12.04).
I have this little C++ program:
Addressverwaltung.cpp:
#include <iostream>
#include "Adresse.h"
using namespace std;
int main() {
cout << "asdf";
Adresse lAdresse1("Max", "Tester", "Strasse 21", 6423, "lol", "asdf#hotmail.com");
lAdresse1.printAdresse();
lAdresse1.setName("Testing");
lAdresse1.printAdresse();
return 0;
}
Adresse.h:
#ifndef Adresse_h
#define Adresse_h
#include <iostream>
#include <string>
class Adresse{
public:
Adresse(std::string pVorname, std::string pName);
Adresse(std::string pVorname, std::string pName, std::string pStrasse, int pPlz, std::string pOrt, std::string pEmail);
void printAdresse();
void setVorname(std::string pVorname);
void setName(std::string pName);
std::string getName();
private:
std::string mVorname;
std::string mName;
std::string mStrasse;
int mPlz;
std::string mOrt;
std::string mEmail;
};
#endif
Adresse.cpp:
#include "Adresse.h"
Adresse::Adresse(std::string pVorname, std::string pName){
mVorname = pVorname;
mName = pName;
}
Adresse::Adresse(std::string pVorname, std::string pName, std::string pStrasse, int pPlz, std::string pOrt, std::string pEmail){
mVorname = pVorname;
mName = pName;
mStrasse = pStrasse;
mPlz = pPlz;
mOrt = pOrt;
mEmail = pEmail;
}
void Adresse::printAdresse(){
std::cout << "ADRESSE:";
std::cout << mVorname + mName;
std::cout << "STRASSE: " + mStrasse;
std::cout << "PLZ: " + mPlz;
std::cout << "EMAIL: " + mEmail;
}
void Adresse::setVorname(std::string pVorname){
mVorname = pVorname;
}
void Adresse::setName(std::string pName){
mName = pName;
}
std::string Adresse::getName(){
return mName;
}
Whenever I click "run" I see the message "make all
make: Nothing to be done for `all'." for about 4 seconds, afterwards the console is empty. I tried cleaning and rebuilding the project but that doesn't help.
Does anyone know how to fix this?

Put a cout.flush() before the return in main() function. That should help:
int main() {
cout << "asdf";
Adresse lAdresse1("Max", "Tester", "Strasse 21", 6423, "lol", "asdf#hotmail.com");
lAdresse1.printAdresse();
lAdresse1.setName("Testing");
lAdresse1.printAdresse();
cout.flush(); // <<<<<<<<<<<<<<<<<<<<<<<<
return 0;
}

Related

C++ Enum as a Constructor Argument

Hello I didn't use C++ for years now, so I'm kinda rusty and can't figure this out!
I have this code | person.h
#ifndef PERSON_H
#define PERSON_H
#include<string>
class Person {
private:
std::string fName, lName;
int age;
enum professionEnum {
Barber,
Developer,
Marketer
} profession;
public:
Person();
Person(std::string fName, std::string lName, int profession);
void setFName(std::string fName);
void print();
};
#endif
a simple person class.
What I want to do is for my constructor Person() to take an int, example :
Person me = Person("me", "meow", 0); // This should assign "me" to Barber, enum index is 0.
Here is what I tried so far (all errors):
this->profession = professionEnum::profession;
this->profession = 0;
this->profession = professionEnum(profession);
Here is the full person.cpp code
#include "person.h"
#include <iostream>
Person::Person() {
this->age = 18;
this->fName = "Default";
this->lName = "Default";
}
Person::Person(std::string fName, std::string lName, int profession) {
this->fName = fName;
this->lName = lName;
this->profession = professionEnum::profession;
this->profession = 0;
this->profession = professionEnum(profession);
}
void Person::print() {
std::cout << "Age : " << this->age << ", FName : " << this->fName << ", LName : " << this->lName << std::endl;
}
Thanks for your help
PS: If you spot any bad practices / bad code, please tell me.

Read class objects from file c++ (Private member variables)

I am learning how to work with objects but am stuck the closest answer is could find was Read class objects from file c++.
In this question:
How would you go about the same task if your member variables were declared as private?
Try something similar to this:
Content of person.txt
John California 3683893
Stalin Russia 489895
Sample code:
#include<iostream>
#include <fstream>
#include <vector>
#include <string>
class person
{
private:
std::string _name;
std::string _address;
unsigned int _phone;
public:
person(const std::string& name, const std::string& address, unsigned int phone)
:_name(name),_address(address),_phone(phone)
{}
std::string getName() const { return _name; }
std::string getAddress() const { return _address; }
unsigned int getPhone() { return _phone; }
};
int main()
{
std::vector<person> persons;
std::string name;
std::string address;
unsigned int phone;
std::ifstream fs;
fs.open("person.txt");
if (fs.is_open()) {
while (std::getline(fs, name, ' ') && std::getline(fs, address, ' ') &&
(fs >> phone))
{
persons.push_back(person(name,address,phone));
}
}
for (auto p : persons) {
std::cout << p.getName() << " " << p.getAddress() << " " << p.getPhone() << '\n';
}
return 0;
}

Object doesn't listen to method or is not created? (C++ OO)

Whenever I try to make an object and call a function on it, it doesn't seem to work.
I have no idea why, since I don't seem to have errors too.
I have searched around on here regarding constructors and the toString-method, but haven't find anything that worked.
I have tried to edit (distinct) the members in the constructor members,
Tried to rewrite the toString method.
Tried to make local object (with no pointer).
But it doesn't return me the things in the object that I created when calling the constructor.
Where does the problem situate in this problem?
Here is my code:
.h file:
#pragma once
#include "stdafx.h"
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
class Store{
private:
int id;
string name;
string adress;
string telephone;
string btwNumber;
public:
int getId();
void setId(int);
string getName();
void setName(string);
string getAdress();
void setAdress(string);
string getTelephone();
void setTelephone(string);
string getBtwNumber();
void setBtwNumber(std::string);
string toString();
Store(int, string, string , string, string);
};
.cpp file:
// Store.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Store.h"
Store::Store(int idnum, string nameS, string adreS, string telephonE, string btwnummeR){
idnum = id;
nameS = name;
adreS = adress;
telephonE = telephone;
btwnummeR = btwNumber;
}
int Store::getId()
{
return id;
}
void Store::setId(int id){
this->id = id;
}
string Store::getName(){
return naam;
}
void Store::setName(string name){
this->naam = naam;
}
string Store::getTelephone(){
return telephone;
}
void Store:setTelephone(string telephone){
this->telephone = telephone;
}
string Store::getBtwNumber()
{
return btwNumber;
}
void Store::setBtwNumber(string btwNumber){
btwNumber = btwNumber;
}
string Store::getAdress(){
return adress;
}
void Store::setAdress(string adress){
this->adress = adress;
}
string Store::toString(){
stringstream s;
s << "Id: " << id << endl;
s << "Naam: " << name << endl;
s << "Adres: " << adress << endl;
s << "Telefoonnummer: " << telephone << endl;
s << "BTWnummer: " << btwNumber << endl;
return s.str();
}
int _tmain(int argc, _TCHAR* argv[])
{
Store *test = new Store (4, "Test", "test", "test", "test");
test->toString();
system("Pause");
return 0;
}
Your constructor is inversed: you are assigning member variables to constructor arguments and not vice versa.
nameS = name;
Should be
name = nameS;
And so on
The method toString does work, but it won't magically decide to output its return value to screen. You'll have to do it yourself:
std::cout << test->toString() << std::endl;
You'll need to add #include <iostream> on top of your cpp file.

Issue with C++ using getline and vector

I have an issue where I am reading a line from a file using getline and the using a stringstream to separate the different variables using a comma as a delimiter. The issue is that a standard cout of the variables shows the seatDes properly, but using the vector I get the name back instead of the seatDes. Not sure why this is happening.
A standard line in file: Jane Doe,04202013,602,1A
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <cstdlib>
#include "reservation.h"
int main(int argc, char **argv)
{
std::ifstream flightFile;
std::string name, date, seatDes, flightNum, line;
int error = 0, conFlightNum;
flightFile.open("reservations.txt");
if(!flightFile)
{
//returns a error value if there is a problem reading the file
error = 1;
return error;
}
else
{
//Start reading files and sticks them into a class object and sticks the object into the vector set
while (std::getline(flightFile, line))
{
std::istringstream ss(line);
std::getline(ss, name, ',');
std::getline(ss, date, ',');
std::getline(ss, flightNum, ',');
conFlightNum = atoi(flightNum.c_str());
ss >> seatDes;
reservation newRes(name, date, conFlightNum, seatDes);
std::cout << name << std::endl << date << std::endl << conFlightNum << std::endl << seatDes << std::endl;
std::cout << "Vector Component" << std::endl;
std::cout //<< newRes.getName() << std::endl << newRes.getDate() << std::endl << newRes.getFlightNum()
<< std::endl << newRes.getSeatDesg() << std::endl;
}
}
flightFile.close();
return 0;
}
reservation.h file
class reservation {
private:
std::string name, seatDesg, date;
int flightNum;
public:
//Default Constructor
reservation(){}
//Big Constructor
reservation(std::string name, std::string date, int flightNum, std::string seatDesg)
{
this->name = name;
this->seatDesg = name;
this->date = date;
this->flightNum = flightNum;
}
//Setters
void setName(std::string name)
{ this->name = name; }
void setFlightNum(int flightNum)
{ this->flightNum = flightNum; }
void setSeatDesg(std::string seatDesg)
{ this->seatDesg = seatDesg; }
void setDate(std::string date)
{ this->date = date; }
//Getters
std::string getName()
{ return name; }
std::string getSeatDesg()
{ return seatDesg; }
std::string getDate()
{ return date; }
int getFlightNum()
{ return flightNum; }
};
reservation(std::string name, std::string date, int flightNum, std::string seatDesg)
{
this->name = name;
this->seatDesg = name; // Here is your problem
this->date = date;
this->flightNum = flightNum;
}
Should be
this->seatDesg = seatDesg;

Linker error undefined reference to class::class (Person::Person in my case)

I am getting a linker error undefined reference to Person::Person when trying to implement my program. The three parts are below. I have been working on fixing it for a few hours now. I know it's probably something simple that I am just not seeing. But I have looked around on the internet and still have not found my answer. So any help would be appreciated.
#ifndef PERSON0_H_
#define PERSON0_H_
#include <string>
class Person // class declaration
{
private:
static const int LIMIT = 25;
std::string lname;
char fname[LIMIT];
public:
Person() {lname = ""; fname[0] = '\0';}
Person(const std::string & ln, const char * fn = "Hay you");
void Show() const;
void FormalShow() const;
};
#endif
#include <iostream>
#include <string>
#include "person0.h"
void Person::Show() const
{
using namespace std;
std::cout << fname << " " << lname << '\n';
}
void Person::FormalShow() const
{
using std::cout;
std::cout << lname << ", " << fname << '\n';
}
#include <iostream>
#include <string>
#include "person0.h"
int main()
{
using namespace std;
Person one;
Person two("Smythecraft");
Person three("Dimwiddy", "Sam");
one.Show();
cout << endl;
one.FormalShow();
cout << endl;
two.Show();
cout << endl;
two.FormalShow();
cout << endl;
three.Show();
cout << endl;
three.FormalShow();
cin.get();
cin.get();
return 0;
}
I am not really a C++ person, so the terminology might be wrong, but I would say that the implementation of the
Person::Person(const std::string & ln, const char * fn)
constructor is missing.