+ 2

C++ code issue

Hi! iwrote a c++ simple program for practicing and im having a big issue. In the this simple program, the user set his name and his basketball habilities (between 0 and 100) and then his habilities are compared with the "othi"(the program player) habilities the issue: i used a function to the user set his habilities values, but i cant get this values back to the main function, Could you please tell me how can i make this program work? sorry by my english, thanks #include <iostream> #include <string> using namespace std; //Declaring all the functions void test(int a, int b); int test2(int x); int DATAp(int a, int b, int c, int z); //Player 1 variable declaration string Player1Name=""; int P1JumpQ=0; int P1shootingQ=0; int P1dribleQ=0; ; int main(){ //Player 2 variable declaration string Player2Name="Othi"; int P2JumpQ=75; int P2shootingQ=45; int P2dribleQ=70; int media2=(P2JumpQ+P2shootingQ+P2dribleQ)/3; //input cout<<"set your basketball player habilities\n set a number between 1 to 100\n"; cout<<"name: "; cin>> Player1Name; int media; // Im using a function (DATAp) to input the habilities value cout<< DATAp( P2JumpQ, P2shootingQ, P2dribleQ, media)<<endl; // im trying to test if the habilities value are the same in the main function media=(P1JumpQ+P1shootingQ+P1dribleQ)/3; cout<<"Your drible ability is= "<< P1dribleQ<<endl; cout<<"media provisoria= " <<media<<endl; //functions tests cout<<"the media of your player "<< Player1Name<<" is: "<< test2(media); test(media, media2); } // Cin all the habilities values int DATAp(int a, int b, int c, int d){ //Do while loop, for the user do not set a value greater than 100 do { cout<<"\nJump power (number<=100): "; cin>> a; //if that condition is true, break the loop if (a<100){ break; } }while (a>100); // If THIS while condition is true, restart the loop do{ cout<<"\nShoot

28th Nov 2016, 11:37 AM
Othienno Rafael Soto Esperança
Othienno Rafael Soto Esperança - avatar
10 Antworten
+ 3
If your code is in your code section then please do it public. I cannot understand your code clearly
28th Nov 2016, 11:46 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 3
You mean when you take user input and than print its value it give you 0. like cout<<"Your drible ability is= "<< P1dribleQ<<endl; on above line you want to print value of P1dribleQ that is given by user input right? please reply
28th Nov 2016, 1:03 PM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 2
Check this, sorry I modified it a lot but I have to do it because of logical errors. It can be made more efficient though. #include <iostream> #include <string> using namespace std; //Declaring all the functions void test(int a, int b); void DATAp(); //Player 1 variable declaration (global variables) string Player1Name=""; int P1JumpQ=0; int P1shootingQ=0; int P1dribleQ=0; int media; int main(){ //Player 2 variable declaration string Player2Name="Othi"; int P2JumpQ=75; int P2shootingQ=45; int P2dribleQ=70; int media2=(P2JumpQ+P2shootingQ+P2dribleQ)/3; cout<<"Set your basketball player name: "; cin>> Player1Name; // Im using a function (DATAp) to input the habilities value DATAp(); cout<<"\n\nYour Jump power ability is= "<< P1JumpQ<<endl; cout<<"\nYour Shooting ability is= "<< P1shootingQ<<endl; cout<<"\nYour drible ability is= "<< P1dribleQ<<endl; cout<<"\n\nThe media of " << Player1Name << " is: " << media << endl; cout<<"\n\n" << Player2Name << " Jump power ability is= "<< P1JumpQ<<endl; cout<<"\n" << Player2Name << " Shooting ability is= "<< P1shootingQ<<endl; cout<<"\n" << Player2Name << " drible ability is= "<< P1dribleQ<<endl; //functions tests cout<<"\n\nThe media of " << Player2Name <<" is: "<< media2; test(media,media2); return 0; } // Cin all the habilities values void DATAp(){ //Do while loop, for the user do not set a value greater than 100 do { cout<<"\nJump power (number<=100): "; cin>> P1JumpQ; } while (P1JumpQ>100); // If THIS while condition is true, restart the loop do{ cout<<"\nShooting ability (number<=100): "; cin>> P1shootingQ; }while (P1shootingQ>100); do{ cout<<"\nDrible ability (number<=100): "; cin>> P1dribleQ; }while (P1dribleQ>100); // i sum all the values for then divide them by 3 media = (P1JumpQ + P1shootingQ + P1dribleQ)/3; } // Thi
28th Nov 2016, 1:20 PM
Mohammed Maaz
Mohammed Maaz - avatar
28th Nov 2016, 11:59 AM
Othienno Rafael Soto Esperança
Othienno Rafael Soto Esperança - avatar
+ 1
program continued.... void test (int a, int b){ if(a > b){ cout<<"\n\n" << Player1Name << " was declared greater than othi."<<endl; } if(a == b){ cout<<"\n\n" << Player1Name << " was declared equal to othi."<<endl; } if(a < b){ cout<<"\n\n" << Player1Name << " was declared least than othi."<<endl; } } Can also find it here: https://code.sololearn.com/c0Ta95RIU6qY/#cpp
28th Nov 2016, 1:23 PM
Mohammed Maaz
Mohammed Maaz - avatar
+ 1
It was not appearing in main as you thought because you were not updating it's value in the DATAp function, so it remains 0 as you initialized it.
28th Nov 2016, 1:37 PM
Mohammed Maaz
Mohammed Maaz - avatar
+ 1
Thanks you a lot Mohammed Maaz i guess in this part it should be the player2 habilities cout<<"\n\n" << Player2Name << " Jump power ability is= "<< P2JumpQ<<endl; cout<<"\n" << Player2Name << " Shooting ability is= "<< P2shootingQ<<endl; cout<<"\n" << Player2Name << " drible ability is= "<< P2dribleQ<<endl; i would like to know if i shouldnt use parameters in the functions, i saw in your code that you didn't. Is this the why the user values were not on main?
28th Nov 2016, 1:46 PM
Othienno Rafael Soto Esperança
Othienno Rafael Soto Esperança - avatar
+ 1
ohhh i saw you used void DATAp instead int DATAp, thanks again for the help ;)
28th Nov 2016, 1:55 PM
Othienno Rafael Soto Esperança
Othienno Rafael Soto Esperança - avatar
+ 1
No problem dude, you are always welcome! and the reason I didn't use the parameters is because there was no such need for them, as you have declared the variables globally. you can modify the program according to your needs anyway.
29th Nov 2016, 3:14 AM
Mohammed Maaz
Mohammed Maaz - avatar
0
cout<<"Your drible ability is= "<< P1dribleQ<<endl; yeah, with this line i was just trying to test if the value given by the user appears on main, but doesnt because it prints 0
28th Nov 2016, 1:32 PM
Othienno Rafael Soto Esperança
Othienno Rafael Soto Esperança - avatar