friend function not printing out what it should - c++

whenever I run the program, there is no output, the program just ends. Am i doing something wrong? I'm sure there's something i missed but i can't seem to figure it out.
#include <iostream>
#include <string>
using namespace std;
class Addr
{
public:
Addr(int i = 0){
total = i;
}
void addNum(int num){
total += num;
}
int getNum(){
return total; }
friend int print(Addr& var);
private:
int total;
};
int print(Addr& var){
return var.total;
}
int main()
{
Addr object1;
object1.addNum(3);
print(object1);
return 0;
}

Your program behaves correctly. There is no output because you are not printing anything to the console in your program.
The print function merely returns the total.
If you wish to print the value to the console then you could for example change the definition as follows:
int print(Addr& var){
cout << var.total << endl; // this prints to the console output
return var.total;
}

There is no issue with your code. The fact is that no print function is used. I have modified your main function.
int main()
{
Addr object1;
object1.addNum(3);
cout<<print(object1);
return 0;
}

Related

C++ put data from stack to file

I still do not have much knowledge in C ++, I would like to ask for help for a task. I must create a stack, which is filled with data entered by the keyboard and the entire stack to write in external stack. I have made functions push, pop and simple program that displays the stack but before that data must be written in an external file. Can anybody help me with the external file?
#include <iostream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
struct elem
{ int key; elem *next;} *start=NULL, *p;
void push(int n)
{
p=start;
start=new elem;
start->key=n;
start->next=p;}
int pop(int &n){
if (start)
{
n=start->key;
p=start;
start=start->next;
delete p;
return 1;
}
else
return 0;
}
int main(){
int num;
cout<<"Input integers:"<<setw(10);
while (cin>>num)
{
push(num);
}
cout<<endl<<"Stack:"<<endl;
while(pop(num))
{
cout<<num<<endl;
}
return 0;
}
//you can use this pseudocode
ofstream outFile;
while(start!=-1){
outFile << pop() << endl;

C++ reinterpret cast is inconsistent

I made a test program:
#include <iostream>
using namespace std;
class Bug {
private:
char a[25];
int& view (int i) {
return *reinterpret_cast<int*>(&a[i]);}
public:
Bug () {}
void overwrite () {
view(a[0]) = 2;
cout << "value of a[0] is: " << view(a[0]) << endl;
}
};
int main () {
Bug b;
b.overwrite();
return 0;
}
... where I'm testing the view function. Everything works as I'd expect; the value beginning at a[0] is 2, as the print statement in the void method shows.
However, I modify the program slightly:
#include <iostream>
using namespace std;
class Bug {
private:
int x;
char a[25];
int& view (int i) {
return *reinterpret_cast<int*>(&a[i]);}
public:
Bug () {}
void overwrite () {
view(a[0]) = 2;
cout << "value of a[0] is: " << view(a[0]) << endl;
}
};
int main () {
Bug b;
b.overwrite();
return 0;
}
Notice that I added a private member variable int x. This was the only change. The moment I do this, the print statement in the overwrite method no longer prints 2, as in the previous program -- it returns a garbage value (and sometimes the correct value, 2).
More importantly, sometimes the correct value will show, but the program will segfault.
Anybody know what's happening under the hood?
Bonus question. for values lower than 25, my private variable char a[value] will show compiler warnings that mention stack smashing (using g++-4.7).

function was not declared in this scope

#include <iostream>
#include <fstream>
using namespace std;
int information1(int hourR);
int information2(int conTime);
int intformation3(int income1);
int hourR,conTime,income1;
ofstream outfile;
int main()
{
int hours,consultTime,income,fortyPercent,seventyPercent;
outfile.open("Billing amount.txt");
hours=information1(hourR);
consultTime=information2(conTime);
income=information3(income1);
if((income<=25000)&&(consultTime<=30))
{
outfile<<"No charges";
}
else if((income>=25000)&&(consultTime>=30))
{
fortyPercent=hours*.4;
outfile<<fortyPercent;
}
if(consultTime<=20)
{
outfile<<"No charges";
}
else if(consultTime>20)
{
seventyPercent=hours*.7;
outfile<<seventyPercent;
}
outfile.close();
return 0;
}
int information1(int hourR)
{
cout<<"Enter hourly rate.";
cin>>hourR;
return hourR;
}
int information2(int conTime)
{
cout<<"Enter total consulting time";
cin>>conTime;
return conTime;
}
int intformation3(int income1)
{
cout<<"Enter income.";
cin>>income1;
return income1;
}
The compiler says information3 is not declared in this scope. But when I do declare it, the compiler says I cant use it. There might be more problems with this program but as of now I'm stuck on this part. Thanks for the help.
spot the difference
int intformation3(int income1);
^
income=information3(income1);

Calling a function in C++

I'm new to the whole C++ programming thing, and I know this is a easy solution, but I just cant figure it out!
I simply just wanna call a function that prints out 1 + 4.
Here's the code:
#include <iostream>
using namespace std;
int func()
{
cout << 1 + 4;
return 0;
}
int main()
{
int func();
}
It shows nothing in the console window, only that the application stopped with return code 0.
Can someone tell me what's wrong?
You are not calling func() function correctly:
int main()
{
// int func(); This line tries to declare a function which return int type.
// it doesn't call func()
func(); // this line calls func() function, it will ouput 5
return 0;
}
you can just call the function by its name. Like func();
int func()
{
cout << 1 + 4;
return 0;
}
the above function is retruning an integer. you are returning 0. to make it more useful return the sum and catch it in main function.
int func(){
return 1+4;// return 5 to main function.
}
now in main.
int main (){
int ans = func();// ans will catch the result which is return by the func();
cout<<ans;
return 0;
}
try to understand the working of each statement.

Two questions of c++ which just change a little,however very different answers

Recently I do a exercise about algorithm with c++. Exercise in here:poj
I find two very confused questions.
I write a class MAZE and there are three primary functions in MAZE,they are
int left_path();int right_path();int mini_path();
and a function to print the answers:
void display(){
cout<<left_path()<<" "<<right_path()<<" ";
cout<<mini_path()<<endl;
}
the program can work correctly.As we see the function display() can be easy;
I write like this
void display(){
cout<<left_path()<<" "<<right_path()<<" "<<mini_path()<<endl;
}
just one change ;however the program can't work,it like loop infinitely.
following is the other question:
the function mini_path's frame like this
int maze::mini_path(){
ini();
queue<pair<int,int> > q;
q.push(make_pair(x,y));
while(!q.empty()){
pair<int,int> tmp=q.front();
q.pop();
int t=...;
if(E){
return t;
}
if(E){
S
}
if(E){
S
}
if(E){
S
}
if(E){
S
}
}
return -1;
}
if there is "return -1" in the end ,the function works right,else the function return random big number.
The program is in only one file and i use the gun compiler.
I don't show the total codes,because i think nobody wants to see them.I just want to ask what problems may lead above strange behaviors.
source code(simplified for question2):
typedef enum {LEFT=-1,RIGHT=1,UP,DOWN} direction;
ifstream fin("file_test3.txt");
class maze{
public:
maze(){input();}
int mini_path();
void input();
void display(){
cout<<mini_path()<<endl;
}
private:
bool is_not_dest(){
return !(x==d_x && y==d_y);
}
void ini_dir()
{
if(e_x==0) dir=DOWN;
else if(e_x==height-1) dir=UP;
else if(e_y==0) dir=RIGHT;
else dir=LEFT;
}
void ini(){
x=e_x;
y=e_y;
path_lenth=1;
ini_dir();
}
direction dir,d;
int width,height,maze_map[40][40],path_lenth;
int x,y,e_x,e_y,d_x,d_y;
};
void maze::input()
{
fin>>width>>height;
char sym;
for(int i=0;i<height;++i)
for(int j=0;j<width;++j){
fin>>sym;
if(sym=='#')
maze_map[i][j]=1;
else if(sym=='.')
maze_map[i][j]=0;
else if(sym=='S'){
maze_map[i][j]=-1;
e_x=i;
e_y=j;
}
else {
maze_map[i][j]=-2;
d_x=i;
d_y=j;
}
}
}
int maze::mini_path()
{
ini();
queue<pair<int,int> > q;
if(dir==LEFT) {maze_map[x][--y]=2;}
else if(dir==RIGHT) {maze_map[x][++y]=2;}
else if(dir==UP) {maze_map[--x][y]=2;}
else {maze_map[++x][y]=2;}
q.push(make_pair(x,y));
while(!q.empty()){
pair<int,int> tmp=q.front();
q.pop();
x=tmp.first;
y=tmp.second;
int t=maze_map[x][y]+1;
if((x==d_x && (y-d_y==1 || y-d_y==-1)) ||(y==d_y && (x-d_x==1||x-d_x==-1))){
return t;
}
if(maze_map[x-1][y]==0){
maze_map[x-1][y]=t;
q.push(make_pair(x-1,y));
}
if(maze_map[x+1][y]==0){
maze_map[x+1][y]=t;
q.push(make_pair(x+1,y));
}
if(maze_map[x][y-1]==0){
maze_map[x][y-1]=t;
q.push(make_pair(x,y-1));
}
if(maze_map[x][y+1]==0){
maze_map[x][y+1]=t;
q.push(make_pair(x,y+1));
}
}
return -1;
}
main()
{
int n;
fin>>n;
while(n-- >0){
class maze m;
m.display();
}
}
I see it! Can you see it? :)
#include <iostream>
using namespace std;
int foo(int bar)
{
cout << bar << endl;
return bar;
}
int _tmain(int argc, _TCHAR* argv[])
{
cout << foo(1) << foo(2) << foo(3) << endl;
return 0;
}
The output:
3
2
1
123
regarding question1:
The order in which the functions are called will be different.
the first solution will call them in following order:
right_path
left_path
mini_path
the second solution results in following order:
mini_path
right_path
left_path
so the solution you probaly want is:
void display(){
cout<<left_path()<<" ";
cout<<right_path()<<" ";
cout<<mini_path()<<endl;
}
There is not enough info to answer the first question; both codes are equivalent.
[Edit:Check other answers. Anyway, both codes should be equivalent: you have bugs in your code.]
About the second question, I guess that that "return -1" marks "no possible path" in your maze, that's why, when you remove it, your program stops working.
In the maze problem, a backtracking algorithm moves square by square. When from a square there is no possible path, this square must be marked as no path.