Problem with codewars.

Joined
Dec 4, 2023
Messages
3
Reaction score
0
hello

im trying to learn c++ so i thaught id do the easy stuff on codewars, please can someone help me find out whats wrong with my code. the the quadrant one. i didnt know i knew so little lol its different actually coding and not just following tutorials, ive serched for answers to the errors, these are the ones i couldnt find answers too.

My code:

C++:
int quadrent;
int function(int x, int y)
{
if(x > 0 && y > 0)
{
quadrent = 1;
}
else if(x < 0 && y > 0)
{
quadrent = 2;
}
else if(x < 0 && y < 0)
{
quadrent = 3;
}
else if(x > 0 && y < 0)
{
quadrent = 4;
}
}
int main()
{
int function(x, y);
}


the errors:

C++:
In file included from main.cpp:6:
./solution.cpp:20:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
./solution.cpp:23:14: error: C++ requires a type specifier for all declarations
int function(x, y);
             ^
./solution.cpp:23:17: error: unknown type name 'y'
int function(x, y);
                ^
In file included from main.cpp:7:
./tests.cpp:3:18: error: use of undeclared identifier 'quadrant'
    Assert::That(quadrant(1, 2), Equals(1));
                 ^
./tests.cpp:4:18: error: use of undeclared identifier 'quadrant'
    Assert::That(quadrant(3, 5), Equals(1));
                 ^
./tests.cpp:5:18: error: use of undeclared identifier 'quadrant'
    Assert::That(quadrant(-10, 100), Equals(2));
                 ^
./tests.cpp:6:18: error: use of undeclared identifier 'quadrant'
    Assert::That(quadrant(-1, -9), Equals(3));
                 ^
./tests.cpp:7:18: error: use of undeclared identifier 'quadrant'
    Assert::That(quadrant(19, -56), Equals(4));
                 ^
main.cpp:9:5: error: conflicting types for 'main'
int main(int, const char *[]) {
    ^
./solution.cpp:21:5: note: previous definition is here
int main()
    ^
1 warning and 8 errors generated.
 
Joined
Jul 4, 2023
Messages
341
Reaction score
39
Check this e.g.
[ working code on-line ]
C++:
#include <iostream>

using namespace std;

int quadrent;
void function(int x, int y)
{
    if(x > 0 && y > 0)
    {
        quadrent = 1;
    }
    else if(x < 0 && y > 0)
    {
        quadrent = 2;
    }
    else if(x < 0 && y < 0)
    {
        quadrent = 3;
    }
    else if(x > 0 && y < 0)
    {
        quadrent = 4;
    }
}

int main()
{
    int x=1, y=1;
    function(x, y);
    cout << quadrent;
}
 
Joined
Dec 4, 2023
Messages
3
Reaction score
0
so basically i messed the main up? i had same errors in previous attempts, its always got to do with other functions and main. thank you so much, really helped alot.

all the best.
 
Joined
Dec 4, 2023
Messages
3
Reaction score
0
okay i just copy pasted into code wars and it was still wrong, someone online said codewars needs return values, and no need for main. but thank you im proud that my code was 99% similar to your example you gave me.
 
Joined
Jul 4, 2023
Messages
341
Reaction score
39
needs return values
[ working code on-line ]
C++:
int function(int x, int y)
{
    int quadrent;
   
    if(x > 0 && y > 0)
    {
        quadrent = 1;
    }
    else if(x < 0 && y > 0)
    {
        quadrent = 2;
    }
    else if(x < 0 && y < 0)
    {
        quadrent = 3;
    }
    else if(x > 0 && y < 0)
    {
        quadrent = 4;
    }
   
    return quadrent;
}

C++:
#include <iostream>

using namespace std;


int function(int x, int y)
{
    int quadrent;
    
    if(x > 0 && y > 0)
    {
        quadrent = 1;
    }
    else if(x < 0 && y > 0)
    {
        quadrent = 2;
    }
    else if(x < 0 && y < 0)
    {
        quadrent = 3;
    }
    else if(x > 0 && y < 0)
    {
        quadrent = 4;
    }
    
    return quadrent;
}

int main()
{
    int x=1, y=1;
    
    cout << function(x, y);
    
    return 0;
}
 
Joined
Dec 10, 2023
Messages
1
Reaction score
0
First you put an int before the function (the one who after int main)
functions don't have data type
Then you didn't define x and y
So
int x;
int y;
cin>>x>>y;
functionAB(x, y);
cout<<quadrent;

change the function name because the name function it will cause a conflict at naming
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,717
Messages
2,569,382
Members
44,701
Latest member
OurCBDLifeSupplement

Latest Threads

Top