C++ Help (another new question)

Recommended Videos

TheRundownRabbit

Wicked Prolapse
Aug 27, 2009
3,825
0
0
Hello, I am working on a program that is quite complicated and need help with a few things.
2 things I need to know.

If the user inputs a year, I need a particular function to divide the first two digits of the year by 4. How do I get the function to only take in the first 2 digits? - SOLVED!

How do I throw a bunch of void functions together in a single function?

Edit:
Also, I need to detect if the year is a leap year, it needs to check if the year is divisible by 4 and not divisible by 100, how can I have the program detect this?

Edit...again:
I also need to take the final 2 digits of any year and use it to calculate year value, problem is, I do not know how to isolate the last 2 to be used in an algorithm.

VALID POINT: I can't use library functions.
 

BlazeRaider

New member
Dec 25, 2009
263
0
0
(assuming its 4 digits and you want the first 2) you could % the year by 100 to isolate the last 2 digits, subtract that value from the year, and divide the year by 400, then multiple by 100 then add back the value you got from using mod.

2012 % 100 = 12
x = 12
2012-12 = 2000
2000 / 400 = 5
5*100 = 500
500+ x = 512

20 / 4 = 5

thats assuming you meant you wanted to take the first 2 digits, turn them into a string, like 2012 -> 20, then divide 20 by 4, getting 5, while leaving the other digits untouched.
 

Palfreyfish

New member
Mar 18, 2011
284
0
0
Is there not a C++ function for getting the left hand side or the right hand side up to a certain number? Or does that only work for strings? Or is it only in C?

Actually, can you not read the string length of the year input into w/ever it is, and then use IF or CASE statements to decide what to do with it. Once that's done, for each IF/CASE statement, divide by 10^whatever to leave two digits before the decimal point, then just ignore everything after the decimal.


Sinput is the length of the converted string, input is the input, and setprecision (0) disregards everything after the decimal point.
Code:
Read input
input = input
Sinput = Strconvert (input)
CASE 1
    (Strlen 'Sinput' = 2)
    carry on with calculation
CASE 2
    (Strlen 'Sinput' = 3
    input=input/10
    setprecision 'input' (0)
CASE 3
    (Strlen 'Sinput' = 4)
    input=input/100
    setprecision 'input' (0)
CASE 3
    (Strlen 'Sinput' = 5)
    input=input/1000
    setprecision 'input' (0)
END CASE STATEMENT
output = Input/4
print output

I'm not sure how the setprecision command is used, but I know what it does. When set precision is set to 0, anything after the decimal point is ignored. Not entirely sure if this would work, but hey, it's my best guess.

You'd probably also have to convert the input into a string to read the string length, while storing the original input until it's needed later on in the calculation.
Mortai Gravesend said:
I just scrolled up to see that you'd said basically the same thing. Ah well, no sense in deleting what I wrote. However, I am unclear whether he wants each digit divided by 4, or the two digits together... Also, can you convert from a string to an integer? I wasn't sure, so I worked around it.
 

TheRundownRabbit

Wicked Prolapse
Aug 27, 2009
3,825
0
0
Palfreyfish said:
Is there not a C++ function for getting the left hand side or the right hand side up to a certain number? Or does that only work for strings? Or is it only in C?

Actually, can you not read the string length of the year input into w/ever it is, and then use IF or CASE statements to decide what to do with it. Once that's done, for each IF/CASE statement, divide by 10^whatever to leave two digits before the decimal point, then just ignore everything after the decimal.


Sinput is the length of the converted string, input is the input, and setprecision (0) disregards everything after the decimal point.
Code:
Read input
input = input
Sinput = Strconvert (input)
CASE 1
    (Strlen 'Sinput' = 2)
    carry on with calculation
CASE 2
    (Strlen 'Sinput' = 3
    input=input/10
    setprecision 'input' (0)
CASE 3
    (Strlen 'Sinput' = 4)
    input=input/100
    setprecision 'input' (0)
CASE 3
    (Strlen 'Sinput' = 5)
    input=input/1000
    setprecision 'input' (0)
END CASE STATEMENT
output = Input/4
print output

I'm not sure how the setprecision command is used, but I know what it does. When set precision is set to 0, anything after the decimal point is ignored. Not entirely sure if this would work, but hey, it's my best guess.

You'd probably also have to convert the input into a string to read the string length, while storing the original input until it's needed later on in the calculation.
Mortai Gravesend said:
I just scrolled up to see that you'd said basically the same thing. Ah well, no sense in deleting what I wrote. However, I am unclear whether he wants each digit divided by 4, or the two digits together... Also, can you convert from a string to an integer? I wasn't sure, so I worked around it.
If what your speaking of is a library function, I can't use those.
 

Palfreyfish

New member
Mar 18, 2011
284
0
0
Haseo21 said:
Palfreyfish said:
Is there not a C++ function for getting the left hand side or the right hand side up to a certain number? Or does that only work for strings? Or is it only in C?

Actually, can you not read the string length of the year input into w/ever it is, and then use IF or CASE statements to decide what to do with it. Once that's done, for each IF/CASE statement, divide by 10^whatever to leave two digits before the decimal point, then just ignore everything after the decimal.


Sinput is the length of the converted string, input is the input, and setprecision (0) disregards everything after the decimal point.
Code:
Read input
input = input
Sinput = Strconvert (input)
CASE 1
    (Strlen 'Sinput' = 2)
    carry on with calculation
CASE 2
    (Strlen 'Sinput' = 3
    input=input/10
    setprecision 'input' (0)
CASE 3
    (Strlen 'Sinput' = 4)
    input=input/100
    setprecision 'input' (0)
CASE 3
    (Strlen 'Sinput' = 5)
    input=input/1000
    setprecision 'input' (0)
END CASE STATEMENT
output = Input/4
print output

I'm not sure how the setprecision command is used, but I know what it does. When set precision is set to 0, anything after the decimal point is ignored. Not entirely sure if this would work, but hey, it's my best guess.

You'd probably also have to convert the input into a string to read the string length, while storing the original input until it's needed later on in the calculation.
Mortai Gravesend said:
I just scrolled up to see that you'd said basically the same thing. Ah well, no sense in deleting what I wrote. However, I am unclear whether he wants each digit divided by 4, or the two digits together... Also, can you convert from a string to an integer? I wasn't sure, so I worked around it.
If what your speaking of is a library function, I can't use those.
It probably is a library function. I wouldn't know, I rarely use C++. Why can't you include Library functions? I mean, to use it you might need these: #include #include , but I'm not sure if those are necessary for setprecision. If someone could correct/inform me that would be lovely.

It is a Library function, assuming my English interpretation skills are up to scratch, as the page is written in a confusing way...
 

TheRundownRabbit

Wicked Prolapse
Aug 27, 2009
3,825
0
0
Palfreyfish said:
Haseo21 said:
Palfreyfish said:
Is there not a C++ function for getting the left hand side or the right hand side up to a certain number? Or does that only work for strings? Or is it only in C?

Actually, can you not read the string length of the year input into w/ever it is, and then use IF or CASE statements to decide what to do with it. Once that's done, for each IF/CASE statement, divide by 10^whatever to leave two digits before the decimal point, then just ignore everything after the decimal.


Sinput is the length of the converted string, input is the input, and setprecision (0) disregards everything after the decimal point.
Code:
Read input
input = input
Sinput = Strconvert (input)
CASE 1
    (Strlen 'Sinput' = 2)
    carry on with calculation
CASE 2
    (Strlen 'Sinput' = 3
    input=input/10
    setprecision 'input' (0)
CASE 3
    (Strlen 'Sinput' = 4)
    input=input/100
    setprecision 'input' (0)
CASE 3
    (Strlen 'Sinput' = 5)
    input=input/1000
    setprecision 'input' (0)
END CASE STATEMENT
output = Input/4
print output

I'm not sure how the setprecision command is used, but I know what it does. When set precision is set to 0, anything after the decimal point is ignored. Not entirely sure if this would work, but hey, it's my best guess.

You'd probably also have to convert the input into a string to read the string length, while storing the original input until it's needed later on in the calculation.
Mortai Gravesend said:
I just scrolled up to see that you'd said basically the same thing. Ah well, no sense in deleting what I wrote. However, I am unclear whether he wants each digit divided by 4, or the two digits together... Also, can you convert from a string to an integer? I wasn't sure, so I worked around it.
If what your speaking of is a library function, I can't use those.
It probably is a library function. I wouldn't know, I rarely use C++. Why can't you include Library functions? I mean, to use it you might need these: #include #include , but I'm not sure if those are necessary for setprecision. If someone could correct/inform me that would be lovely.

It is a Library function, assuming my English interpretation skills are up to scratch, as the page is written in a confusing way...
My professor told me I can't use library functions.
 

Palfreyfish

New member
Mar 18, 2011
284
0
0
Haseo21 said:
Palfreyfish said:
Haseo21 said:
Palfreyfish said:
Is there not a C++ function for getting the left hand side or the right hand side up to a certain number? Or does that only work for strings? Or is it only in C?

Actually, can you not read the string length of the year input into w/ever it is, and then use IF or CASE statements to decide what to do with it. Once that's done, for each IF/CASE statement, divide by 10^whatever to leave two digits before the decimal point, then just ignore everything after the decimal.


Sinput is the length of the converted string, input is the input, and setprecision (0) disregards everything after the decimal point.
Code:
Read input
input = input
Sinput = Strconvert (input)
CASE 1
    (Strlen 'Sinput' = 2)
    carry on with calculation
CASE 2
    (Strlen 'Sinput' = 3
    input=input/10
    setprecision 'input' (0)
CASE 3
    (Strlen 'Sinput' = 4)
    input=input/100
    setprecision 'input' (0)
CASE 3
    (Strlen 'Sinput' = 5)
    input=input/1000
    setprecision 'input' (0)
END CASE STATEMENT
output = Input/4
print output

I'm not sure how the setprecision command is used, but I know what it does. When set precision is set to 0, anything after the decimal point is ignored. Not entirely sure if this would work, but hey, it's my best guess.

You'd probably also have to convert the input into a string to read the string length, while storing the original input until it's needed later on in the calculation.
Mortai Gravesend said:
I just scrolled up to see that you'd said basically the same thing. Ah well, no sense in deleting what I wrote. However, I am unclear whether he wants each digit divided by 4, or the two digits together... Also, can you convert from a string to an integer? I wasn't sure, so I worked around it.
If what your speaking of is a library function, I can't use those.
It probably is a library function. I wouldn't know, I rarely use C++. Why can't you include Library functions? I mean, to use it you might need these: #include #include , but I'm not sure if those are necessary for setprecision. If someone could correct/inform me that would be lovely.

It is a Library function, assuming my English interpretation skills are up to scratch, as the page is written in a confusing way...
My professor told me I can't use library functions.
Well that's awkward. Hmmm. The divisible by 4 bit I think you could do by dividing the number by 4, and then because the answer's going to need a floating point to store it, then it might spit out an error? I'm not sure, however. Sorry, I can't really be much help other than that. My professors never restricted libraries from me. And surely if it's divisible by 10 and 4 then it's a leap year? 2040's a leap year, so's 2000. If it's divisible by 4 then it's a leap year for the next 1000 years or so.

Obviously it's not an impossible task, or he wouldn't have set it.
 

TheRundownRabbit

Wicked Prolapse
Aug 27, 2009
3,825
0
0
Palfreyfish said:
Haseo21 said:
Palfreyfish said:
Haseo21 said:
Palfreyfish said:
Is there not a C++ function for getting the left hand side or the right hand side up to a certain number? Or does that only work for strings? Or is it only in C?

Actually, can you not read the string length of the year input into w/ever it is, and then use IF or CASE statements to decide what to do with it. Once that's done, for each IF/CASE statement, divide by 10^whatever to leave two digits before the decimal point, then just ignore everything after the decimal.


Sinput is the length of the converted string, input is the input, and setprecision (0) disregards everything after the decimal point.
Code:
Read input
input = input
Sinput = Strconvert (input)
CASE 1
    (Strlen 'Sinput' = 2)
    carry on with calculation
CASE 2
    (Strlen 'Sinput' = 3
    input=input/10
    setprecision 'input' (0)
CASE 3
    (Strlen 'Sinput' = 4)
    input=input/100
    setprecision 'input' (0)
CASE 3
    (Strlen 'Sinput' = 5)
    input=input/1000
    setprecision 'input' (0)
END CASE STATEMENT
output = Input/4
print output

I'm not sure how the setprecision command is used, but I know what it does. When set precision is set to 0, anything after the decimal point is ignored. Not entirely sure if this would work, but hey, it's my best guess.

You'd probably also have to convert the input into a string to read the string length, while storing the original input until it's needed later on in the calculation.
Mortai Gravesend said:
I just scrolled up to see that you'd said basically the same thing. Ah well, no sense in deleting what I wrote. However, I am unclear whether he wants each digit divided by 4, or the two digits together... Also, can you convert from a string to an integer? I wasn't sure, so I worked around it.
If what your speaking of is a library function, I can't use those.
It probably is a library function. I wouldn't know, I rarely use C++. Why can't you include Library functions? I mean, to use it you might need these: #include #include , but I'm not sure if those are necessary for setprecision. If someone could correct/inform me that would be lovely.

It is a Library function, assuming my English interpretation skills are up to scratch, as the page is written in a confusing way...
My professor told me I can't use library functions.
Well that's awkward. Hmmm. The divisible by 4 bit I think you could do by dividing the number by 4, and then because the answer's going to need a floating point to store it, then it might spit out an error? I'm not sure, however. Sorry, I can't really be much help other than that. My professors never restricted libraries from me. And surely if it's divisible by 10 and 4 then it's a leap year? 2040's a leap year, so's 2000. If it's divisible by 4 then it's a leap year for the next 1000 years or so.

Obviously it's not an impossible task, or he wouldn't have set it.
Crap, instead of "not divisible by 10" I meant to say "not divisible by 100", my bad
 

Palfreyfish

New member
Mar 18, 2011
284
0
0
Ah, okay. Sorry I couldn't be of more help, I've been writing about code all day it seems, what with my Computing project and this... Have you tried a website dedicated to C++? They'd probably be more help than me :)
 

DoPo

"You're not cleared for that."
Jan 30, 2012
8,663
0
0
Haseo21 said:
How do I throw a bunch of void functions together in a single function?
I don't understand what you mean. Do you have something like
Code said:
Code:
//some void functions

public void foo() {
   //does something
}

public void bar() {
//does something
}

public void baz() {
   //does something
}

//the "master" function
public void blah() {
   foo();
   bar();
   baz();
}
Assuming it's all in the same class. Also, my C++ syntax is crap right now, partly because I'm just about to go to sleep.

Haseo21 said:
Also, I need to detect if the year is a leap year, it needs to check if the year is divisible by 4 and not divisible by 100, how can I have the program detect this?
I skimmed the thread and it seems like you had some problems with the leap years still, despite some suggestions. Doesn't this work?

Code:
public int isLeap(int year) {
   //what we'll return
   int isLeap = 0;

   //needs to be divisible to 4 to be a leap year
   if (0 == year % 4) {
       // but wait - is it a century?
       if (0 == year % 100) {
           // most probably not a leap year, unless it's divisible by 400
           if (0 == year % 400) {
              isLeap = 1;
           }
       } else {
           //thank you calendar makers, for making it so easy for us
           isLeap = 1;
       }
   }

   return isLeap;
}
I think it only works for, umm, four digit years and some three digit ones but I have no clue. I'm so tired right now, that I can't figure it out. And I don't know if it works even. I hate doing calculations with times and dates.

I'll take a look tomorrow and try to figure out what's wrong with my code (I'm pretty sure there is something wrong). But I'll just post it now because it might help you. Or make you laugh, I don't know.

P.S. no formatting for code...crap.
 

TheRundownRabbit

Wicked Prolapse
Aug 27, 2009
3,825
0
0
DoPo said:
Haseo21 said:
How do I throw a bunch of void functions together in a single function?
I don't understand what you mean. Do you have something like
Code said:
Code:
//some void functions

public void foo() {
   //does something
}

public void bar() {
//does something
}

public void baz() {
   //does something
}

//the "master" function
public void blah() {
   foo();
   bar();
   baz();
}
Assuming it's all in the same class. Also, my C++ syntax is crap right now, partly because I'm just about to go to sleep.

Haseo21 said:
Also, I need to detect if the year is a leap year, it needs to check if the year is divisible by 4 and not divisible by 100, how can I have the program detect this?
I skimmed the thread and it seems like you had some problems with the leap years still, despite some suggestions. Doesn't this work?

Code:
public int isLeap(int year) {
   //what we'll return
   int isLeap = 0;

   //needs to be divisible to 4 to be a leap year
   if (0 == year % 4) {
       // but wait - is it a century?
       if (0 == year % 100) {
           // most probably not a leap year, unless it's divisible by 400
           if (0 == year % 400) {
              isLeap = 1;
           }
       } else {
           //thank you calendar makers, for making it so easy for us
           isLeap = 1;
       }
   }

   return isLeap;
}
I think it only works for, umm, four digit years and some three digit ones but I have no clue. I'm so tired right now, that I can't figure it out. And I don't know if it works even. I hate doing calculations with times and dates.

I'll take a look tomorrow and try to figure out what's wrong with my code (I'm pretty sure there is something wrong). But I'll just post it now because it might help you. Or make you laugh, I don't know.

P.S. no formatting for code...crap.
Thank you, but I have one more question.
I also need to take the final 2 digits of any year and use it to calculate year value, problem is, I do not know how to isolate the last 2 to be used in an algorithm. Can ya help?
 

MidnightCat

New member
Jul 21, 2009
125
0
0
Haseo21 said:
Thank you, but I have one more question.
I also need to take the final 2 digits of any year and use it to calculate year value, problem is, I do not know how to isolate the last 2 to be used in an algorithm. Can ya help?
Try this:
Code:
//nYear is the year, nDigits will be the last two digits
int nDigits = nYear;
while(nDigits >= 100)
{
	nDigits-= 100;
}
You should end up with the last two digits of any number that you enter. :)
 

DoPo

"You're not cleared for that."
Jan 30, 2012
8,663
0
0
Haseo21 said:
Thank you, but I have one more question.
I also need to take the final 2 digits of any year and use it to calculate year value, problem is, I do not know how to isolate the last 2 to be used in an algorithm. Can ya help?
So it works? Huh, cool.

Yeah, I just tested that
Code:
isLeap()
and it does indeed work correctly with all the years. At least the positive ones - as I said, I hate date arithmetic and I don't really know how or, or even if, you can calculate leap years from before year 1 (also, there is no year 0, which messes the things a bit, as well). But I'm pretty sure you aren't tasked with reinventing the calendar system.

And for the question, what MidnightCat gave you would work but it's a bit weird - takes a roundabout approach. I would use

Code:
int lastTwoDigits = year % 100
There - one line.