so i have asked for help with understanding the void type now i have to create a code that calculates the grade point average of 3 numbers entered. i have to use 4 functiions and 1 has to be a void. the day i learned functions was the day i learned void so im trying to work at understanding them completely. I keep stressing this out, i am a beginner, and i recently saw that we should never use <conio.h>, but i use it all the time and the professor says its no problem, but i guess thats because i am using it on a homework assignment. anyways im posting this code and i want to know why it gives me this error?
void calculate ();
int number1 (int x)
{
cout << "Please enter the first grade : ";
cin >> x;
}
int number2 (int y)
{
cout << "Please enter the second grade : ";
cin >> y;
}
int number3 (int z)
{
cout << "Please enter the third grade : ";
cin >> z;
}
You have declared that "calculate" does not return anything and that "number1", "number2", and "number3" return an integer.
Hint: You need to use the return statement.
Also, the design of this program is questionable, if anything. All three of your "numberX" functions do the same thing, so they should be the same function. Also, you're giving the parameters different names in each function, which leads me to believe you don't understand how function scope works.
Also, the only time you may use conio.h is if your professor requires it. If it is optional and simply allowed, you should avoid it - the point is to learn programming properly. Many professors have not stayed updated with the times and are teaching really old programming from years ago - things have changed a lot. Just be sure to respect your professor even if you know what he is teaching is wrong - clearly he doesn't care if he won't bother to stay up to date with programming.
well we havent gone over the scope statement but since my teacher required us to use 4 different functions and 1 being a return type i thought that this is how i had to do it. it says that 'a' is being used without being initialized but because im calling the number1 function and there is a 'cin' i thought that would have initialized it? i guess my teacher didnt explain functions in a way that was easy for me to understand because i could write this code easily without using functions, but i have to use functions. *sad face*
I didnt get a paper he just said that we have to create a code that calculates the grade point average of 3 grades entered. example (this is his source code):
this program calculates the grade point average of 3 grades
Please enter the first grade : 95 (user input)
Please enter the second grade : 88 (user input)
Please enter the third grade : 79 (user input)
the grade point average of these 3 grades is : 87
would you like to continue? (y/n):
****thats it, thats his program, but he said we have to create that same program using 4 different functions and one of the functions has to be a void type. so i wrote MY code the way i did thinking thats how it is done. like i was saying in my last post, i put the return in the functions i created but now it says that 'a' is being used without being initialized but because im calling the number1 function and there is a 'cin'. i thought that would have initialized it? i guess my teacher didnt explain functions in a way that was easy for me to understand because i could write this code easily without using functions, but i have to use functions. *sad face* thats why im trying to grasp using functions because obviously they are very important.
int number1 (int x)
{
cout << "Please enter the first grade : ";
cin >> x;
}
are invalid and have no sense. First of all they do not use the value of the parameter. Secondly they return nothing though they have return type int that is they shall return an objecy of type int.
#include "stdafx.h"
#include <iostream>
#include <string>
usingnamespace std;
int number1()
{
int x = 0;
cout << "Please enter the first grade : ";
cin >> x;
return x;
}
int number2()
{
int x = 0;
cout << "Please enter the second grade : ";
cin >> x;
return x;
}
int number3()
{
int x = 0;
cout << "Please enter the third grade : ";
cin >> x;
return x;
}
int calculate(int x, int y, int z)
{
return ( x + y + z / 3 );
}
int main()
{
string stop;
do
{
int a, b, c;
cout << endl;
a = number1();
b = number2();
c = number3();
cout << calculate( a, b, c ) << endl;
cout << "Exit?: ";
cin >> stop;
}while (stop != "y" || stop != "Y" );
retrun 0;
}
Run a console application in MS VC++ by pressing Ctrl + F5.
ooooo.....so the variables declared in the main function ALWAYS call the function. for example
a= number1();. because what i thought was that the variables declared in the main had to be put inside the functions parenthesis to call the function.example: number1(a); (i hope that makes sense).
dont mean to be a bother but i when you see
void number(); //what exactly is the point of using a function that does not receive any parameters, especially a void type function?
It is a non-static member function.
It requires a parameter (an object of that class) that it's passed implicit
so if you do `foo.clear()' then `foo' is a parameter
if you are just going to void it. if possible i would like to see an example of a void function that does something but gives no return like a math operation or something to that effect, but even if you were to void the math operation how would you see the result of it if it voided?