Jump to content

Calling another functioin?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

#1
MinoriZ

MinoriZ

    Newbie

  • Members
  • Pip
  • 4 posts
Okay basically i'm trying to make a simple console program to calculate and spit out my gpa and what not. Anyway, I want to know how to loop back to the top of the main function once I finish a process so I do not have to keep restarting the program in order to do another calculation. (And yes there is another "If" statement, but I think this bit of code should be enough for someone to come up with an answer, thanks:


#include "stdafx.h"

#include <iostream>


using std::cin;

using std::cout;


int main()

{

	int number1, number2, number3, number4, number5, number6, number7;

	int meh;

	int average;


	cout << "Please choose which function you would like for me to perform.\n1. Semester Grade (One class)\n2. Quarterly GPA\n";

	cin >> meh;


	if ( meh == 1 )

	{

	    cout << "\n" << "Please enter first quarter percentage: ";

		cin >> number1;


		cout << "Please enter second quartrer percentage: ";

		cin >> number2;


		average = ( number1 + number2 ) / 2;


		cout << "Your semester grade is a: " << average << "\n";

	}


(Other If statements and stuff continues after this point)



#2
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
There's several alternatives. You could use recursion, and call the main-function itself again, you could use goto-statements and labels, you could use a while-statement, etc. My suggestion is to use a while-statement.

The while-statement works in this way:
while ( expression )
    ... statement 1
    ... statement 2
    ...
    ... statement N
This is a pseudo-code, so do not try this yourself, it won't work. But it shows you how it works. The statements will be executed while the expression is true. So it actually only takes two values; false (0) and true (!false)

Let's say you want to do to print the message M to the user N times. Then the loop would look like this;
int N = 10;
const char *M = "Hello, World";

while(N >= 0)
{
    std::cout << M << std::endl;
    N--;
}
You need to decrement the value of N. If you don't, then the argument to the while-statement will never become false, because N will always be greater than 0; true. But when N reaches -1, then it's not greater than than zero anymore and the result will be; false.

#3
Victor

Victor

    Programmer

  • Members
  • PipPipPipPip
  • 116 posts
If you make your main function a void, you can just call it at the end of what you're doing with a simple
main();
I attached something I just threw together, exe and the source. Let me know what you think

Attached Files

  • Attached File  GPA.zip   4.35K   7 downloads


#4
MinoriZ

MinoriZ

    Newbie

  • Members
  • Pip
  • 4 posts
Oh, okay I think I get how its working now. Thanks a bunch for your help both of you.

#5
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
Victor, what you're talking about is recursion. But your example is really weird. Usually, you never make a prototype for the main-function, it's simply not needed by the compiler nor linker. Next, in your example, you're not even including your Main.hpp-file.

#6
MinoriZ

MinoriZ

    Newbie

  • Members
  • Pip
  • 4 posts
Okay, so I did some research about the:
void main()

And I found this Link that links to pages describing issues with declaring your main as a void. So I guess this isn't something I need to do. And I am slightly confused, about how while statements will help me with this. I'll try a few things and post some code or attach a file later today.

#7
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
Check this link as well. Written by the creator of C++ himself.

In what way are you confused? How it work, or...?
Please specify what you really want to do. If you just want to loop forever, over and over again, then you can just give it true as "argument."
while(true){ /* ... loop forever ... */ }


#8
Victor

Victor

    Programmer

  • Members
  • PipPipPipPip
  • 116 posts
My bad. My example I made thinking along the lines of how you helped me a few months ago with a calculator. (Thread here). I used advice from that thread to repeat the calculator again. But again, sorry, my bad.

#9
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
It's alright. You weren't completely lost. It's actually possible to use your idea. You can call the main-function as much as you want to, like you showed in your code - but you don't need the prototype for main, like I said before.

#10
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
although...main should be returning an int on windoz and linux, since you pass back basic errorcodes.

#11
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Recursively calling main is a potentially bad idea if you are declaring variables in it. It would be much better to have the logic wrapped in a while loop with an input to escape from the loop.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#12
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
You can, yes, call it recursivly, but you will see no commercial application EVER do this. A logic-loop with some input would be much better.
while(TRUE) {
  <Do whatever>
  if(cin.get() == 'q')
  {
      return 0;
  }
}