Functions:beginner

Cant get my function "giveDays" to give me the remaining days using the modulus.

# include <iostream>
# include <cmath>
using namespace std;

void giveWeeks (int & days );
void giveDays (int & days);

int main ()
{
int inNum;



cout << "Please enter number of days:";

cin >> inNum;;

cout << inNum <<" days have: \n";

giveWeeks(inNum);

cout << inNum <<" weeks \n";

giveDays (inNum);

cout << inNum << " days remaining ";

system ("pause");
return 0;
}
void giveWeeks (int & days)
{



days = days / 7;
}
void giveDays (int & days)
{
days = days % 7;
}
Why would you not use a return value for your functions, rather than overwriting the input variable?
YOUR PROGRAM SHOULD BE IN [ CODE ] ... [ / CODE ] !!!!

ill do it for ya:

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
# include <iostream>
# include <cmath>
using namespace std;

void giveWeeks (int & days );
void giveDays (int & days);

int main ()
{
int inNum;



cout << "Please enter number of days:";

cin >> inNum;;

cout << inNum <<" days have: \n"; 

giveWeeks(inNum);

cout << inNum <<" weeks \n";

giveDays (inNum);

cout << inNum << " days remaining ";

system ("pause");
return 0;
}
void giveWeeks (int & days)
{ 



days = days / 7;
}
void giveDays (int & days)
{
days = days % 7;
}


AND WHAT THE HELL ???? To initialize a function you cant write just void function!!! ill give you an eg.

1
2
3
4
5
6
7
8
// THIS IS A FUNCTION
void DisplayTheArray(double anArray[], int Start, int End)
{
    for (int i=Start; i<=End; ++i)
    {
         cout<<"The "<<i+1<<"th element of the array is: "<<anArray[i]<<"\n";
    }
}



This function can be used in main program to display the array elements by simply writting :
DisplayTheArray(anArray[z]) //where z should be a number between Start and End

also, i think you need a more detailed example>>
something like this :

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
/*  
	Copiright © 2011 Jumper007TM All Rights Reserved 
*/

#include <iostream>

using namespace std;

// We initialize a fuction to display the array at any time by simply using : DisplayArray(anArray[i])
void DisplayArray(int anArray[10000], int Start)
{
	//This will generate the array from the point (anArray[Start]) to the point of (anArray[10000])	
	for (int i=Start; i<=10000; ++i)
	{
		cout<<"The "<<i+1<<"th place in Array is occupied by"<<anArray[i]<<";\n";
	}
}

// We initialize another function, but this one will read the element fo the array by using: ReadArray(anArray[i])
void ReadArray(int anArray[10000], int Start)
{
	//This will let you give values to the array from the starting point (anArray[Start]) to the point of (anArray[10000])
	for (int i=Start; i<=10000; ++i)
	{
		cin>>anArray[i];
	}
}

// Here starts the real program
int main()
{
	int anArray[10000];
	int i, Start;
	
	cout<<"Give the start point value of the array: ";
	cin>>Start;
	
	// Here we use our function to read the array
	ReadArray(anArray[10000]);
	
	// Here we print out to check if the function worked
	DisplayArray(anArray[10000]);
	
	return 0;
}

/*  
	Copiright © 2011 Jumper007TM All Rights Reserved 
*/


Last edited on
@jumper007:

You were taking issue with this?
1
2
void giveWeeks (int & days );
void giveDays (int & days);


It's called a function prototype and it's perfectly valid, as long as it'd defined elsewhere (which it is). I use these all the time in header files.

Also, your caps lock is hurting my eyes.

Also, also, you don't initialise functions, you define them.
Last edited on
And WOW, i cant see where you need <cmath> !!!!!


An eg. for using <cmath> would be :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int a,b,c;

    cin>>a>>b;

    c=sqrt(a;b);
    
    cout<<c;

    return 0;
}

Last edited on
yes, i apologise i didnt see that
1
2
3
4
5
6
7
8
void giveWeeks (int & days)
{ 
days = days / 7;
}
void giveDays (int & days)
{
days = days % 7;
}

but he/she still has mistakes !
PS: THIS FORUM IS FOR C++ NOT FOR ENGLISH (dont tell me is not initialise as i didnt ask you !)
PS2: IF YOU STILL TOLD ME ABOUT MY ENGLISH , NEVER USE Also, also

PS3: i hope that now your eyes wont hurt
Last edited on
I know I shouldn't feed the trolls, but I can't resist.

 
c = sqrt(a);

Fixed that for ya.
EDIT: on farther inspection, sqrt only takes one argument.

I don't have one eye. Do you have more than one caps lock key?
(NOTE: The comment which prompted this response has since been edited out.)

I don't work for a big company, but I do work in the engineering department for a local company writing code for embedded systems. So, do you work for Google/IBM/etc.?
(NOTE: This question was also edited out.)

EDIT:
I wasn't criticising your English, I was criticising your use of terminology (which is quite relevant to a C++ forum)... as well as making a point about how irritating pedants are. ;)
Last edited on
Getting back to the question at hand:

You're assigning a value to a variable through user input, and then over-writing that value with your first function. This is what's causing the unexpected behaviour when you call the second function.

(see my first comment about functions returning values)
Topic archived. No new replies allowed.