c++. how do I store a string value in an int variable?

piski

Senior member
Jan 21, 2002
312
0
0
I am prompting the user for the year (1992, 2001 etc.) From this i need to get the century. They only way that I can think is store it as a string and make a substring of the first 2 digits and then add 1. there must be another way but it is late and I am exhausted. If anyone can tell me how to do this or a better way to do this i would be very appreciative. thanks again
k
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
I don't really understand your explanation, but atoi() will convert a char* to an int.

#include <cstdlib>

....

string blah = "2003";
int foo = atoi(blah.c_str());

Or you can use a stringstream.

stringstream ss;
ss << "2003";
int blah;
ss >> blah;

Or there's boost::lexical_cast<>
 

piski

Senior member
Jan 21, 2002
312
0
0
for example.

the user inputs the year 1992


string year;
cin << year;
year.substr(0,2)

The user enters 1992 into the string var year. Then I make a substring of the first 2 numbers (19). I then store this 19 in an int and then add 1 to get the century

1900's are the 20th century. Hopefully that makes it a little easier to understand

thanks again
k
 

RemyCanad

Golden Member
Sep 28, 2001
1,849
0
0
So if they give you 1901 you want to store 20.

I would just do a simple < or > test.

Example

int date = 0;
int century = 0;

cout << "Please enter date: " << encl;
cin >> date;

if (date >= 2000){
century = 20; }


I am sure there is more compact way to do this. For example if you divide by 100 then take the floor of it and add one that should work...

Example

1999/100 = 19.99
floor(19.99) = 19
19+1=20.

I just don't know what kind of run time the floor function has...
 

onelin

Senior member
Dec 11, 2001
874
0
0
umm, how about you don't input from the user as string? You can easily break that up with modulus. :)
 

onelin

Senior member
Dec 11, 2001
874
0
0
ok, input as an int. say they enter 2003 and it is stored in a variable x.
century=x/100; // this will give you the 20
year=x%100; // this will give you the 03 (although it won't display the 0 unless you format it)

/ is obviously normal division, and % (modulus) is the remainder.

is that what you want?
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
cin >> year you mean :p

I would get it as an int first:

int year;
cin >> year;

and then if you need it in a string, use a stringstream instead:

#include <sstream>
....
stringstream century;
century << year;

then use century.str() to get a normal string out of the stringstream. As for getting the century from the year, numerically, I would do this:

int year = 1992;
int century = year/100 + 1;

That will have the same end result as cutting off the last 2 digits.
 

RemyCanad

Golden Member
Sep 28, 2001
1,849
0
0
int date = 0;
int century = 0;

cout << "Please enter date: " << endl;
cin >> date;

century = (date/100);
century = floor(century);
century++;
 

Replicon

Member
Apr 23, 2002
152
0
71
cmath? just read it in as an int and divide by 100... then add 1...

int x;
cin >> x;
cout << x/100 + 1;
 

onelin

Senior member
Dec 11, 2001
874
0
0
division gets the century just as easily, as I showed above :) +1 is needed of course, which I forgot.
 

piski

Senior member
Jan 21, 2002
312
0
0
yeah i think all of those will work. i am so tired i dont know why i didnt think of the division. Plus we haven't learned the sstream library yet and we need to use what we have learned. thanks a lot everyone. that really helped.
have a good night!
k
 

onelin

Senior member
Dec 11, 2001
874
0
0
g'night. Sometimes the simplest things are what we think of last! When parsing numbers a digit at a time (I seem to remember some recursive exercises in a class in C or Java) /, * and % by 10's are a great tool.
 

RemyCanad

Golden Member
Sep 28, 2001
1,849
0
0
True. I wasn't paying full attention when I wrote it.

As already stated the floor function is completely useless when using ints. I was thinking about it from the pure math prospective not the programming one.