I cant get functions to work!

This is a discussion on I cant get functions to work! within the C++ Programming forums, part of the General Programming Boards category; I've made a simple console game just for fun, but I cant get random() or mult() to work. Heres the ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    10

    I cant get functions to work!

    I've made a simple console game just for fun, but I cant get random() or mult() to work.

    Heres the code how it would work:

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <iostream>
    #include <fstream>
    int main()
    {
      int input;
      cout<<"--Main Menu--"<<endl;
      cout<<"1. Play Game"<<endl;
      cout<<"2. Exit"<<endl;
      cout<<"Please enter your option: ";
      cin>>input;
      switch (input)
      {
        case 1:
          int input2;
          int x;
          int y;
          int z;
          x=1;
          y=2;
          z=2;
          cout<<"What is "<<x<<"x"<<y<<endl;
          cin>>input2;
          switch (input2)
          {
          case 2:
            cout<<"Congratulations you have won!"<<endl;
            return 0;
          default:
            cout<<"Sorry, you have lost"<<endl;
            return 0;
          }
        case 2:
          return 0;
        default:
          cout<<"Error! Exiting Game!"<<endl;
          return 0;
      }
      return 0;
    }
    But i want it to look like this:

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <iostream>
    #include <fstream>
    int main()
    {
      int input;
      cout<<"--Main Menu--"<<endl;
      cout<<"1. Play Game"<<endl;
      cout<<"2. Exit"<<endl;
      cout<<"Please enter your option: ";
      cin>>input;
      switch (input)
      {
        case 1:
          int input2;
          int x;
          int y;
          int z;
          x=random(10);
          y=random(10);
          z=mult(int x, int y);
          cout<<"What is "<<x<<"x"<<y<<endl;
          cin>>input2;
          switch (input2)
          {
          case z:
            cout<<"Congratulations you have won!"<<endl;
            return 0;
          default:
            cout<<"Sorry, you have lost"<<endl;
            return 0;
          }
        case 2:
          return 0;
        default:
          cout<<"Error! Exiting Game!"<<endl;
          return 0;
      }
      return 0;
    }
    Note that int x and y are random, and z is the product of x and y.

    Can anyone help me? Im a beginner by the way

  2. #2
    Registered User
    Join Date
    Dec 2003
    Posts
    167
    I don't know of a random function but i know there's rand. I've never heard of mult though. What does that function do?
    silk.odyssey

  3. #3
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57
    Here Dan try this


    Code:
    #include <iostream>
    #include <time.h>
    #include <stdlib.h>
    using namespace std;
    
    unsigned mult (int x, int y);
    
    int main()
    {
      int input;
      cout<<"--Main Menu--"<<endl;
      cout<<"1. Play Game"<<endl;
      cout<<"2. Exit"<<endl;
      cout<<"Please enter your option: ";
      cin>>input;
      switch (input)
      {
        case 1:
          int input2;
          int x;
          int y;
          int z;
    	  srand (time (NULL));			
    	  
    	  x = (rand () % 10) + 1; // makes random # between 1 and 10
    	  y = (rand () % 10) + 1;
          z = mult (x, y);
          cout<<"What is "<<x<<"x"<<y<<endl;
          cin>>input2;
                      if (input2 == z)
    	  {
    		  cout << "Concratulations you won!";
    	  }
    	  else
    	  {
    		  cout << "Sorry, you lost!" << endl;
    	  }
    	  break;
    	  return 0;
        case 2:
          return 0;
        default:
          cout<<"Error! Exiting Game!"<<endl;
          return 0;
      }
      return 0;
    }
    
    unsigned int mult (int x, int y)
    {
    	return x * y;
    }
    I edited alot but now it works fine.
    If you want an explaination about what I did just PM me or email me at : ryan_nielson@hotmail.com
    Learning C++
    Programmer in training

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Code:
    z=mult(int x, int y);
    This is incorrect, you don't specify variable type in function calls. It should be this:
    Code:
    z=mult(x,y);
    If you want an explaination about what I did just PM me or email me at : ryan_nielson@hotmail.com
    Please don't do this. By taking the discussion out of the public forum, you are making it harder for everyone else to learn. People often search through old threads to get help, so it's always good to fully resolve the discussion where everyone can see it.

    EDIT:
    For information about random numbers, read the FAQ.
    Last edited by bennyandthejets; 06-13-2004 at 06:57 AM.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57
    Sorry about that Benny,
    I just thought it would be easier to explain to him over MSN messenger instead of typing in forums.
    Learning C++
    Programmer in training

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    That's ok, everyone has to learn sometime.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions are Still Not Understood.
    By errigour in forum C Programming
    Replies: 6
    Last Post: 04-09-2009, 02:54 PM
  2. Replies: 2
    Last Post: 03-05-2009, 09:25 AM
  3. Trying to do basic math functions. Why doesn't this work?
    By crazychile in forum C Programming
    Replies: 5
    Last Post: 10-25-2008, 05:14 PM
  4. How properly inherit from template?
    By 6tr6tr in forum C++ Programming
    Replies: 118
    Last Post: 04-25-2008, 04:30 AM
  5. Replies: 6
    Last Post: 05-06-2003, 03:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21