Post: More c++ trouble!!!ugh
04-20-2011, 03:24 PM #1
|C++|
< ^ > < ^ >
(adsbygoogle = window.adsbygoogle || []).push({}); problem solved..
Last edited by |C++| ; 04-20-2011 at 05:03 PM.
04-20-2011, 03:55 PM #2
Originally posted by SLiiTH3R View Post
Ok so im having trouble printing the values. if you read thee code you can see i have a constructor with the initialiser saying
Soldier int (startHealth,int startStrenth):
health(startHealth),
strenth(startStrenth)
{
//nothing yet
}
health and strenth are private
so i linked it over to some public functions. then later on when i define it in the int main() ( i define it with the constructor like :
int main()

Soldier bob(7,9);
bob.attack();
when i call bob.attack() it should output
"int attack()
{
cout<<"he attacked with"<<startStrenth()<<"strenth";
return 0;
}
but all it outputs is "he attacked with0" its putting the return value at the end not the "9" which i assigned here in the
int main()

Soldier bob(7,9);

here is the full code,in a nut shell basically i want it to print the strenth which i assigned through Soldier bob(7,9);
as you can see 9 stands for startStrenth which i called in bob.attack;
anyways i think you will see the problem when you debug this
    
#include "stdafx.h"
#include <iostream>

using namespace std;
class Soldier
{
private:
int health;
int strenth;
public:
Soldier(int startHealth,int startStrenth):
health(startHealth),
strenth(startStrenth)
{
//nothing yet
}
int startHealth()
{
health;
return 0;
}
int startStrenth()
{
strenth ;
return 0;
}
int attack()
{
cout<<"**Soldier aims down the barrel of his MP5K and lets off 20 BULLETS FULLY AUTO ** he attacked with"<<startStrenth();
return 0;
}
};

int main()
{
Soldier bob(7,9);
bob.attack();
int h;
cin>>h;
return 0;
char f;
cin>>f;
}

sorry if my explanation is bad but im sure you'll get it.


return 0 in your strenth and health functions returns 0. Return 0 means the operation has completed and its true. Do return strenth; and return health; instead of return 0.

    #include <iostream>

using namespace std;
class Soldier
{
private:
int health;
int strenth;
public:
Soldier(int startHealth,int startStrenth):
health(startHealth),
strenth(startStrenth)
{
//nothing yet
}

int startHealth()
{
health;
return health;
}

int startStrenth()
{
strenth;
return strenth;
}

int attack()
{
cout<<"**Soldier aims down the barrel of his MP5K and lets off 20 BULLETS FULLY AUTO ** he attacked with"<<startStrenth();
return 0;
}
};

int main()
{
Soldier bob(7,9);
bob.attack();
int h;
cin>>h;
return 0;
char f;
cin>>f;
}

04-20-2011, 04:09 PM #3
|C++|
< ^ > < ^ >
Originally posted by mobatey View Post
return 0 in your strenth and health functions returns 0. Return 0 means the operation has completed and its true. Do return strenth; and return health; instead of return 0.

    #include <iostream>

using namespace std;
class soldier
{
private:
Int health;
int strenth;
public:
Soldier(int starthealth,int startstrenth):
Health(starthealth),
strenth(startstrenth)
{
//nothing yet
}

int starthealth()
{
health;
return health;
}

int startstrenth()
{
strenth;
return strenth;
}

int attack()
{
cout<<"**soldier aims down the barrel of his mp5k and lets off 20 bullets fully auto ** he attacked with"<<startstrenth();
return 0;
}
};

int main()
{
soldier bob(7,9);
bob.attack();
int h;
cin>>h;
return 0;
char f;
cin>>f;
}



genius thank you so much +rep

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo