Good afternoon!
Sorry if the question seems rather vague, but here's some (incomplete) code for some context. Specifically, this is about the "UserInfo inputInfo" definition part as seen in the functions UserInfo::setUserInfo() and UserInfo::displayProfile() in the implementation file.
project02.cpp (implementation file)
#include <iostream>
#include "project02.h"
using namespace std;
void UserInfo::setUserInfo()
{
UserInfo inputInfo;
string fName;
string lName;
int bYear;
string city;
string occupation;
cout << "Please enter your first name: ";
cin >> fName;
inputInfo.setFirstName(fName);
cout << "Please enter your last name: ";
cin >> lName;
inputInfo.setLastName(lName);
cout << "You are now registered as: " << inputInfo.getFirstName() << " " << inputInfo.getLastName();
}
void UserInfo::displayProfile()
{
UserInfo inputInfo;
cout << "Profile Information:" << endl;
cout << "Name: " << inputInfo.getFirstName() << " " << inputInfo.getLastName();
}
void UserInfo::setFirstName(string fName)
{
_firstName = fName;
}
string UserInfo::getFirstName()
{
return _firstName;
}
void UserInfo::setLastName(string lName)
{
_lastName = lName;
}
string UserInfo::getLastName()
{
return _lastName;
}
project02.h (header file)
#ifndef PROJECT02_H
#define PROJECT02_H
using namespace std;
class UserInfo
{
public:
string getFirstName();
void setFirstName(string first);
string getLastName();
void setLastName(string last);
int getBirthYear();
void setBirthYear(int year);
string getCurrentCity();
void setCurrentCity(string city);
string getOccupation();
void setOccupation(string occ);
void setUserInfo();
void displayProfile();
private:
string _firstName;
string _lastName;
int _birthYear;
string _currentCity;
string _occupation;
};
#endif // PROJECT02_H
project02main.cpp (main file)
#include <iostream>
#include "project02.h"
using namespace std;
int main()
{
UserInfo inputInfo;
inputInfo.setUserInfo();
return 0;
}
Now the question is: is there an alternative to repeatedly defining the object "UserInfo inputInfo;" each time for a different function in the implementation file?
Don't create objects of the same data type within methods of that datatype at this point - simply call setFirstname() and getFirstname() to use methods that will modify the same object that you are currently using.
Related
I am trying to get variables from a txt file which looks like;
1 Prince Heins 25
2 Lady Bridgette 29
3 Tony Ann 223
4 Lucy Phoenix 35
and here is my code but i can not get the variables from txt file to myArray. I created them with the number of line by using getline and linenumber is equal to linecount . Then i created many objects which is equal to linecount number. Next step was getting the variables from txt file line by line and i tried to use while(pbin) which is my read function. I am trying to send variables to class by using set functions which includes my strings and integer but i get an compile error:[Error] no match for 'operator>>' (operand types are 'std::ifstream {aka std::basic_ifstream}' and 'void') I checked the examples but none of them solved my issue.
Couldn't get the issue and fix it. I would be glad if someone helps.
#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <istream>
#include <iomanip>
#include <cstring>
using namespace std;
class contact{
private:
int listno;
string name;
string surname;
string phonenumber;
public:
contact(){
this->name="Unknown";
this->surname="Unknown";
this->phonenumber="Unknown";
this->listno=0;
}
contact (string name,string surname,string phonenumber){
this->name=name;
this->surname=surname;
this->phonenumber=phonenumber;
this->listno=0;
}
contact(int listno,string name,string surname,string phonenumber){
this->name=name;
this->surname=surname;
this->listno=listno;
this->phonenumber=phonenumber;
}
void setListno(int listno){
this->listno=listno;
}
int getListno(){
return listno;
}
void setName(string name){
this->name=name;
}
string getName(){
return name;
}
void setSurname(string surname){
this->surname=surname;
}
string getSurname(){
return surname;
}
void setNumber(string phonenumber){
this->phonenumber=phonenumber;
}
string getNumber(){
return phonenumber;
}
void showInfos(){
cout << "Listno: " << this->listno << endl;
cout << "Name: " << this->name << endl;
cout << "Surname: " << this->surname << endl;
cout << "Number: " << this->phonenumber<<endl;
}
};
int main(){
int mListno;
string mName;
string mSurname;
string mNumber;
ifstream pbin("phoneData2.txt");
string line;
long linecount;
contact* myArray = new contact[linecount];
for(linecount=0;getline(pbin,line);linecount++);
if(pbin.is_open()){
int i;
for(i=0;i<linecount;i++){
while(
pbin >> myArray[i].setListno(mListno);
pbin >> myArray[i].setName(mName);
pbin >> myArray[i].setSurname(mSurname);
pbin >> myArray[i].setNumber(mNumber);
)
}
}
pbin.close();
return 0;
}
edited:
#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <istream>
#include <iomanip>
#include <cstring>
using namespace std;
class contact{
private:
int listno;
string name;
string surname;
string phonenumber;
public:
contact(){
this->name="Unknown";
this->surname="Unknown";
this->phonenumber="Unknown";
this->listno=0;
}
contact (string name,string surname,string phonenumber){
this->name=name;
this->surname=surname;
this->phonenumber=phonenumber;
this->listno=0;
}
contact(int listno,string name,string surname,string phonenumber){
this->name=name;
this->surname=surname;
this->listno=listno;
this->phonenumber=phonenumber;
}
void setListno(int listno){
this->listno=listno;
}
int getListno(){
return listno;
}
void setName(string name,int count){
this->name=name[count];
}
string getName(){
return name;
}
void setSurname(string surname){
this->surname=surname;
}
string getSurname(){
return surname;
}
void setNumber(string phonenumber){
this->phonenumber=phonenumber;
}
string getNumber(){
return phonenumber;
}
void showInfos(){
cout << "Listno: " << this->listno << endl;
cout << "Name: " << this->name << endl;
cout << "Surname: " << this->surname << endl;
cout << "Number: " << this->phonenumber<<endl;
}
friend istream & operator>> (istream & in, contact & con){
in >> con.listno >> con.name >> con.surname >> con.phonenumber;
return in;
}
};
int main(){
ifstream pbin("phoneData2.txt");
string line;
long linecount=0;
contact* myArray = new contact[linecount];
for(linecount=0;getline(pbin,line);linecount++);
pbin.seekg(0);
if(pbin.is_open()){
int i;
for(i=0;i<linecount;i++){
if(! (pbin >> myArray[i]))
{
cout << "The contact is not found." ;
}
}
}
pbin.close();
return 0;
}
Add to contact a >> overload
friend std::istream & operator>> (std::istream & in, contact & con)
{
in >> con.listno >> con.name >> con.surname >> con.phonenumber;
return in;
}
and then in main, the for loop becomes
for(i=0;i<linecount;i++){
if (! (pbin >> myArray[i]))
{ // failed to read a contact
// do something here to clean up the mess
}
}
To round out a couple of the other mistakes,
int main(){
ifstream pbin("phoneData2.txt");
string line;
long linecount = 0; // otherwise you don't know what number linecount starts at
contact* myArray = new contact[linecount];
for(linecount=0;getline(pbin,line);linecount++);
pbin.clear(); // Clear the error flag from hitting the end of the file
// Needed when building to older C++ Standards
pbin.seekg(0); // rewind the file
if(pbin.is_open()){
int i;
for(i=0;i<linecount;i++){
if (! (pbin >> myArray[i]))
{
// do something here to clean up the mess
}
}
}
pbin.close();
return 0;
}
// The function which i was required to make was to.string() in the class,Which i had no idea how to make.This is an odd function(not comparing with the math one.)which returns value in two different types of data types i.e(string,integer).The only thing stuck me was assigning a variable after making (string to.string()) function//The return value of function is something like
[age,first_name,last_name,standard](without the square brackets with the commmasin the output)
p.s=need a simpler function without using vector header.
#include <iostream>
#include <sstream>
using namespace std;
class Student{
public :
void set_age(int no){
age_no=no;
}
void set_standard(int no){
std_no=no;
}
void set_first_name(string identity){
name_letter=identity;
}
void set_last_name(string identity2){
last_name_letter = identity2;
}
int get_age(){
return age_num;
}
int get_standard(){
return std_no;
}
string get_first_name(){
return name_letter;
}
string get_last_name(){
return last_name_letter;
}
private :
int age_no;
int std_no;
string name_letter;
string last_name_letter;
};
int main() {
int age, standard;
string first_name, last_name;
cin >> age >> first_name >> last_name >> standard;
Student st;
st.set_age(age);
st.set_standard(standard);
st.set_first_name(first_name);
st.set_last_name(last_name);
cout << st.get_age() << "\n";
cout << st.get_last_name() << ", " << st.get_first_name() << "\n";
cout << st.get_standard() << "\n";
cout << "\n";
cout << st.to_string();
return 0;
}
If you want to create a string with the format age, first_name, last_name, standard then you could do something like
class Student
{
public:
...
std::string to_string() const;
...
};
std::string Student::to_string() const
{
return std::to_string(get_age()) + ", " +
get_first_name() + ", "
get_last_name() + ", "
std::to_string(get_standard());
}
As an aside, I would suggest making all of your getter functions const for example
int get_age() const;
This denotes that the method will not mutate or modify any of the values of the class's member variables.
I am tasked to create a Print function that prints user inputted data that is specific to an object. This print function must use the Get() Function commands I created.
I have googled and looked for similar questions but could not find a way of how I could approach this. How can I create this function my teacher wants?
The object I want to print specifically is book1
My code:
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
class Book {
public:
void SetTitle(string title_input);
string GetTitle();
void SetAuthor(string& author_input);
string GetAuthor();
void SetCopyRightYear(int copyright_year_input);
int GetCopyRightYear();
void PrintBook();
private:
string title;
string author;
int copyright_year;
};
void Book::SetTitle(string title_input) {
title = title_input;
}
string Book::GetTitle() {
return title;
}
void Book::SetAuthor(string& author_input) {
author = author_input;
}
string Book::GetAuthor() {
return author;
}
void Book::SetCopyRightYear(int copyright_year_input) {
copyright_year = copyright_year_input;
}
int Book::GetCopyRightYear() {
return copyright_year;
}
void Book::PrintBook() {
cout << "Title of Book: " << GetTitle() << endl;
cout << "Author of Book: " << GetAuthor() << endl; // Function is broken FIXME
cout << "Copyright Year: " << GetCopyRightYear() << endl;
}
int main ()
{
string title_input = "";
string author_input = "";
int copyright_year_input = 0;
Book book1;
Book book2;
Book book3;
Book book4;
cout << "Enter the book title: ";
cin >> title_input;
book1.SetTitle(title_input);
cout << book1.GetTitle();
cout << "Enter the author name: ";
cin >> author_input;
book1.SetAuthor(author_input);
cout << "Enter the copyright year: ";
cin >> copyright_year_input;
book1.SetCopyRightYear(copyright_year_input);
cout << PrintBook();
Book.h
#pragma once
#include <string>
class Book
{
public:
Book() = default;
~Book() = default;
const std::string GetTitle() const;
const std::string GetAuthor() const;
const int GetCopyRightYear() const;
void SetTitle(const std::string);
void SetAuthor(const std::string);
void SetCopyRightYear(const int);
void PrintBook();
private:
std::string title;
std::string author;
int copyright_year;
};
Book.cpp
#include "Book.h"
// ------------------------------
#include <iostream>
void Book::SetTitle(const std::string title_input)
{
title = title_input;
}
const std::string Book::GetTitle() const
{
return title;
}
const int Book::GetCopyRightYear() const
{
return copyright_year;
}
const std::string Book::GetAuthor() const
{
return author;
}
void Book::SetCopyRightYear(const int copyright_year_input)
{
copyright_year = copyright_year_input;
}
void Book::SetAuthor(const std::string author_input)
{
author = author_input;
}
void Book::PrintBook()
{
std::string output_str = "";
std::cout << "Title of Book: " << GetTitle() << std::endl;
std::cout << "Author of Book: " << GetAuthor() << std::endl;
std::cout << "Copyright Year: " << GetCopyRightYear() << std::endl;
}
main.cpp
// C++ Libraries.
#include <iostream>
#include <string>
// User classes
#include "Book.h"
// Namespaces
int main()
{
std::string title_input = "";
std::string author_input = "";
int copyright_year_input = 0;
// research dynamic memory allocation.
Book book1;
Book book2;
Book book3;
Book book4;
// user sets book title.
std::cout << "Enter the book title: ";
std::getline(std::cin, title_input);
book1.SetTitle(title_input);
// user sets the authors name
std::cout << "Enter the author name: ";
std::getline(std::cin, author_input);
book1.SetAuthor(author_input);
// user inputs the copyright year.
std::cout << "Enter the copyright year: ";
std::cin >> copyright_year_input;
book1.SetCopyRightYear(copyright_year_input);
// Display the information.
book1.PrintBook();
}
Notes:
When you start using multiple namespaces its easier to see what is what if you dont predefine them.
Const correctness means you and other developers know what can be changed and what cant. It also makes things clearer for the compiler.
std::getline reads the whole line including the blank spaces.
Just a quick note on clarity and understanding. At the moment your code is messy which makes it incredibly hard to debug not only for yourself but for others.
I can't tell on here but just in case, your classes should be in header and source code formatting, with a main source code file for the main function (entry point). Whether or not you've been told this information before I would highly recommend doing some research into basic C++. Just for starters I've put some links below to help. Once your code is neatly formatted you might work out what the problem is.
Happy coding :)
References:
Herb Sutter Cpp Convention 2014 - Simplicity over Complexity:
https://www.youtube.com/watch?v=xnqTKD8uD64
Headers and Includes - C++ formatting:
http://www.cplusplus.com/forum/articles/10627/
Also see the tutorials on cplusplus.com.
CustomerInfo.cpp
#include "CustomerInfo.h" // <==== Funtion Definition
CustomerInfo::CustomerInfo() // <==== The Scope Resolution which is two colons :: gives me access to the class
{
newZipCode = 0;
}
CustomerInfo::CustomerInfo(string name, string address, int zipCode)
{
newName = name;
newAddress = address;
newZipCode = zipCode;
}
CustomerInfo::~CustomerInfo()
{
}
string CustomerInfo::getName() const
{
return newName;
}
string CustomerInfo::getAddress() const
{
return newAddress;
}
int CustomerInfo::getZipCode() const
{
return newZipCode;
}
The main.cpp file
#include <iostream>
#include <string>
#include "CustomerInfo.h"
using namespace std;
int main()
{
string name;
string address;
int zipCode;
cout << "Enter your name: ";
getline (cin, name);
cout << "Enter your address" << endl;
getline (cin, address);
cout << "Enter your ZipCode: ";
cin >> zipCode;
CustomerInfo Real_1(name, address, zipCode);
cout << endl << " Name: " << Real_1.getName() << endl <<
"Address: " << Real_1.getAddress() << endl <<
"ZipCode: " << Real_1.getZipCode() << endl;
return 0;
}
The CustomerInfo.h file
#ifndef CUSTOMERINFO_H
#define CUSTOMERINFO_H
// Header ==> Function Declaration
#include <iostream>
#include <string>
using namespace std;
class CustomerInfo
{
public:
CustomerInfo(); // <==== Default Constructor
CustomerInfo(string, int); // <==== Overload Constructor
~CustomerInfo(); // <===== Destructor - Done using an object it will be destroyed out of memory'
string getName() const; // <==== Accessor Functions - Return member variables one value at a time. In addition, no void function will be used.
// getName - returns name of person
string getAddress() const;
// getAddress - returns address of person
int getZipCode() const;
// getZipCode - returns zipcode of person
private:
//Member Variables
string newName;
string newAddress;
int newZipCode;
}; // <=== Requires semicolon after brackets for classes
#endif // CUSTOMERINFO_H
The error I receive is out-of-line definition of 'CustomerInfo' does not match any declaration in 'CustomerInfo'
The following line is the error
CustomerInfo::CustomerInfo(string name, string address, int zipCode)
Your problem seems to be that you define a constructor
CustomerInfo::CustomerInfo(string name, string address, int zipCode)
which does not appear in the class definition (in CustomerInfo.h). The closest one is
CustomerInfo(string, int);
but it doesn't match the signature. Just replace it with
CustomerInfo(string name, string address, int zipCode);
to your class definition in the header file.
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.