5 Replies - 1088 Views - Last Post: 15 June 2012 - 01:10 PM Rate Topic: -----

#1 Sirr_Purr_the_Cat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 14-June 12

Visual Basic C++ Code - Welcome screen and Menu output issues

Posted 14 June 2012 - 08:20 PM

Hey guys! New to the programming/computer science field and I really like what I've learned from this website so far, but google only gets me so far. With this code I think you can get the basic idea, a menu comes up and prompts input selection that leads to another "screen" with the menu options still available. My problem is my instructor has specifically asked for the welcomeScreen to appear only once at the very begining and when I run it as the code is written below, for some reason or another, it simply shows myMenu. If anyone could please explain to me what I'm doing wrong that would be great.

Now with that said I think what I really want to know is if the way I am writing this code is efficient and correct. I am more used to using Eclipse for Java so I had the hardest time simply getting myMenu to work correctly and appear with all the input options but I worry that my code is flawed since I am such an inexperienced C++ writer.

#include <iostream>
using namespace std;	

void welcomeScreen()
{
	cout << "===============================================" << endl;
	cout << ""<< endl;
	cout << "Welcome to My Bank ATM" << endl;
	cout << "" << endl;
	cout << "===============================================" << endl;
	cout << "Can I help you today?"<< endl;
	cout << "" << endl;
	system ("pause");

}

char myMenu()
{
	cout << "Enter # 1 for Deposits" << endl;
	cout << "Enter # 2 for Withdrawals" << endl;
	cout << "Enter # 3 for Transfers" << endl;
	cout << "Enter # 4 to View Balance" << endl;
	cout << "Enter # 5 to Exit" << endl;
	cout << "" << endl;
	cout << "Enter a number to continue" << endl;

	int xin;;
	cin >> xin;
	system("CLS");

	if (xin == 1)
	{
		cout << "Entering Deposits section\n" << endl;
		return myMenu();
	}

	else if (xin == 2)
	{
		cout << "Entering Withdrawals section\n" << endl;
		return myMenu();
	}

	else if (xin == 3)
	{
		cout << "Entering Transfers section\n" << endl;
		return myMenu();
	}

	else if (xin == 4)
	{
		cout<< "Entering Balance section\n" << endl;
		return myMenu();
	}

	else if (xin == 5)
	{
		cout << "Exiting from the program\n" << endl;
		return myMenu();
	}

	else
	{
		cout << "Invalid Entry!\n" << endl;
		return myMenu();
	}	
	
	system ("pause");
}


int main()
{
	myMenu();
	welcomeScreen();
	return 0;
}



I truly hope someone will be able to help me. I look forward to working with you guys and eventually throwing back my own information to help others!

Sorry for the mistake in my [code] tag...I don't know how to edit posts unfortunately :/

This post has been edited by jimblumberg: 14 June 2012 - 08:38 PM
Reason for edit:: Fixed Code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Visual Basic C++ Code - Welcome screen and Menu output issues

#2 jimblumberg  Icon User is offline

  • member icon

Reputation: 3112
  • View blog
  • Posts: 9,483
  • Joined: 25-December 09

Re: Visual Basic C++ Code - Welcome screen and Menu output issues

Posted 14 June 2012 - 08:44 PM

In main(), where do you call welcomeScreen()? Is it before or after your call to your menu function? Also the way your menu function is designed is not ideal. Instead of recursively calling your menu function you should read up on the different types of loops. See this link: Control structures and look into using the while or do/while loop constructs.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Sirr_Purr_the_Cat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 14-June 12

Re: Visual Basic C++ Code - Welcome screen and Menu output issues

Posted 14 June 2012 - 09:49 PM

Thanks for fixing my post and for the reply Jim.

Pardon my question but even if I use a while loop wouldn't I have to still call myMenu every time an input is put in?
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon

Reputation: 3112
  • View blog
  • Posts: 9,483
  • Joined: 25-December 09

Re: Visual Basic C++ Code - Welcome screen and Menu output issues

Posted 15 June 2012 - 03:57 AM

Not if you setup you loop correctly. A loop was designed for exactly what you are doing with your recursive calls, without the overhead of the function call.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 starrim  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 21
  • Joined: 08-May 12

Re: Visual Basic C++ Code - Welcome screen and Menu output issues

Posted 15 June 2012 - 12:52 PM

I have a question, you used

Return myMenu()

in the function myMenu,

I don't know how it works, return a function in itself?Won't that be an endless loop? And what's your purpose by this?
Was This Post Helpful? 0
  • +
  • -

#6 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 270
  • View blog
  • Posts: 3,880
  • Joined: 08-January 12

Re: Visual Basic C++ Code - Welcome screen and Menu output issues

Posted 15 June 2012 - 01:10 PM

Actually, a function can return itself. It is called recursion. But as you correctly pointed out, in this case it will lead to an infinite loop.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1