Open Side Menu Go to the Top
Register
C++ mega noob question C++ mega noob question

04-27-2015 , 07:07 PM
Hi there.

Newbie alert incoming :-)

Im trying to learn some C++ Just for the fun of it, but there are something that i dont really understand.

I am trying to create a very simple program, but using multiply files. Just because i want to figure out how it works i have sorta made 3 files. I know this proberly dodent make any sense. and there are smarter ways to do it.

But im kinda just doing this because i want to figure out how i can work with mulitply files and have my functions in class.cpp files.

I do have some big problems to understand classes and functions.

My program is sorta made up from these files
Main.cpp
Choise.cpp ( intended this to be some kinda menu class)
Poker.cpp ( where the actual poker calculations takes place)
Choise.h
Poker.h


In my main.cpp

I am calling a function from the poker class. and i want to print out the return value from the function.

How do i do this. Is it even posibel or am I doing something wrong, or maybe i just dont understand it ?

Main.cpp
Code:
 
#include "Choise.h"
#include "Poker.h"

using namespace std;

int main(){

	Choise choise1("valg \n");
	

	char uservalg = choise1.userChoise();

	if (uservalg == 1){
		cout << "You chose to enter the size of the pot. \n\n";
		// call function enter PotSize from poker class
	}

	if (uservalg == 2){
		cout << "You chose to bet. \n\n";
		
		Poker bet;
		bet.betIntoPot();
		// Here I want to print out the return value from the function betIntoPot from the Poker.cpp file.

	}
	if (uservalg == 3){
		cout << "You chose to call a bet. \n\n";
		// call function callBet from poker class
	}


	system("PAUSE");
	return 0;


}
poker.cpp

Code:
#include <iostream>

#include "Poker.h"



Poker::Poker()
{
	
}

float Poker::potSize()
{
float potSize;
cout << "Enter the size of the pot before you action \n\n";
cin >> potSize;
return potSize;
}


int Poker::betIntoPot()
{
	int betSize;
	cout << "Enter size of your bet \n\n";
	cin >> betSize;
	cout << "You have bet " << betSize << " into the pot.\n\n";
	return betSize;
}

int Poker::callBet()
{
	int call;
	cout << "Enter size of bet to call\n\n";
	cin >> call;
	return call;
}

So far the program runs with out any errors. But I just dont know how to use the return (betSize) value from the betIntoPot function in my main program.

Is it even posibel to do this ?
C++ mega noob question Quote
04-27-2015 , 07:18 PM
Why do you want it to return betSize? Like, do you want to assign that value to a different variable? Or do you just want it to echo the betSize that the user inputs?

Can you include Poker.h so we can see how your variables are set up in the class?

I guess to answer your question, you use the return value as you would any type of int, because the function returns an int. So you could do "cout << bet.betIntoPot();“ and it would do your function and then output the return value to cout.
C++ mega noob question Quote
04-27-2015 , 07:26 PM
Make that:

Code:
int returnedBetSize = bet.betIntoPot();
cout << "Returned bet size: " << returnedBetSize << endl;
endl replaces \n\n to work on any platform instead of having to change, for example, between \n\n and \r\n

Now, having said that, there are several issues. That isn't surprising for a beginner so do not panic. Just go with the flow while you learn.

There is one thing in particular you should be aware of as early as possible:
From the files I can see so far, it seems that you do use namespace std in header files, probably in the global scope.
That is something I'd train myself to not do as soon as possible. Like never. Read up on global namespace pollution to get an idea why this is a bad habit.
C++ mega noob question Quote
04-27-2015 , 07:34 PM
Been coding in cpp for a year and it's just now becoming an issue.

What is the proper way for, say, cout then? std::cout? Or something similar?
C++ mega noob question Quote
04-27-2015 , 09:40 PM
printf is faster than cout
C++ mega noob question Quote
04-27-2015 , 11:56 PM
At least encapsulate the "using namespace" commands in .cpp files and preferably within your own namespace or targeted classes/methods/functions.

If you want to be really deliberate about it, opt for "using std::cout" etc and then you can skip "using namespace" all together. Stick to the things you really want to use instead of importing the entire namespace and everything and their grandmothers.
C++ mega noob question Quote
04-28-2015 , 02:11 AM
Thanks for the help Kazana and Anais :-)

Like you both suggested this did it for me.

Code:
int returnedBetSize =  bet.betIntoPot();
cout << "Returned bet size: " << returnedBetSize << endl;
My poker.h

Looks like this.

Code:
#pragma once

using namespace std;

class Poker 
{
public:
	Poker();

	float potSize();

	int callBet();

	int betIntoPot();
};
Also thanks for the hint about replacing \n with endl; and only using what is needed from std:: instead of the entire namespace.

Anyway like I sayd I know it may not make much sense to use the return value like this. But im just sorta fooling a bit around to see how things work :-)
C++ mega noob question Quote
04-28-2015 , 08:19 AM
Hey, that's how we learn. Mess around until we understand why something works the way it does.
C++ mega noob question Quote
04-28-2015 , 05:49 PM
If I'm using standard functions a lot I'll do what kazanna suggested and do using std::cout etc. at the top of each source file, otherwise with less common ones I think it doesn't hurt to explicitly call out the full function name with each call to indicate it's a standard library function and not one of yours for future maintainers, then you just do std::cout << "etc."
C++ mega noob question Quote
04-28-2015 , 08:16 PM
Quote:
Originally Posted by klondi
Thanks for the help Kazana and Anais :-)

Like you both suggested this did it for me.

Code:
int returnedBetSize =  bet.betIntoPot();
cout << "Returned bet size: " << returnedBetSize << endl;
My poker.h

Looks like this.

Code:
#pragma once

using namespace std;

class Poker 
{
public:
	Poker();

	float potSize();

	int callBet();

	int betIntoPot();
};
Also thanks for the hint about replacing \n with endl; and only using what is needed from std:: instead of the entire namespace.

Anyway like I sayd I know it may not make much sense to use the return value like this. But im just sorta fooling a bit around to see how things work :-)
Wouldn't hurt to put your class in a namespace as it helps to organize your code into coherent packages. If you keep adding more functionality to your application you will inevitably develop other classes, functions, etc. that are related.
C++ mega noob question Quote

      
m