only one function from class intilized in main function of c++ - c++

I just wrote a simple program that has two functions in a class. Problem is when I called them from main(), only the first function executes and the program terminates without calling the second function.
#include <stdio.h>
#include <iostream>
using namespace std;
class exp{
public:
string name;
public:
string fun1(){
cout<<"please enter value for first function ";
cin>>name;
cout<<"yourname from first function is ";
cout<<name;
return 0;
}
string fun2(){
cout<<"Please enter value for second function ";
cin>>name;
cout<<"yourname from second function is ";
cout<<name;
return 0;
}
};
int main(){
exp b1,b2;
cout << b2.fun1();
cout << b1.fun2();
}
The output is
please enter value for first function preet
yourname from first function is preet

You are returning 0 while the return type is string. Constructing a std::string from a null pointer is not allowed. You could use return ""; instead.

Here, try this
#include <stdio.h>
#include<iostream>
using namespace std;
class exp
{
private: // changed from public to private
string name;
public:
int fun1() // changed from string to int
{
cout<<"\nplease enter value for first function ";
cin>>name;
cout<<"\nyourname from first function is ";cout<<name<<endl;
return 0;
}
int fun2() // changed from string to int
{
cout<<"\nPlease enter value for second function ";
cin>>name;
cout<<"\nyourname from second function is ";
cout<<name<<endl;
return 0;
}
};
int main()
{
exp b1,b2;
b2.fun1(); // removed cout
b1.fun2(); // removed cout
}
The problem was you were using cout inside your functions, yet you were also calling them inside a function, that is cout<<b2.fun1();. This is not a good practice.
There was also the problem that the type of your functions were string, but they were returning an integer.
You had also made name as public which just defies the use of OOP. So I made it private.
Cheers......Hope this solves your Problems.. :)

Related

How to set a custom variable name through a function argument

So what i am trying to do is basically get the second argument of a function, and making the second argument the name of a variable so i can easily store the users input. Here is my code
`
#pragma once
#include <iostream>
using namespace std;
void askAndStore(string question, string variable)
{
cout << question + " ";
cin >> variable;
}
`
Pass variable by reference:
void askAndStore(string question, string& variable)
{
cout << question + " ";
cin >> variable;
}
string& instead of string. Without using & you'd be passing in a copy of your varaible, using a reference type string& you pass the actual variable in which is then modified by cin >> variable;.
P.S Don't use using namespace std;
You can't do this in C++.
You could have a std::map<std::string, SomeType> that you populate with your read-in names.
#pragma once
#include <iostream>
#include <map>
#include <string>
class Values
{
std::map<std::string, std::string> values;
public:
void askAndStore(std::string question, std::string name)
{
std::cout << question << " ";
std::cin >> values[name];
}
std::string get(std::string name)
{
return values[name];
// or return values.at(name); if name must already exist in values
}
};
int main()
{
Values user;
user.askAndStore("What is your name?", "usersName");
}
That assumes your values are std::strings

Why '&' reference in not required in classes in C++?

in the following code for swapping by call by refernce is used
#include <iostream>
#include <conio.h>
using namespace std;
void swap(int& c,int& d)
{
int temp;
temp=c;
c=d;
d=temp;
}
int main()
{
int a,b;
cout<<" Enter value of a";
cin>>a;
cout<<"\n Enter value of b";
cin>>b;
swap(a,b);
cout<<"a: "<<a;
cout<<"\n b:"<<b;
return 0;
}
but if i use class and make object then there is no need of '&' refernce in calling the method.
#include <iostream>
#include <conio.h>
using namespace std;
class swapy
{
public:
int a;
int b;
void swap(int c,int d)
{
int temp;
temp=c;
c=d;
d=temp;
}
};
int main()
{
swapy s;
cout<<" Enter value of a";
cin>>s.a;
cout<<"\n Enter value of b";
cin>>s.b;
swap(s.a,s.b);
cout<<"a: "<<s.a;
cout<<"\n b:"<<s.b;
return 0;
}
Can you explain how this worked?Why & was not needed in the second program.
First of all you are not calling your class swap() function, because you are not calling it with your class object s, for class swap() function you have to call it with s. like
s.swap(c,d);
Further Change your function swap() name to any other name like mySwap() then you'll get to know that this is not correct, because swap() is a built-in function of std::
so it will be simple to understand if you change the name of your function.
Furthermore, you are not using class member variables correctly, you have to do something with your class member variables a and b. your class is useless here.

Function should have a prototype while declaring in class

