//main

#include <iostream>
#include <cstdlib>
using namespace std;
#include "libro.h"
int main()
{
    test ab;
    cout<<"enter width limit";
    int length;
    cin>>length;
    ab.alternate(length);
}

//implementation

#include <iostream>
#include <cctype>
#include <cstdlib>
using namespace std;
#include "libro.h"

int test::alternate(int width)
{
    //for odd, 3 char-ellipsis
    if(width%2==1)
    {
         keep=width-3;
    }
    //for even, 4 char-ellipsis
    else if(width%2==0)
    {
        keep=width-4;
    }

    return keep_per_end=keep/2;
}

//interface

#ifndef LIBRO_H_INCLUDED
#define LIBRO_H_INCLUDED
#include <iostream>
using namespace std;

class test
{
    public:
    //check if even or odd 
    int alternate(int width);
    int keep_per_end;
    int keep;
};
#endif // LIBRO_H_INCLUDED

What are you trying to do?


Also you don't need the second if, just else!

And your return is hinky!

return keep_per_end=keep/2;

You need to add white space to your equations to make problems more apparent!

return keep_per_end = keep / 2;

I'm not sure what you're trying to do but instead do

keep_per_end = keep / 2;
  return keep_per_end;

ab is not right? in my main?

changed my code on top.

keep = width - 3
keep = width -4

What exactly is the function suppose to be doing?

the function is supposed to find out the number of characters at each end of a string it is supposed to store. and insert ... if odd and .... if even

//main

#include <iostream>
#include <cstdlib>
using namespace std;
#include "libro.h"
int main()
{
    test ab;
    cout<<"enter width limit";
    int length;
    cin>>length;
    ab.alternate(length);
}

You're checking for odd 1,3,5,7,9, etc.
And even 2,4,6, 8, etc.

But you're subtracting 3 from odd making it even but removing
3 characters?
When subracting 4 from even, you're removing 4 characters?

What are you trying to align to? You mention insert, do you mean append a space to make odd even? Then why is even being adjusted by four?

I'm still not seeing the required functionality!

my problem is that it doesn't return keep_per_end, my program doesn't compile

in my main it doesn't like ab.alternate(length);

That last line I pointed out typically saves the result of the = as a logical compare, not the length.
Break it into two lines.

the keep_per_end will stay valid as long as the class is within scope.

main()
needs you to return a value.


return 0;

modified my implementation

#include <iostream>
#include <cctype>
#include <cstdlib>
using namespace std;
#include "libro.h"

int test::alternate(int width)
{
    //for odd, 3 char-ellipsis
    if(width%2==1)
    {
        if(width)
        {
         keep=width-3;
        }
    }
    //for even, 4 char-ellipsis
    else if(width%2==0)
    {
        if(width)
        {
        keep=width-4;
        }
    }

    keep_per_end=keep/2;
    return keep_per_end;
}

that still doesn't solve my original issue.

int test::alternate(int width)
{
    //for odd, 3 char-ellipsis
    if(width%2==1)
    {
         keep=width-3;
    }
    //for even, 4 char-ellipsis
    else
    {
        if(width)
        {
        keep=width-4;
        }
    }

    keep_per_end=keep/2;
    return keep_per_end;
}
int main()
{
    test ab;
    cout<<"enter width limit";
    int length;
    cin>>length;
    ab.alternate(length);
   return 0;
}

That's the only thing I see!

I missed it....

int test::alternate(int width)
{
  keep = 0;
    //for odd, 3 char-ellipsis
    if(width%2==1)
    {
         keep=width-3;
    }
    //for even, 4 char-ellipsis
    else
    {
        if(width)
        {
        keep=width-4;
        }
    }

    keep_per_end=keep/2;
    return keep_per_end;
}

thanks for help.

Was that it?
I only noticed that it wasn't initialized in a zero state and compilers consider that an error! I find sometimes I have to initialized something even though I know the code will change it but the compiler doesn't see that state!

nope it isn't solved.

Rats.
I just cut your code into a test bench and had no errors.

Try putting it all in one file.

class definition

Then your two functions and try that!

What's your compiler error?

undefined reference to 'test::alternate(int)'

I thought I posted this line but don't see it so....

Combine them all into the same file and try again.

Sounds like a header problem. But I don't see any. I combined mine into the same file and compiled fine.

yea it compiles in one file. But i need library.

i got it to work. had to change some properties of how is built.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.