2 Replies - 766 Views - Last Post: 30 October 2010 - 06:42 AM Rate Topic: -----

#1 frankilische   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-October 10

calling functions....

Posted 30 October 2010 - 05:38 AM

Please i am new to C++ and need to understand basics....i have got a major assignment that would bee needing handing in by next week and still having problems of calling functions......this example below is something i just thought of out of the blues to use as an example to get explanations on how to call functions and also to avoid using the goto in coding as i hear that is bad practice........

can someone please put me through on calling functions and avoiding using goto in d example below:

#include <iostream>
#include <string>

using namespace std;


float psquare (float a)
{
    return (4*a);
}

float prectangle (float w, float l)
{
    return ((2*w)+(2*l));
}

int main()
{
    float d,a,w,l;
    char n;
    cout << "Please choose from options below:\t";
    cout << "1. Square\n";
    cout << "2. Rectangle\n";
    cin >> d;
    if ( d == 1 )
    {
     	cout << "length of side:\t";
	cin >> a;
	{return psquare;}
	goto end;
    }
    else if ( d == 2 )
    {
     	cout << "length of side:\t";
	cin >> w,l;
	{return prectangle;}
	goto end;
    }
    else
    {
     	cout << "invalid input, Please try again\n\n";
	return main ();
    }

  end:
    cout <<"Do you want to calculate an other shape? (y / n):";
    cin >>n;
    if (n == 'y')
    { return main();}
    system("pause");
    return 0;
}




please i need help as i am really confused....please......

MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 30 October 2010 - 05:46 AM
Reason for edit:: Added code tags & formatted code via indentation


Is This A Good Question/Topic? 0
  • +

Replies To: calling functions....

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: calling functions....

Posted 30 October 2010 - 05:47 AM

Read this on Functions

Also, WHY are you using goto? There are very few legitimate needs for goto, and this certainly isn't one of them.
You want to wrap your code in a LOOP.

This post has been edited by JackOfAllTrades: 30 October 2010 - 05:50 AM
Reason for edit:: Thought you WANTED to use goto. Thankfully you don't

Was This Post Helpful? 0
  • +
  • -

#3 geofbot   User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 80
  • Joined: 26-September 10

Re: calling functions....

Posted 30 October 2010 - 06:42 AM

Instead of goto, you could make a "go back" function. THat way, instead of always using goto, use could just call a function that would return the choice. Also, a good idea would be to put your menu (Square/Rectangle) into a do-while loop.

Also, NEVER USE SYSTEM("PAUSE"). It is not portable and is slow because you have to go into the operating system and whatever. Use cin.get(), and if there is already something in the stream, use cin.ignore() and then cin.get().

This post has been edited by geofbot: 30 October 2010 - 06:44 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1