Could you help me seperate my main function into multiple different functions, please?

Hello all, I've been working on this program for a while now, it urges the user to input coordinates of points lying on a cartesian plane and then outputs the quadrant with most points in them. I only have one main() function and I've been trying to get my main function to call various functions and seperate the rest into other functions, but no matter how I try it I keep running into problems. Here is my 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
#include <iostream>
#include <cstdlib>
using namespace std;
//define type:
struct TPont{double x,y;} ;
//define the maximum relation:
int max(int a,int b,int c,int d)
{
int max_val=a;
if (b>max_val)
max_val=b;
if (c>max_val)
max_val=c;
if (d>max_val)
max_val=d;
return max_val;
}

int main()
{
    //input
    TPont p;
    int n;
    //output
    //checking the validity of input
    bool error;
    string tmp;
    //intializing quadrants
    int first=0;
    int second=0;
    int third=0;
    int fourth=0;

    //inputting
    do{
    cout << "How many points will you be checking for?" << endl; cin >> n;
    error=cin.fail();
        if (error)
        {
            cout << "Wrong input!" <<endl;
            cin.clear(); getline(cin,tmp,'\n');

        }
    }while (error);
    for(int i=0;i<n;i++){
        do{
        cout << "Please type the x and y coordinates of point p! " << endl; cin >> p.x >> p.y;
        error=cin.fail();
        if (error)
        {
            cout << "Wrong input!" <<endl;
            cin.clear(); getline(cin,tmp,'\n');

        }
    }while (error);

    //counting how many points lie in each quadrant
    if (p.x>0)
    {
        if (p.y>0) first++;
            else if (p.y<0) fourth++;
    }else if (p.x<0){
        if (p.y>0) second++;
            else if (p.y<0) third++;
    }
    //If any points lie on the x axis then the program writes that
    if (p.x==0)
   {
        cout << "The (" << p.x << ", "<< p.y <<")" << "point lies on the X axis." <<endl;
    }

    }

    //checking for maximum and writing it on the screen

    if (first==max(first, second, third, fourth))
    {
         cout << "Most points lie in the first quadrant." <<endl;

    }
    if (second==max(first, second, third, fourth))
    {
         cout << "Most points lie in the second quadrant." <<endl;

    }
    if (third==max(first, second, third, fourth))
    {
         cout << "Most points lie in the third quadrant." <<endl;

    }
    if (fourth==max(first, second, third, fourth))
    {
         cout << "Most points lie in the fourth quadrant." <<endl;

    }

    system("pause");
    return 0;
}


So far I have tried to do it like this:
main included a:
void read(n,p) function
void quadrantCounting(first,second,third,fourth) function
void quadrantOutput(first,second,third,fourth) function

But I've never managed to get it to work, I'd really appreciate any help! I have also asked another question about the same topic considering arrays here: http://www.cplusplus.com/forum/beginner/56410/ so if anyone can help me with that as well,I'd be very grateful! Thank you in advance!
By the way here is how I tried to do it, but it works so awfully that I am ashamed to post it :)


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
#include <iostream>
#include <cstdlib>
using namespace std;
//define type
struct TPont{double x,y;} ;
float read(int &n, TPont &p);
void quadrants(int &first, int &second, int &third, int &fourth);
void waitforKB();

int main()
{
    //input
    TPont p;
    int n, first, second, third, fourth;
    //output
    read(n, p);
    quadrants(first,second,third,fourth);
    waitforKB();
    return 0;
    }


//reading input
float read(int &n, TPont &p){
    bool error;
    string tmp;
    do{
    cout << "How many points will you be checking for?" << endl; cin >> n;
    error=cin.fail() || n<0;
        if (error)
        {
            cout << "Wrong input!" <<endl;
            cin.clear(); getline(cin,tmp,'\n');
        }
    }while (error);

 for(int i=0;i<n;i++){
    do{
    cout << "Please give the x y coordinates of point p!" << endl; cin >> p.x >> p.y;
    error=cin.fail();
        if (error)
        {
            cout << "Wrong input!" <<endl;
            cin.clear(); getline(cin,tmp,'\n');
        }
    }while (error);
    }
    return p.x;
    return p.y;
    }
     

//define maximum relation
int max(int a,int b,int c,int d)
{
int max_val=a;
if (b>=max_val)
max_val=b;
if (c>=max_val)
max_val=c;
if (d>=max_val)
max_val=d;
return max_val;
}

//count which quadrants have how many points
void quadrants(int& first, int& second, int& third, int& fourth){
    TPont p;
    if (p.x>0)
    {
        if (p.y>0) first++;
            else if (p.y<0) fourth++;
    }else if (p.x<0){
        if (p.y>0) second++;
            else if (p.y<0) third++;
    }

//check for maximum
    if (first==max(first, second, third, fourth))
    cout << "Most points lie in the first quadrant." <<endl;
    if (second==max(first, second, third, fourth))
    cout << "Most points lie in the second quadrant." <<endl;
    if (third==max(first, second, third, fourth))
    cout << "Most points lie in the third quadrant." <<endl;
    if (fourth==max(first, second, third, fourth))
    cout << "Most points lie in the fourth quadrant." <<endl;
}

//This doesn't work AT ALL, I am just trying to put the coordinates that lie on the x axis in an array, but it isnt working at all
//    void xAxis(float xAxis[]){
//    if (p.x==0)
//    {
//       p.y=xAxis[n];
//       cout << xAxis[n] <<endl;
//    }
//    }

void waitforKB()
    {
        system("pause");
    }
Last edited on
Instead of posting different topics, why not use a PB link and ask this problem in the original thread. It'll be easier to keep track of your problems then... Don't start on two different issues in the same program at two different topics...
I just didn't want to make one topic too clustered and the guideline said that the title of the thread should explain the problem and I didn't want to have 2 different problems under one topic. I am really new here, but I'll do what you are saying next time, sorry!
Its not all for the rules... If you try changing two bits of your code at the same time, you won't know where the problem is occurring.. So go one step at a time..
Yeah, you are right, I am so lost now exactly because I've started working on two separate problems. I've decided to focus on the X axis problem right now (that you helped me with) and once I have a program that does what I'd like it to do, I'll deal with how to turn various parts of it into functions and make the main function call it.
Topic archived. No new replies allowed.