Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 transferring to different functions
Index -> Programming, C++ -> C++ Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
signal_1001




PostPosted: Wed May 26, 2004 8:07 pm   Post subject: transferring to different functions

i'm making a SIMPLE game of wheel of fortune because im not very good and i was trying to transfer to different functions in order to have just one for spinning the wheel and buying a vowel and solving the puzzle. but it's not carrying the declared variables and stored information. is there anyway i can transfer it without copying the same code over and over?
code:
bool guessed=false;
        while(guessed==false)
        {
cout<<"\n"<<blankPhrase<<endl;
                cout<<"\n\n\tWould you like to a) spin the wheel\n";
                cout<<"\t                  b) buy a vowel\n";
                cout<<"\t                  c) solve the puzzle\n";
                char option;
                cin>>option;                                                  cin.ignore(10,'\n');
                switch (toupper (option)) //giving options
                {
                case 'A':
                        spin();
                        break;
                case 'B': //next option
                        vowel();
                        break;
                case 'C': //last option
                        answer();
                        break;
                default:
                        bred();
                        cout<<"\tInvalid choice, please enter a, b, or c\n";
                        pause();
                }
}

void spin()
{
}

any ideas???
Sponsor
Sponsor
Sponsor
sponsor
Mazer




PostPosted: Wed May 26, 2004 8:31 pm   Post subject: (No subject)

From what you're saying, you'll either need to make the variables global or pass them to your functions as parameters.
Andy




PostPosted: Wed May 26, 2004 8:33 pm   Post subject: (No subject)

not sure what ur asking, but i think what your trying to say ask is how to use variables in functions. you could either declare your variables local or passes them through parameters...
guruguru




PostPosted: Thu May 27, 2004 3:45 pm   Post subject: (No subject)

You have declared option in the while loop. This means that its scope is only for the while loop. YOu may have other variables declared that way, for I don't know the rest of the program. The procedures cant access local loop variables. I know option isnt important to the function, but you may have made the same mistake with other variables. Otherwise, the procedures can access the global variables (exceptions occur but its a SIMPLE game :p).
Andy




PostPosted: Thu May 27, 2004 6:24 pm   Post subject: (No subject)

guruguru wrote:
exceptions occur
no not really... since well there is no such thing as a procedure and the point of a global variable is so to allow it to be accessed by different functions
wtd




PostPosted: Thu May 27, 2004 6:56 pm   Post subject: (No subject)

Asking the question then getting a correct answer, and then doing something based on that answer should be in separate functions.

code:
char ask_question(std::ostream& out = std::cout, std::istream& in = std::cin)
{
   char option = '\0';
   do
   {
      out << "Would you like to:" <<std::endl
          << "\ta)\tSpin the wheel? << std::endl
          << "\tb)\tBuy a vowel?" << std::endl
          << "\tc)\tSolve the puzze?" << std::endl;
      in >> option;
      option = toupper(option);
   } while (option != 'A' && option != 'B' && option != 'C');
   return option;
}

void do_action(char option_selected std::ostream& out = std::cout)
{
   switch (option_selected)
   {
      case 'A':
         spin_the_wheel();
         break;
      case 'B':
         buy_a_vowel();
         break;
      case 'C':
         solve_the_puzzle();
         break;
      default:
         out << "Bad option!" << std::endl;
         break;
   }
}
guruguru




PostPosted: Thu May 27, 2004 6:57 pm   Post subject: (No subject)

Umm... a procedure is another way of saying function. Some people call them different things... live with it Evil or Very Mad . They are called many things... functions one of the main ones, but I refer to different types of 'functions' differently, big deal. And as for your second point, that was implied and was what I basically said. And yes depending on your definition of global exceptions due occur.
wtd




PostPosted: Thu May 27, 2004 7:04 pm   Post subject: (No subject)

The difference between procedures and functions exists in at least the vast majority of programming languages. However, C and C++ do not enforce that difference.

The difference is that a function does not alter the state of the machine, but returns a new value.

code:
int times_two(int input)
{
   int new_value = input * 2;
   return new_value;
}


A procedure, however, may alter the state of the machine (in the form of variables), but it cannot return a value.

Eiffel is among the few languages which enforce the difference between procedures and functions. Together they are known as "routines".
Sponsor
Sponsor
Sponsor
sponsor
guruguru




PostPosted: Thu May 27, 2004 7:07 pm   Post subject: (No subject)

Thank you. My point exactly.
Andy




PostPosted: Thu May 27, 2004 7:56 pm   Post subject: (No subject)

wtf you talking about??? he just said they are different... and go ahead and give me your definition of global
guruguru




PostPosted: Thu May 27, 2004 8:13 pm   Post subject: (No subject)

Myself:
Quote:
I refer to different types of 'functions' differently


wtd:
Quote:
The difference is that a function does not alter the state of the machine, but returns a new value.

A procedure, however, may alter the state of the machine (in the form of variables), but it cannot return a value.


I refer to something that returns a value as 'functions', and other as 'procedures'. What wtd said!

I rest my case.

And for global: There can be variables in different files that are global, maybe you call variables in a class a global variable for the class... it depends a lot on what the user defines it as. Not that I agree with all of these, but people can refer to global as many things.
wtd




PostPosted: Thu May 27, 2004 8:50 pm   Post subject: (No subject)

Perhaps the best definition of a global variable is: "a variable which exists independent of any other context."

If for instance, I write a simple Hello, world! in C++:

code:
#include <string>
#include <iostream>

std::string hello_world = "Hello, world!";

int main()
{
   std::cout << hello_world << std::endl;

   return 0;
}


Then hello_wrld exists no matter what else I include in the program, though it may be superseded by a local variable by the same name.

If, however, I put the declaration and initialization of hello_world inside main, then that variable would only exist in the context of any given call to main.

Similarly, if I include the declaration for hello_world as a member of a struct or class (really the same thing in C++) then it only exists in the context of that class (or an instance of that class).

If I include it in a namespace, then it only exists in the context of that namespace.
guruguru




PostPosted: Thu May 27, 2004 8:54 pm   Post subject: (No subject)

Good definition! I assume you're saying that something in a class or main is local.? Or are you tryng to say its global?!?! When I was talking about exceptions, I was glancing at namespaces. Anyways...
wtd




PostPosted: Thu May 27, 2004 9:04 pm   Post subject: (No subject)

A variable declared in a function is local to that function. It has no meaning (or existence) outside of that function.

A "variable" declared in a class declaration isn't really a variable at all. It's more like a mold (seeing as how "template" has it's own meaning in C++) for a variable, and the entire class declaration is merely a blueprint for an object of that class.

In very few programming languages are classes actually "things" in and of themselves, and those tend to be the more dynamic languages, along the lines of Python, Ruby, or Dylan.
guruguru




PostPosted: Thu May 27, 2004 9:10 pm   Post subject: (No subject)

Ok. I agree.

Quote:
In very few programming languages are classes actually "things" in and of themselves


What do you catagorize classes as in C++? A blueprint? It can also be seen as a thing that contains other things can it not be? Because theoretically, a blueprint by itself would do nothing- you have to activate one of its components. But a class can activate its components automatically at creation and destruction. So it's not quite a blueprint and more of a 'designer' that mainly lays out plans but can also implement.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 23 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: