Help with Char* (i.e. c = atoi(year+2))

Y

yogi_bear_79

char *year = strtok_s(NULL, "\0", &nextToken);
c = atoi(year+2);


The above code c = atoi(year+2); was suggested to me to help turn the
Char* into an int and only keep the last two characters. Now I need to
do the same thing but only keep the first two. I'd like a reference
on the +2 part, as this is the part I didn't know about. Since I
assume the code to display the first two digits would be similar I
would like a reference on what was done here. Thanks
 
C

Christopher

char *year = strtok_s(NULL, "\0", &nextToken);
c = atoi(year+2);

The above code c = atoi(year+2); was suggested to me to help turn the
Char* into an int and only keep the last two characters. Now I need to
do the same thing but only keep the first two. I'd like a reference
on the +2 part, as this is the part I didn't know about. Since I
assume the code to display the first two digits would be similar I
would like a reference on what was done here. Thanks

1 Do not use char* for text. Use std::string instead
2 Do not use ato_anything_ or _anything_toa functions. Use a
std::stringstream for conversions instead
3 The <ctime> header already has everything you may need for time and
date objects and the C++ streams already know how to translate them to
text and provide formatting for you.
 
Y

yogi_bear_79

1 Do not use char* for text. Use std::string instead
2 Do not use ato_anything_ or _anything_toa functions. Use a
std::stringstream for conversions instead
3 The <ctime> header already has everything you may need for time and
date objects and the C++ streams already know how to translate them to
text and provide formatting for you.

Thanks, we haven't gotten to std:string yet, this lab is done except
for seperating the first two digits from the year, like I did with the
last two
 
I

Ian Collins

yogi_bear_79 said:
char *year = strtok_s(NULL, "\0", &nextToken);

What's strtok_s?
c = atoi(year+2);


The above code c = atoi(year+2); was suggested to me to help turn the
Char* into an int and only keep the last two characters. Now I need to
do the same thing but only keep the first two. I'd like a reference
on the +2 part, as this is the part I didn't know about. Since I
assume the code to display the first two digits would be similar I
would like a reference on what was done here. Thanks

If year pointed to the C string "2008", year+2 would point to "08".
 
Y

yogi_bear_79

What's strtok_s?



If year pointed to the C string "2008", year+2 would point to "08".

Yes, year+2 does point to 08, which is what I wanted. However I also
need two point to the first two digits 20. That is the part I do not
know how to do. My compiler said that strtok was depricated and to
use strtok_s. This is just a simple lab.
 
D

Default User

yogi_bear_79 said:
char *year = strtok_s(NULL, "\0", &nextToken);

There's no standard function called strtok_s(). As such, I can't
comment on whether what you have works. I suspect you're not showing us
all the code.
c = atoi(year+2);


The above code c = atoi(year+2); was suggested to me to help turn the
Char* into an int and only keep the last two characters. Now I need to
do the same thing but only keep the first two. I'd like a reference
on the +2 part, as this is the part I didn't know about. Since I
assume the code to display the first two digits would be similar I
would like a reference on what was done here. Thanks

No, it wouldn't. The easiest way, given a modifiable buffer, would be;

year[2] = '/0';


Don't use atoi() unless you know for sure that the string represents an
number that fits in an int.

Better yet is the advice you were given before, use std::string and
such. You have no real knowledge of C-style strings, so why are trying
to use them?



Brian
 
Y

yogi_bear_79

yogi_bear_79 said:
char *year = strtok_s(NULL, "\0", &nextToken);

There's no standard function called strtok_s(). As such, I can't
comment on whether what you have works. I suspect you're not showing us
all the code.
c = atoi(year+2);
The above code c = atoi(year+2); was suggested to me to help turn the
Char* into an int and only keep the last two characters. Now I need to
do the same thing but only keep the first two.  I'd like a reference
on the +2 part, as this is the part I didn't know about.  Since I
assume the code to display the first two digits would be similar I
would like a reference on what was done here.  Thanks

No, it wouldn't. The easiest way, given a modifiable buffer, would be;

year[2] = '/0';

Don't use atoi() unless you know for sure that the string represents an
number that fits in an int.

Better yet is the advice you were given before, use std::string and
such. You have no real knowledge of C-style strings, so why are trying
to use them?

Brian


The entire code is:
char* nextToken;
int a, b, c, d;

char *month = strtok_s(longDate, " ", &nextToken);
char *day = strtok_s(NULL, " ", &nextToken);
char *year = strtok_s(NULL, "\0", &nextToken);

c = atoi(year+2);//returns the last two digits from char *year
d = atoi(year); //need to be modified to return the first two digits.

I am in a distant learning enviornment, this is only a simple lab, the
input data is controlled, thus I know exactly what will be in my
string to start with. We are not to std:string yet, or even error
handleing. While I agree there are certainly better ways to do
things, in this instance this is what is required.
 
Y

yogi_bear_79

There's no standard function called strtok_s(). As such, I can't
comment on whether what you have works. I suspect you're not showing us
all the code.
No, it wouldn't. The easiest way, given a modifiable buffer, would be;
year[2] = '/0';
Don't use atoi() unless you know for sure that the string represents an
number that fits in an int.
Better yet is the advice you were given before, use std::string and
such. You have no real knowledge of C-style strings, so why are trying
to use them?

The entire code is:
char* nextToken;
int a, b, c, d;

        char *month = strtok_s(longDate, " ", &nextToken);
        char *day = strtok_s(NULL, " ", &nextToken);
        char *year = strtok_s(NULL, "\0", &nextToken);

c = atoi(year+2);//returns the last two digits from char *year
d = atoi(year); //need to be modified to return the first two digits.

I am in a distant learning enviornment, this is only a simple lab, the
input data is controlled, thus I know exactly what will be in my
string to start with.  We are not to std:string yet, or even error
handleing.  While I agree there are certainly better ways to do
things, in this instance this is what is required.- Hide quoted text -

- Show quoted text -

int main ()
{
char longDate[20];

cout << " \n Enter a date in the following format(February 27,
2008):";
cin.getline(longDate,20);
cout << day_of_the_week(longDate);
}

int day_of_the_week(char longDate[])
{
char* nextToken;
int a, b, c, d, r, w, x, y, z;

char *month = strtok_s(longDate, " ", &nextToken);
char *day = strtok_s(NULL, " ", &nextToken);
char *year = strtok_s(NULL, "\0", &nextToken);
......


This is the code, it seems fairly simple to me, it splits the input
date (given format) into the three sections, and it works great, I
have no idea how to attain the same resutls with std::string
 
D

Default User

yogi_bear_79 said:
Yes, year+2 does point to 08, which is what I wanted. However I also
need two point to the first two digits 20. That is the part I do not
know how to do.

See my previous message.
My compiler said that strtok was depricated and to
use strtok_s.

That's a lie told by certain Microsoft products. strtok() has not been
deprecated (although it is a tricky function to use properly) and
strtok_s() is a non-standard proprietary function.




Brian
 
Y

yogi_bear_79

See my previous message.


That's a lie told by certain Microsoft products. strtok() has not been
deprecated (although it is a tricky function to use properly) and
strtok_s() is a non-standard proprietary function.

Brian

Ok, I changed the code to:

char *month = strtok(longDate, " ");
char *day = strtok(NULL, " ");
char *year = strtok(NULL, "\0");

but I am unsure how to assign the integer d the value of: year[2] = '/
0';
 
D

Default User

yogi_bear_79 said:
yogi_bear_79 wrote:
No, it wouldn't. The easiest way, given a modifiable buffer, would
be;

year[2] = '/0';
c = atoi(year+2);//returns the last two digits from char *year
d = atoi(year); //need to be modified to return the first two digits.

See directly above. That pokes a null-terminator into the third
character slot (safe as long as the string has at least a length of
two. That truncates it at that point.
I am in a distant learning enviornment, this is only a simple lab, the
input data is controlled, thus I know exactly what will be in my
string to start with. We are not to std:string yet, or even error
handleing. While I agree there are certainly better ways to do
things, in this instance this is what is required.

Ok.



Brian
 
Y

yogi_bear_79

yogi_bear_79 said:
yogi_bear_79 wrote:
No, it wouldn't. The easiest way, given a modifiable buffer, would
be;
year[2] = '/0';
c = atoi(year+2);//returns the last two digits from char *year
d = atoi(year); //need to be modified to return the first two digits.

See directly above. That pokes a null-terminator into the third
character slot (safe as long as the string has at least a length of
two. That truncates it at that point.
I am in a distant learning enviornment, this is only a simple lab, the
input data is controlled, thus I know exactly what will be in my
string to start with.  We are not to std:string yet, or even error
handleing.  While I agree there are certainly better ways to do
things, in this instance this is what is required.

Ok.

Brian

I hate to be thick headed but I still can not assign the integer d,
the first two digits from *year. I have

year[2] = '/0';

But all my syntax to convert that to an integer or assign it to any
variable type for that matter fails

Basicaly I have

*year which contains 4 digits. I need the integer d

to equal the first two digits, so I enter 2008, I want d to equal 20.
I know your probbaly shaking your head in amazment, but I am
essentialy self taught here!
 
D

Default User

yogi_bear_79 said:
On Mar 6, 6:45 pm, "Default User" <[email protected]> wrote:
I hate to be thick headed but I still can not assign the integer d,
the first two digits from *year. I have

year[2] = '/0';

But all my syntax to convert that to an integer or assign it to any
variable type for that matter fails

That was my mistake. It should be '\0'. Or just 0.




Brian
 
Y

yogi_bear_79

Default said:
year[2] = '/0';

Damn it! '\0', of course.

Brian



!!!!LOL, thanks...... This code seems prety simple to me as far as the
strtok goes, all the code I've looked at via std::string seemed way
more complicated to achieve the same results. Thanks for you
paitence..until the next lab!
 
D

Default User

yogi_bear_79 said:
Default said:
year[2] = '/0';

Damn it! '\0', of course.

Brian

Bummer, just when i thought I was G2G..this code:

d = (year[2] = '\0');

equates to 0 no matter what year!

Start with this:

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
char str[] = "1990";

int year = atoi(str);

cout << year << "\n";
str[2] = '\0';
year = atoi(str);
cout << year << "\n";

return 0;
}



Brian
 
Y

yogi_bear_79

yogi_bear_79 said:
Default User wrote:
year[2] = '/0';
Damn it! '\0', of course.
Brian
Bummer, just when i thought I was G2G..this code:
d = (year[2] = '\0');
equates to 0 no matter what year!

Start with this:

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
   char str[] = "1990";

   int year = atoi(str);

   cout << year << "\n";
   str[2] = '\0';
   year = atoi(str);
   cout << year << "\n";

   return 0;

}

Brian

ok. got it! You are a paitent tutor...
 
D

Default User

T

Thomas J. Gritzan

yogi_bear_79 said:
The entire code is:
char* nextToken;
int a, b, c, d;

char *month = strtok_s(longDate, " ", &nextToken);
char *day = strtok_s(NULL, " ", &nextToken);
char *year = strtok_s(NULL, "\0", &nextToken);

c = atoi(year+2);//returns the last two digits from char *year
d = atoi(year); //need to be modified to return the first two digits.

c = atoi(year) % 100; // the remainer of the division
d = atoi(year) / 100; // effectively cuts the last 2 decimals
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top