| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
A random question
Well maybe this is a stupid question cause I will probably not explain myself good or I am just way off.
But here it goes. okay so I got this Code:
int a_func()
{
int a;
int b;
if( b==2)
{
a= 20;
}
if (b<=30 && b>=20)
a = 30;
//blah blah blah some more if statements
return a;
}
then I want to show it in the int main() Code:
int main()
{
cout<< a_func();
}
Now this is where the problem is I want to change b from int main(). The best thing I thought of was making an integer equal to the integer from the a_func() such as this int b; b = a_func().b; and this int b; b = a_func()::b; but all that doesn't work cause this isn't a class well my question is this How can I change b from the int main() and so that int a can be returned. Oh yeah and this might be useless info but the func() is saved as .h but I think that really doesn't change anything. Last edited by BloodlustShaman : December 12th, 2005 at 09:35 PM. Reason: add |
|
#2
|
||||
|
||||
|
A question is never stupid if you are trying to understand bloodlust and don't let people discourage you if you are trying.
I would make a struct of variables then use the int func() that way you have control over the variables in the int func(). Just my opinion though. Maybe something like this.Code:
struct variables
{
int a;
int b;
};
variables v[10];//or however many you think you need
int func()
{
v[0].a = 10;
cout<<v[0].a<<endl;//this is a small example of how to implement this
//concept you might want to use a for statement rather than an if
//statement that way it chooses the right variable. I could probably
//help better if I knew better what your program was intended to do
}
int main()
{
func();//then you make a call to the func(); much like you would to a class see.
system("PAUSE");
return EXIT_SUCCESS;
}
So you see the concept of what I'm saying, right. If not post more of your code and explain what you are attempting to do bloodlust and Ill try to help a little more. ![]()
__________________
---Official Member Of The Itsacon Fan Club--- ![]() Give a man a fish and he will eat for a day. Teach a man to fish and he will sit in a boat all day drinking beer. |
|
#3
|
||||
|
||||
|
You cannot reach b from outside func(), b is only visible inside the scope of func. The only way to have a b in the func as well as outside the func is to put it in a higher scope, i.e. global or something. Maybe the nicest thing to do here is to pass b as a parameter to the function, if that doesn't change what you need to do.
Good luck! |
|
#4
|
|||
|
|||
|
Well as always thanks for all the help and I really must agree with you, Geo. Cause I didn't explain myself so I will try to explain it.
Also I must say I do not like making another post when it is not asking help or helping so I have to thank everybody in the forums that have helped cause sometimes I do not post a thanks when someone has help me but thats cause I do not like puting stuff that is not helping people or helping me. But back to the problem. Well this is my real stuff.(changed it a bit cause tried your method Geo. ) Code:
# include <iostream>
int show_level()
{
// i had the int level and int experience here.
// they were if statements before and I do not want to post all the for statement // cause there are only the same stuff as these
for (experience == 0)
{int level = 1;}
for (experience == 1)
{int level = 2;}
for (experience > 1 && experience <=3)
{int level = 3;}
//I think it is easy to see that I want it to return level
return level;
}
// the int main()
# include "displaying_levels.h"
# include <iostream>
using namespace std;
int main()
{
cout<<show_level();
// yes no PAUSE nor cin.get() cause there was more but really this is all I want to know
}
Now I must say great idea Geo. finally showed me an idea that I didn't use already lol but I give you all credit for that idea but let me explain.I want the displaying_levels.h to check on the experience and to return the level so that when the user gains enough experience to gain level so that int show_level() can return the new level. Also have to say that I do not like telling the purpose of the program cause people sometimes change directions to what really the purpose of the program was made for. I am doing this so I can have practice in using .h files and I must say I was doing fine tell this part came up, so people I know there are many ways of doing this (and probably easier ways but I want to make this so I can practice in using .h files and getting comfortable using them. The output of the program really is nothing to me) Now back to the program I have tried to use methods to do this like this int show_level(int experience) { int level = 0; //blah blah same if statements return level; } int main() { cout<< show_level(10); } Now I do not know if this one even worked cause I have tried so many but as one of my final idea's in the end of the day I decided to make a int in int main and equal it to experience. But do not know how int show_level() { int experience=0; //blah blah blah samething } int main() { int x; x= func().experience; or x=func()::experience; now you guys can see why I post this same think in my first post. As I said I have tried so many things and I remember one did show a number but it always equal to what I assign level to. Like this //tired of writing so much stuff so stay with me showlevel() int level= 0 or int level =4; but then it returns whatever I put it in the beginning Also I have not started but I will later write a function that adds the experience so that then it will need to enter the showlevel() but my guess it is the same as changing experience in the int main() So this is what I need to do 1.)Probably rewrite the showlevel 2.)Make it so I can change experience outside of the showlevel 3.)Make it so it can return the new level when calling the showlevel Well thanks for all you guys Oops forgot to show how I have it now Code:
//the displaying_levels.h
# include <iostream>
using namespace std;
int experience = 0;
int show_level()
{
int level=0;
// too much errors with for statements
if (experience == 0)
{int level = 1;}
if (experience == 1)
{int level = 2;}
if (experience > 1 && experience <=3)
{int level = 3;}
if (experience >= 4 && experience <= 7)
{int level = 4;}
if (experience >=8 && experience <= 12)
{int level = 5;}
if (experience >=13 && experience <= 20)
{int level = 6;}
if (experience >=21 && experience <= 30)
{int level = 7;}
if (experience >=31 && experience <= 42)
{int level = 8;}
if (experience >= 43 && experience <= 65)
{int level = 9;}
if (experience >= 66 && experience <= 88)
{int level = 10;}
if (experience >= 89 && experience <= 119)
{int level = 11;}
if (experience >= 120 && experience <= 150)
{int level = 12;}
if (experience >= 151 && experience <= 201)
{int level = 13;}
if (experience >= 202 && experience <= 262)
{int level = 14;}
if (experience >= 263 && experience <= 323)
{int level = 15;}
if (experience >= 324 && experience <=329)
{int level = 16;}
if (experience >= 330 && experience <= 599)
{int level = 17;}
if (experience >= 600 && experience <= 900)
{int level = 18;}
if ( experience >= 901 && experience <= 2901)
{int level = 19;}
if (experience >= 2902 && experience <= 5000)
{int level = 20;}
return level;
}
//the main .cpp
# include <iostream>
# include "displaying_weapons.h"
# include "displaying_levels.h"
using namespace std;
int main()
{
display_sword();
display_shield();
display_Wshield();
display_potion();
display_Sshield();
display_Spotion();
display_DSpotion();
cout<<show_level();
cin.get();
return 0;
}
Last edited by BloodlustShaman : December 13th, 2005 at 09:58 PM. Reason: Forgot to put post I have |
|
#5
|
||||
|
||||
|
Quote:
LOL man dont worry about it, and like I said in the other post about our skills. I am working on similar things as you. I started working on multiFile programs too. The only difference is, is that I started with like five different .h files so I really bombed so now Im staying realistic and starting one file at a time like your doing right now But I see what you are trying to accomplish and I know this may not help but let me write something and see if it will help you at all. I'm about the same skill lvl as you but I think that doesn't really matter much its really just sometimes stepping back and thinking about something for a minute so you can understand it better. so Ill get back to you in a bit with a resolution. At least my way.NOTE: I think in the int show_level() you need to have it return an int to that void in order to calculate it properly. Example int show_level(int points) then in your other void were you determine if the person gets points you send that amount back to that int show_level(int points) function so it can calculate it. Because other wise it is just an empty function that calculates to 0 everytime. Because that is what you assigned the variable in the first place Keep trying though man, you'll get it. Keep refering to your examples and make sure you are not using things out of place. like the cout<<show_level(); You dont really need the cout statement, just the show_level(); that is all. just the show_level(); will automatically send the user to that function and show its contents, that is if you have any cout statements in that void =). But let me get back to you in a while(time >= tomorrow) =) you know me he he |
|
#6
|
||||
|
||||
|
Ok here is my program for you it is very simple and I hope this will help you a little bit. I can also attach it if you would like so you can examine the files. Notice the change to the show_level() at the bottom its key to what you are trying to accomplish I believe!!!
Code:
//main file
#include <cstdlib>
#include <iostream>
#include "show_level.h"
using namespace std;
int main()
{
int p = 0;//integer to be returned to show_level function
int q1 = 0;//q1 -q5 are for the answer to the questions...
int q2 = 0;
int q3 = 0;
int q4 = 0;
int q5 = 0;
cout<<"Who is the coolest guy on campus quiz\n"
<<"I will ask you a series of 5 questions to determine how\n"
<<"cool you are\n"
<<"----------------------------------------------------------\n"
<<"1] Question\n"
<<"Who was the first president?\n"
<<"1] George Washington\n"
<<"2] Tohmas Jefferson\n"
<<"3] Michael Jordan\n"
<<"----------------------------------------------------------\n";
cin>>q1;
if(q1 == 1)
{
cout<<"That is correct that is one point added to your rep\n";
p += 1;//adds one to reputation
}
else
{
cout<<"Sorry wrong answer\n";
}
cout<<"----------------------------------------------------------\n"
<<"2] Question\n"
<<"Who shot JFK?\n"
<<"1] James Earl Raye\n"
<<"2] Tohmas Jefferson\n"
<<"3] Michael Jordan\n"
<<"----------------------------------------------------------\n";
cin>>q2;
if(q2 == 1)
{
cout<<"That is correct that is one point added to your rep\n";
p += 1;//adds one to reputation
}
else
{
cout<<"Sorry wrong answer\n";
}
cout<<"-----------------------------------------------------------\n";
show_level(p);//sends user to the show_level(int points) or the .h file, remember the p basically stands for the int points for that function so every instance of points in that function is basically a reference to p :thumbs:
system("PAUSE");
return EXIT_SUCCESS;
}
Now here is the .h file that determines the points Code:
//.h file with the show_level(int points)
using namespace std;
int show_level(int points)
{
cout<<"Time to determine how cool you are \n";
cout<<"-------------------------------------\n";
if(points == 0)//points really stands for p in the main or which ever function you get it from =)
{
cout<<"Man your a loser\n";
}
else if(points == 1)
{
cout<<"Your pathetic\n";
}
else if(points == 2)
{
cout<<"Well you could be cooler\n";
}
else if(points == 3)
{
cout<<"Average guy\n";
}
else if(points == 4)
{
cout<<"You are above the rest\n";
}
else
{
cout<<"YOUR THE MAN\n";
}
}
there you go bloodlust, I know this is by far not as complex as what you are doing but hopefully it shows the simplicity of how to send an integer back to the function to calculate it. I did not finish all five questions so you'll never get above you could be better, but you could finish this very easy or just improve what you got. ![]() NOTE: I would change those for statements back to if statements if I were you. They would be functionally better in my opinion. Hope this helps you a little bit man... Last edited by Geo.Garnett : December 14th, 2005 at 02:38 PM. |
|
#7
|
||||
|
||||
|
BLS,
In this piece of code: Code:
int level=0;
// too much errors with for statements
if (experience == 0)
{int level = 1;}
if (experience == 1)
{int level = 2;}
if (experience > 1 && experience <=3)
{int level = 3;}
etc.
You need to remove all the int parts starting at int level = 1;. Every opening curly brace { opens a new scope. The int level declaration in every if block is therefore a new variable which is masking the level variable declared in the beginning of showlevel ( int level = 0; ). Every if block will assign a value to their 'local' level variable but not to the level variable in the enclosing scope (showlevel scope), so you never assign any new value to level and it always returns what you assigned it in the beginning. |
|
#8
|
|||
|
|||
|
Quote:
WOW VERY HAPPY MAN I AM NICE PEICE OF CODE THAT WAS IT TO SHOW ME HOW TO DO IT. and that is exactly what i was asking remember in my other post: I wanted help on how to do this and not code to get the output right Now I can proceed on going with this code cause last night was hell for me trying to fix it. Also thanks Icon for that tip. Happy as hell baby ![]() |
|
#9
|
||||
|
||||
|
Well Im glad I could be of assistance BLS.
![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > A random question |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|