I have a string to be printed via a function. I am using turbo-c compiler.
While using procedural method I am able to do it from following code :
#include <iostream.h>
#include <conio.h>
void strr(char name[]);
void main(){
char name1[10];
cout << "Enter name";
cin >> name1;
strr(name1);
getch();
}
void strr(char name[]){
cout << name;
}
But With oop method I am not able to print the string. My Code is :
#include <iostream.h>
#include <conio.h>
class name{
public: void strr(char name[]);
};
void main(){
char name1[10];
cout << "Enter name";
cin >> name1;
strr(name1);
getch();
}
void name::strr(char name[]){
cout << name;
}
With oop method I am getting error Function 'strr' hould have a prototype.
Since your function is defined inside the class, you need an object/instance of the name class to invoke it :
name obj;
cin >> name1;
obj.strr(name1);
Alternatively, if you declare the function as static, then you can invoke it without a class-instance, since the function is a class-function :
class name{
public: static void strr(char name[]) {cout << name << endl;}
};
...
cin >> name1
name::strr(name1);
Try this
name :: void strr(char name[])
{}

Adding a class to a map

I am trying to add a class object to a map, this is what I have:
#include<vector>
#include<map>
#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
class Student{
int PID;
string name;
int academicYear;
public:
Student(int, string, int);
};
Student::Student (int P, string n, int a) {
PID = P;
name = n;
academicYear = a;
}
void createStudent(map<string, Student>);
int main(int argc, char** argv){
map <string, Student> studentList;
createStudent(studentList);
}
void createStudent(map<string, Student> studentList){
int PID;
string name;
int academicYear;
cout << "Add new student-\nName: ";
getline(cin, name);
cout << "PID: ";
cin >> PID;
cout << "Academic year: ";
cin >> academicYear;
Student newstud (PID, name, academicYear);
studentList[name] = newstud; //this line causes the error:
//no matching function for call to
//'Student::Student()'
}
I don't understand why the constructor function is being called there, I thought newstud would already have been constructed from the previous line. Can anyone explain what is happening when I try to add newstud to the map?
Problem 1st
std::map::operator[] will insert new element into container if its not present, using default constructor, which in your case is not present and will probably doesn't make sense even if you provide one.
So use std::map::insert for such scenario
Problem 2nd
Even if you successfully insert using studentList.insert(std::make_pair(name, newstud)); its not going to reflect changes in original map studentList in main ( ) unless you use a reference type, in function definition & declaration of createStudent
So use
void createStudent(map<string, Student>& );
In order to add entries using the function, you should make your parameter studentList pass by reference.
And instead of
studentList[name] = newstud;
use:
studentList.insert(std::make_pair(name, newstud));
Just a suggestion, though.

Could not separate implementation and interface

I am unable to compile the following files.
I am trying to pass the name and age to an object and after checking and assigning each age to proper category(adult, kid...) then i am trying to print it.
My 3 files are following:
The first 1:
//cannot access private member declared in class Person
//No constructor could take the source type, or constructor overload resolution was ambiguous.
#include <iostream>
#include <string>
using namespace std;
#include "person2.h"
void getData(Person&);
void displayData(Person&);
int main(){
Person p;
getData(p);
displayData(p);
}
void getData(Person& p){
cout<< "Enter the name: ";
cin>> p.name;
cout<<"Enter the age: ";
int age;
cin>> age;
p.setAge(age);
p.ageGroup = p.determineAgeGroup(age);
}
void displayData(Person& p){
cout<<p.name<< " is in the group of " << p.ageGroup <<endl;
}
The second one:
#include <iostream>
#include <string>
using namespace std;
class Person {
public:
string name;
string ageGroup;
void setAge(int&);
string getAge();
string getAgeGroup(int);
private:
int age;
string determineAgeGroup(int );
};
The third one:
#include <iostream>
#include <string>
#include "person2.h"
using namespace std;
void Person::setAge(int& a){
if(a<0) cout<< "No";
}
string Person::getAge(){
return age;
}
string Person::determineAgeGroup(int a){
if(a>= 65) return "Senior";
else if(a<65 & a>= 20) return "Adult";
else if(a<20 & a>= 13) return "Teen";
else return "Kid";
}
string Person::getAgeGroup(int a){
return determineAgeGroup(a);<<endl;
}
Check the following:
endl instead of end in displayData
declare int age in getData and pass that value to p.setAge
return value from getAge should be an int
make determineAgeGroup() public or call determineAgeGroup() from a member function like setAge() or call your public function getAgeGroup().
This should at least get you compiling...
Note: The final edit that solved the problem was bullet point number 4, in particular calling the public function getAgeGroup().