#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)
Calling another functioin?
Started by MinoriZ, Aug 24 2007 08:09 AM
11 replies to this topic
#1
Posted 24 August 2007 - 08:09 AM
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:
|
|
|
#2
Posted 24 August 2007 - 08:21 AM
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:
Let's say you want to do to print the message M to the user N times. Then the loop would look like this;
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
Posted 24 August 2007 - 11:34 AM
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
#4
Posted 24 August 2007 - 05:52 PM
Oh, okay I think I get how its working now. Thanks a bunch for your help both of you.
#5
Posted 24 August 2007 - 10:13 PM
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
Posted 25 August 2007 - 10:26 AM
Okay, so I did some research about the:
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.
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
Posted 25 August 2007 - 10:45 AM
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."
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 ... */ }
#9
Posted 25 August 2007 - 10:36 PM
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
Posted 27 August 2007 - 05:20 PM
although...main should be returning an int on windoz and linux, since you pass back basic errorcodes.
#11
Posted 28 August 2007 - 08:23 AM
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.
#12
Posted 30 August 2007 - 10:19 PM
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;
}
}


Sign In
Create Account

Back to top










