I want to know if this code is close to being correct?

Pages: 12
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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "stdafx.h"
#include <conio.h>
#include <iomanip>
#include <iostream>
#include <string>


using namespace std;

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;
}

	



int main()
{
	
	string stop;
	int a,b,c;
	cout << endl;
	cout << endl;
	do 
	{
		cout << number1(a);
		cout << number2(b);
		cout << number3(c);

		cout << "Exit?: ";
		cin >> stop;
cout << final;

	}while (stop == "y" || stop == "Y" );
	_getch();
	
}
void calculate(int x, int y, int z)
{
	final = (x+y+z/3);
}


the errors i keep getting is that number 1,number2 and number3 must return a value but i thought asking for the input was returning a value?
Last edited on
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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.
Last edited on
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*
Last edited on
Fyah wrote:
we havent gone over the scope statement
It is not a statement, it is a concept that should have been taught before learning functions or even loops.

Can you post what your assignment is? I think there is a misunderstanding here.
Last edited on
closed account (3CXz8vqX)
From the looks of things...you don't need a void type since you expect it to 'return' something.

Ps. From the looks of things everything you're doing can be done without conio.h

But yes, everything that has a type other than void must 'return' something otherwise you get a bunch of errors.

Just wait until you get to void pointers...hehe
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.
The functions as this

1
2
3
4
5
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.
Ravenshade wrote:
Just wait until you get to void pointers...hehe
Hopefully this is the last thing anyone ever learns :)
so how exactly could i get the function to make sense? and do the calculation im trying to achieve. dude this function business has worn me out.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "stdafx.h"
#include <iostream>
#include <string>


using namespace 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.
Last edited on
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?
std::string::clear() takes no parameters and returns nothing, but it does something useful.
closed account (Dy7SLyTq)
and heres another:
1
2
3
4
5
//sorry for the messy code
void intro()
{
cout<<"A long time ago ... ";
}
> std::string::clear() takes no parameters
┬┐you sure?
void clear(string *const this);
closed account (Dy7SLyTq)
yes
http://www.cplusplus.com/reference/string/string/clear/
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
closed account (Dy7SLyTq)
is there a page that explains that? i didnt look at your example well. i didnt think of classes in that way and would like to learn more
but why write

1
2
3
4
void intro()
{
cout << " a long time ago....";
}


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?
Pages: 12