Sign in to follow this  
somebodys_life

something to make this easier...

Recommended Posts

i know there is something to make this easier i just can't remember what it is or how to do it. I have a series of functions that i need to be operated multiple times. (C++) So i put them into something similar to a class and i just call that class everytime i wish to perform that series of functions. I can't remember what this is or the general syntax. If anyone could give me a brief description and tell me what it is i am thinking of i would really appreciate it. thanks

Share this post


Link to post
Share on other sites
It sounds like you a bit confused on terminology.

You don't "operate" functions. You call them. However, classes are templates for creating and using objects. You can't call classes.

Although from your post, it's not clear what you're trying to do, I'm going to guess you are trying to create a function to call several functions in succession. Here's how you would do that:


void FuncA()
{
...
}

void FuncB()
{
...
}

void FuncC()
{
}

void CallAllFuncs()
{
FuncA();
FuncB();
FuncC();
}

.... // later in your main or whereever

CallAllFuncs();


These functions can also take parameters and return values.

Share this post


Link to post
Share on other sites
ok another thing, i want to ad something to an int, and i can't get it to work properly..

its just,

if (blah){
+5 x;
}

and it won't work, i'm not sure if that is correct or not.

thanks

Share this post


Link to post
Share on other sites
Quote:
Original post by somebodys_life
ok another thing, i want to ad something to an int, and i can't get it to work properly..

its just,

if (blah){
+5 x;
}

and it won't work, i'm not sure if that is correct or not.

thanks

what language are you using? that's definitely not a C derivative language.

Share this post


Link to post
Share on other sites

int A;

A = 7; // int 'A' now holds '7'

A += 5; // int 'A' is now '13'

A = A + 5; // int 'A' is now '18'

A -= 2; // int 'A' is now '16'

A = A - 2; // int 'A' is now '14'
_________________________________

int B;

B = 5; //Sets 'B' as five

B = A; //Sets 'B' to whatever 'A' is

B += A; //Adds 'A' to 'B'

B++; //Adds '1' to 'B'

B--; //Subtracts '1' from 'B'

B = B * B; //Multiplys 'B' times itself

A = B * B; //'A' now holds B sqared. (If 'B' is equal to '2', 'A' would now be '4' and 'B' would still be '2'



I think you want:


if(whatever)
{
x += 5;
}



But I can't be sure unless I know what luagage you are using. (i.e. C, C++, Java, et c...)

Share this post


Link to post
Share on other sites
im having another issue (not enough i know) i keep having problems with the void things...

void stat_show(){
cout<<"You have "<<sol<<" (s) remaining."<<endl;
cout<<"You have "<<power<<" attack points."<<endl;
cout<<"You have "<<def<<" defense points."<<endl;
}

i get the following errors...

expected `;' before "void"
expected primary-expression before "void"

this is placed inside of main.

thanks

Share this post


Link to post
Share on other sites
Maybe you have defined a class or struct or ... before the start_show fuction and forgot the ";".
class C...
{
...
}; // <--- put a ";" here
void start_show

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You need to know what functions are, how to declare them, and how to call them. You also need to know what classes are and at least how to use them. You need to know these things before you start programming games or anything else for that matter. If you didn't know (up until now) how to add 5 to a variable, you are still at step 1 of learning how to program and should buy a book that teaches you some basic C++. All of your answers will probably be in that book.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by somebodys_life
i already have a book, and it describes adding to a variable the exact same way i tried to. "C++ Without Fear"


If it said that "+5 x;" would add 5 to x, then burn that book and buy another one.

Share this post


Link to post
Share on other sites
I mistyped what my code actually was. But i did do it the way the book shows and it dosen't work.

And I remembered this is an error i have been having with the compiler i am re-installing it as i type this and going to see if it fixes the problem.

thanks

Share this post


Link to post
Share on other sites
i just thought this may help, cause i re-installed it and it still dosen't work.



using namespace std;
#include <iostream>


int main(){




int x;
int y;

int sol = 50;

int power = 50;

int def = 50;

int cash = 500;

bool german = false;
bool american = false;
bool russian = false;
bool french = false;


cout<<"Welcome to Axis and Allies, World War 2 Simulation."<<endl;
cout<<"Please begin by choosing your country."<<endl;


while (german == false && american == false && russian == false && french == false) { //check to
// make sure that nothin has been selected that
cout<<"1. Germany"<<endl;//pick german
cout<<"2. America"<<endl;//pick american
cout<<"3. Russia"<<endl;//pick russian
cout<<"4. France"<<endl;//pick french

cin>>x;//ask

if (x == 1){

german = true;
cout<<"You are German!"<<endl;
sol += 55;

}
if (x == 2) {
american = true;
cout<<"You are American!"<<endl;
sol += 50;
}
if (x == 3){
russian = true;
cout<<"You are Russian!"<<endl;
sol += 100;
}
if (x == 4){
french = true;
cout<<"You are French!"<<endl;
sol += 25;
}; // i have tried it with and without the semi here!




void stat_show () {
cout<<"You have "<<sol<<" (s) remaining."<<endl;
cout<<"You have "<<power<<" attack points."<<endl;
cout<<"You have "<<def<<" defense points."<<endl;



}

}


system("PAUSE");
}




Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I’m pretty sure re-installing the compiler will not solve your problem because “+5 x” is not c nor c++.

Share this post


Link to post
Share on other sites
Quote:
Original post by somebodys_life
im having another issue (not enough i know) i keep having problems with the void things...

void stat_show(){
cout<<"You have "<<sol<<" (s) remaining."<<endl;
cout<<"You have "<<power<<" attack points."<<endl;
cout<<"You have "<<def<<" defense points."<<endl;
}

i get the following errors...

expected `;' before "void"
expected primary-expression before "void"

