Integer division

I'm sure this is pretty standard, but I need to make a program that uses user input to perform a selected calculation. It has multiple files (this is the functions file, it also has a main file, which is definitely correct, and a header file). My errors are that I have multiple definitions of main, even though I only used the word main once (in the main file).
Also, it says in line 6 that I have no return in a function returning non-void. How do I fix these.
It's long so this is just a section, i've only cut out a few operations though. Am I missing anything?

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
  
#include <iostream>
#include <cmath>               // do I #include the header and main files here?
using namespace std;

int Selection (){
    int Sel = 0;
    cout << "Please select a desired operation from the list below : \n\n";
    cout << "1 Calculate percentage of selected number  \n\n ";
    cout << "2 Calculate integer division of two entered numbers.";
    cout << "3 Exit. \n\n Selection:";
    cin >> Sel;
    cout << Sel;
    return Sel;
}

bool operationSelector (int Sel) {
    if (Sel ==1){
        double N=0.0;
        double P=0.0;
        perEntNum (N,P);
        return true;
    }
     else if (Sel == 2){
        double N = 0.0;
        double D = 0.0;
        iDiv(N,D);
        return true;
    }
    else {
        return false;
        }
}

void perEntNum (double Number, double Percentage){
    double N = 0.0;
    double P = 0.0;
    cout << "Enter Number: ";
    cin << N;
    count << "\n\n Enter Percentage out of 100: ";
    cin << P;
    cout << "\n\n Resulting Amount: " <<N*P/100<<endl;
}

void iDiv (double Numerator, double Denominator){
    double N = 0.0;
    double D = 0.0;
    cout << "Enter Number 1: ";
    cin << N;
    count << "\n\n Enter Number 2: ";
    cin << P;
    cout << "\n\n Resulting Amount: " <<int(Numerator/Denominator)<<endl;
}
Last edited on
It says 'main' is first defined in my "main function" as the error, but it isn't defined here is it?
is this a header file? try making this a header file (.h) not (.cpp)

then add a main.cpp or source.cpp to your project.

1
2
3
4
5
6
7
#include "myfunctions.h"

int main ()
{
      return 0;
}
Last edited on
You obviously did define main more than once or you would not be getting that error.
I am going to guess that you tried to write main() as a list of function calls and over complicating everything in the progress also.
1
2
3
4
5
6
7
8
9
10
11
// What purpose do the arguments serve?
// Number and Percentage are never used.
void perEntNum (double Number, double Percentage){
    double N = 0.0;
    double P = 0.0;
    cout << "Enter Number: ";
    cin << N;
    count << "\n\n Enter Percentage out of 100: ";
    cin << P;
    cout << "\n\n Resulting Amount: " <<N*P/100<<endl;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// What purpose does this function even have?
void iDiv (double Numerator, double Denominator){
    double N = 0.0;
    double D = 0.0;
    cout << "Enter Number 1: ";
    cin << N;
    count << "\n\n Enter Number 2: ";
    cin << P;
    cout << "\n\n Resulting Amount: " <<int(Numerator/Denominator)<<endl;
}

// Could be simplified to 
void iDiv (double Numerator, double Denominator){
    cout << "\n\n Resulting Amount: " <<int(Numerator/Denominator)<<endl;
}
// since all it does is get unused input from the user
// or better yet just write the cout statement in place of the function call 
// it would be easier to see that you equation is equal to 0 / 0 that way 


With all the mismatches and unused variables in this part it is very likely that you do have more than one main function.
Post your Source.cpp file..
Thanks. The problem was that i said #include (main file)


Any one know why the function at line 45 wont work? It keeps giving me a very large negative number.
What numbers are you calling iDiv with? Also, you declare variables N and D but never do anything useful with them, as admkrk pointed out.

It'd probably be helpful if you showed what you were doing in your main function, we're only getting half the story.
Last edited on
You're welcome OP :) some times you can't take compiler errors for face value.

Please post your Source File

1
2
3
4
5
6
7
8
9
10
void iDiv (double Numerator, double Denominator){
    double N = 0.0;
    double D = 0.0;
    cout << "Enter Number 1: ";
    cin << N;
    count << "\n\n Enter Number 2: "; // Here should be cout you have count
    cin << P;
    cout << "\n\n Resulting Amount: " <<int(Numerator/Denominator)<<endl; // what are you doing here? 

}


1
2
3
4
5
6
7
8
9
void perEntNum (double Number, double Percentage){
    double N = 0.0;
    double P = 0.0;
    cout << "Enter Number: ";
    cin << N;
    count << "\n\n Enter Percentage out of 100: ";
    cin << P;
    cout << "\n\n Resulting Amount: " <<N*P/100<<endl;
}
Last edited on
Unfortunately I do not have the computer with the other files with me. I think though, that they just tell this to run. It doesn't do anything different. (The spelling errors are for the same reason, I am just typing out the code here rather than copying and pasting)

What I am trying to do there is complete a division between any two numbers and change the answer to an integer. Obviously that is not what is actually happening though
Last edited on
Ok i think i know what you mean. I gave you 2 examples on how you can structure a function. i hope you find this useful. If you have any questions please ask.

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

#include <iostream>

using namespace std;


void divFrac() // no need for functions parameters here nothing going into function
{
    int N;
    int D;
    cout << "Enter first Num" << endl;
    cin >> N ;
    cout << "Enter second Num" << endl;
    cin >> D;
    
    cout << "Result: " << (int)(N/D) << endl;
}

void divFrac2(int N, int D) // Passing Parameters to function example
{
    cout << "Result " << (int) (N/D) << endl;
    
}

int main()
{
    // no parameters needed for this function
    
    divFrac();
    
    
    // Passing Parameters to function example
    
    int firstnum;
    int secondnum;
    
    
    cout << "Enter first Num" << endl;
    cin >> firstnum ;
    cout << "Enter second Num" << endl;
    cin >> secondnum;
    
    divFrac2(firstnum,secondnum); // this function takes in 2 ints (int, int)
    
    return 0;
}


Last edited on
The spelling errors are for the same reason, I am just typing out the code here rather than copying and pasting

That is like saying "my program looks something like this" compared to "this is my program". Spelling, punctuation, case, and many other things make a big difference! You are just wasting the time of the people that are are trying to help you if you do not post what your code really is.
Topic archived. No new replies allowed.