#Include and functions

Hi,

I'm very new to C++, i've just began learning a few days back and wrote 4-5 programs which are just very simple programs. This is my 5th program, it works fine but I have a few questions hopefully someone can help me with.

1. In the following program I would like to make it so that instead of the program shutting down by inputting a negative number, it shuts down when I press the "esc" key on my keyboard, but continues when I press "enter".

2. Make the functions calculate1 and calculate2, in to a seperate file so that I can use it with the #include command with my other programs.

Well here's the source code;

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Modulo - This program calculates the first
//          integer entered by the user modulo
//          the second integer entered by the
//          user

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int nVar1;
int nVar2;
int nVar;
int nVar3;

// Define the function calculate2
int calculate2(int, int)
{
nVar3 = nVar1/nVar2;

// calculate the modulus

nVar = nVar1 - nVar2 * nVar3;


cout << nVar;

}

// Define the function calculate1
int calculate1(int, int)
{
nVar3 = nVar1/nVar2-1;

// calculate the modulus

nVar = nVar1 - nVar2 * nVar3;

cout << nVar;

}

int main(int nNumberofArgs, char* pszArgs[])
{
cout << "This program calculates mod of an integer\n"; 
cout << "and another integer entered by the user\n\n";
     
// loop forever

for (;;)
{
int value = 0;
cout << "Enter a positive integer to continue\n";
cout << "or a negative integer to terminate program: ";
cin >> value;

// if it's negative
if (value < 0)
{
// then exit
break;
}
else
{

// Ask for two numbers

cout << "Enter integer1:";
cin  >> nVar1;

cout << "Enter integer2:";
cin  >> nVar2;

if (nVar2 < 0)
{
cout << "Error, no negative integers\n\n";
continue;
}

if (nVar2 > 0)
{

if (nVar1 < 0)
{
calculate1(nVar1, nVar2);
cout << "\n";
}
else
{



calculate2(nVar1, nVar2);
cout << "\n";
}
}

else
{
cout << 0;
cout << "\n";
}





}

}   
cin.ignore();
return 0;
}




Thanks
Krahl
Last edited on
At the moment, you are using global variables, which should be avoided at all cost. Because you define them to be global, your function input is simply ignored (because they are undefined, you have no way to refer to them).
Personally, I'd change this to:
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <conio.h> // This is for the key pressing function getch()
using namespace std;

// Define the function calculate2
void calculate2(int x, int y) // Since we dont RETURN an int or anything else, this function returns "void"
{
int nVar3 = x/y;

// calculate the modulus

int nVar = x - y * nVar3;


cout << nVar;

}

// Define the function calculate1
void calculate1(int x, int y) // Since we dont RETURN an int or anything else, this function returns "void"
{
int nVar3 = x/y-1;

// calculate the modulus

int nVar = x - y * nVar3;

cout << nVar;

}

int main(int nNumberofArgs, char* pszArgs[])
{

int nVar1;
int nVar2;

cout << "This program calculates mod of an integer\n"; 
cout << "and another integer entered by the user\n\n";
     
// loop forever

for (;;)
{
if (getch()==27) // if the escape key is pressed
{
// then exit
break;
}
else
{

// Ask for two numbers

cout << "Enter integer1:";
cin  >> nVar1;

cout << "Enter integer2:";
cin  >> nVar2;

if (nVar2 < 0)
{
cout << "Error, no negative integers\n\n";
continue;
}

if (nVar2 > 0)
{

if (nVar1 < 0)
{
calculate1(nVar1, nVar2);
cout << "\n";
}
else
{



calculate2(nVar1, nVar2);
cout << "\n";
}
}

else
{
cout << 0;
cout << "\n";
}





}

}   
cin.ignore();
return 0;
}

An even better alternative would be using the standard modulus operator. Which is %.
Last edited on
1. In the following program I would like to make it so that instead of the program shutting down by inputting a negative number, it shuts down when I press the "esc" key on my keyboard, but continues when I press "enter".

you can check the user's input.if it is enter(\r) then continue and if it is escape (\x(hex code of esc character)) the exit the program.

2. Make the functions calculate1 and calculate2, in to a seperate file so that I can use it with the #include command with my other programs.

you 'd better learn how to make classes and encapsulation...what you want is the main purpose of classes.
here will help you learn that :
http://www.cplusplus.com/doc/tutorial/classes/
1. In the following program I would like to make it so that instead of the program shutting down by inputting a negative number, it shuts down when I press the "esc" key on my keyboard, but continues when I press "enter"


No reliable way to do this with standard libs alone. You'll need to venture into platform specific libraries to do that.

conio.h (as Kyon suggested) is nonstandard and may not exist on everyone's computer (or even on yours -- it's pretty ancient and it doesn't come with some newer compilers), so I don't recommend using it.

All input with the standard libs is line-by-line. So to receive any input from the user, the user must press enter. The closest you could get to having Escape exit the program would be to have the user press Escape, then Enter (this could be accomplished with something like what sourena suggested, but that solution is a little fugly, IMO).

Personally I wouldn't worry bother with this. Doing fancy things with the console typically isn't worth the effort, IMO. The console isn't really designed to be user friendly.

void calculate2(int x, int y) // Since we dont RETURN an int or anything else, this function returns "void"


This is pretty good advise, since you had a function that wasn't returning anything.

Although better advise would be to return something, and not output to cout.

The function's name is "calculate", not "calculate_and_print". So it should only calculate the number. Printing the result is (or should be) outside its scope.

What if you want to do more than one calculation with that number? What if you don't want to print the result, but what to just keep it for later use? Or what if you want to output it to a file instead of printing it?

Your function doesn't allow for any of that. The value is printed and then immediately forgotten by the program. Thereby limiting the function's usefullness to one very specific task.

To make reusable code, you generally want to keep the jobs of functions very minimalist. Have them to a very specific job and nothing more.

A better calculate function would look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
int calculate1(int nVar1, int nVar2)
{
    int nVar3 = nVar1/nVar2-1;

    // calculate the modulus

    int nVar = nVar1 - nVar2 * nVar3;

    return nVar;

    // note I never send the result to 'cout', I just calculate it and return it.
    //  let the calling funtion send it to cout if you want to print it.
}



2. Make the functions calculate1 and calculate2, in to a seperate file so that I can use it with the #include command with my other programs.


1
2
3
4
// calculate.h

int calculate1(int,int);
int calculate2(int,int);


1
2
3
4
5
6
7
// calculate.cpp

#include "calculate.h"

/*
   put function bodies for calculate1 and calculate2 here
*/


1
2
3
4
5
6
7
8
9
10
11
12
// main.cpp

#include <iostream>
#include "calculate.h"

int main()
{
    // now we can call it from another cpp file:
    std::cout << calculate1(1,2);

    return 0;
}



other notes

You should try to name your functions to be more clear. The function name should describe what the function does. This makes your code much easier to follow and reduces mistakes caused by calling the wrong function.

"calculate1" leaves a lot to be desired. What is it calcuating?

From the comments I see that it's calculating the modulus. So the name "CalcModulus" makes a bit more sense, doesn't it?
Thanks Disch, Sourena and Kyon for your time and your repies.

I knew about the "%" command being used for modulus but i wanted to calculate it myself, otherwise the program would be too simple for me to learn anything from it.

I will use all your advise in the future.
Last edited on
Topic archived. No new replies allowed.