this is placed inside of main.

thanks

let's try

just thought this may help, cause i re-installed it and it still dosen't work.



#include <iostream>
#include <cstdlib>
using namespace std;

void show_stats (); //declaration of function

int main(){

int x;
int y;

int sol = 50;

int power = 50;

int def = 50;

int cash = 500;

bool german = false;
bool american = false;
bool russian = false;
bool french = false;


cout << "Welcome to Axis and Allies, World War 2 Simulation." << endl;
cout << "Please begin by choosing your country." << endl;


while (german == false && american == false && russian == false && french == false) {
//check to make sure that nothin has been selected that
cout << "1. Germany" << endl; //pick german
cout << "2. America" << endl; //pick american
cout << "3. Russia" << endl; //pick russian
cout << "4. France" << endl; //pick french

cin >> x; //ask

if (x == 1){

german = true;
cout << "You are German!" << endl;
sol += 55;
}

if (x == 2) {
american = true;
cout << "You are American!" << endl;
sol += 50;
}

if (x == 3){
russian = true;
cout << "You are Russian!" << endl;
sol += 100;
}

if (x == 4){
french = true;
cout << "You are French!" << endl;
sol += 25;
} // no semi here... ever

} //end while loop


system("PAUSE"); //you need: #include <cstdlib> to use this.
return 0; //the program needs this so don't forget it!
}

//function declaration always after main function. never inside.
void show_stats () {
cout << "You have " <<sol<< " (s) remaining." << endl;
cout << "You have "<<power<<" attack points." << endl;
cout << "You have " << def << " defense points." << endl;
}

I'm sorry I just had to correct that [smile]
And what you need is a Beginner C++ programming book.
Notice there's nothing about games in the above statement.

edit: after "correcting" the function i realized he did nothing wrong. the (lack of) spacing threw me off. so i just corrected the program instead.

Share this post


Link to post
Share on other sites
This will work:

#include <iostream>
using namespace std;
int sol = 50;

int power = 50;

int def = 50;

void stat_show () {
cout<<"You have "<<sol<<" (s) remaining."<<endl;
cout<<"You have "<<power<<" attack points."<<endl;
cout<<"You have "<<def<<" defense points."<<endl;



}

int main(){




int x;
int y;

int cash = 500;

bool german = false;
bool american = false;
bool russian = false;
bool french = false;


cout<<"Welcome to Axis and Allies, World War 2 Simulation."<<endl;
cout<<"Please begin by choosing your country."<<endl;


while (german == false && american == false && russian == false && french == false) { //check to
// make sure that nothin has been selected that
cout<<"1. Germany"<<endl;//pick german
cout<<"2. America"<<endl;//pick american
cout<<"3. Russia"<<endl;//pick russian
cout<<"4. France"<<endl;//pick french

cin>>x;//ask

if (x == 1){

german = true;
cout<<"You are German!"<<endl;
sol += 55;

}
if (x == 2) {
american = true;
cout<<"You are American!"<<endl;
sol += 50;
}
if (x == 3){
russian = true;
cout<<"You are Russian!"<<endl;
sol += 100;
}
if (x == 4){
french = true;
cout<<"You are French!"<<endl;
sol += 25;
}; // i have tried it with and without the semi here!





}


system("PAUSE");
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this