Jump to content

can't get a method to work

- - - - -

  • Please log in to reply
2 replies to this topic

#1
justanothernoob

justanothernoob

    Newbie

  • Members
  • PipPip
  • 22 posts
Hi, I program in c# and i'm trying to learn c++. I can't seem to get a method to work right.

Here is what I have:
// more than one returning value

#include <iostream>

#include <time.h>

#include <string>


using namespace std;

 

int money = 1000;

string playername;

int currentmenu;

int choice;


void GetName ()

{ 

  cout << "What is your name?\n";

  getline(cin, playername);

}

void DisplayMainMenu ()

 { 


   cout << "1.Buy\n2.Sell\n3.Item Shop\n";

   cin >> choice;

   if (choice == 1)

   {

   DisplayBuyMenu();

   }

 }

void DisplayBuyMenu ()

{

  cout << "1. Item 1\n2. Item 2\n"; 

}

int main ()

{

 GetName();

 DisplayMainMenu();

 

  return 0;

}




The compiler tells me that DisplayBuyMenu() is not declared in this scope. Does anyone know what I am doing wrong? thanks.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
You need to move DisplayBuyMenu above DisplayMainMenu.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Because you're calling DisplayBuyMenu before actually defining it, the compiler doesn't know about it and throws an error. You can put entire function above DisplayMainMenu, or just the header, so the compiler will know about it.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users