This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-01-05 12:10:48

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Problems With Types in C++

Okay, so I'm working on a C++ program meant to help Python and Ruby programmers learn C++, and I'm running into some trouble with the Ruby to_i and to_f functions.  Here's my code:

Code:

int to_i(string number)
{
    try
    {
        int i;
        std::stringstream(number) >> i;
        return (i);
    }
    
    catch(...)
    {
        throw "Function to_i requires string.";
    }
}

What I'm trying to do is make it so that to_i can take both strings and floats.  Is there some way I can do this, or do I have to make a different function?  Also, is it possible to make this work like in Ruby, so instead of typing

Code:

to_i(myString);

you can type

Code:

myString.to_i();

?

Last edited by maxskywalker (2012-01-05 12:12:00)

Offline

 

#2 2012-01-05 12:56:09

poopo
Scratcher
Registered: 2009-09-20
Posts: 1000+

Re: Problems With Types in C++

You'd have to make a whole new variable type that has that function like(I don't know C++ so it'll be kinda Pseudo):
pooposvar:
int to_i(){
    this.to_i();
}
other class:
pooposvar var = new pooposvar("hi");
var.to_i();
I think this should work.  smile


http://i45.tinypic.com/28rnqki.jpg

Offline

 

#3 2012-01-05 13:35:19

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Problems With Types in C++

poopo wrote:

You'd have to make a whole new variable type that has that function like(I don't know C++ so it'll be kinda Pseudo):
pooposvar:
int to_i(){
    this.to_i();
}
other class:
pooposvar var = new pooposvar("hi");
var.to_i();
I think this should work.  smile

Hm, not understanding this.  Do you know any other languages that you could write it in?

Offline

 

#4 2012-01-05 15:58:59

roijac
Scratcher
Registered: 2010-01-19
Posts: 1000+

Re: Problems With Types in C++

you'll have to define the class myString and define the method to_i in it, don't you?

Offline

 

#5 2012-01-05 17:14:58

samtwheels
Scratcher
Registered: 2011-03-20
Posts: 1000+

Re: Problems With Types in C++

define two methods: one called to_i(string mystring), and one called to_i(float myfloat)
you have to program them separately, though.

Offline

 

#6 2012-01-05 18:16:26

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Problems With Types in C++

samtwheels wrote:

define two methods: one called to_i(string mystring), and one called to_i(float myfloat)
you have to program them separately, though.

And C++ will figure out which is which for me?  Cool!  It's not that long a function, and if it was, I could use goto.

Offline

 

#7 2012-01-05 18:50:12

stevetheipad
Scratcher
Registered: 2011-08-06
Posts: 1000+

Re: Problems With Types in C++

You should ask Cheddargirl, she knows C++


http://i.imgur.com/0x8ia.jpg
gone

Offline

 

#8 2012-01-06 11:47:26

poopo
Scratcher
Registered: 2009-09-20
Posts: 1000+

Re: Problems With Types in C++

maxskywalker wrote:

poopo wrote:

You'd have to make a whole new variable type that has that function like(I don't know C++ so it'll be kinda Pseudo):
pooposvar:
int to_i(){
    this.to_i();
}
other class:
pooposvar var = new pooposvar("hi");
var.to_i();
I think this should work.  smile

Hm, not understanding this.  Do you know any other languages that you could write it in?

What I mean is that there would have to be a method in the String class that was to_i()
for it to be called as:
myString.to_i();
instead of:
to_i(myString);
So you could add this code to the String class(this would be how it would be in Java):
public String to_i()
{
    return myClass.to_i(this);
}
So you call it like this:
String myString = "2";
int myInt
myInt = myString.To_i();

That should work I think.


http://i45.tinypic.com/28rnqki.jpg

Offline

 

#9 2012-01-06 12:26:23

maxskywalker
Scratcher
Registered: 2008-01-27
Posts: 1000+

Re: Problems With Types in C++

poopo wrote:

maxskywalker wrote:

poopo wrote:

You'd have to make a whole new variable type that has that function like(I don't know C++ so it'll be kinda Pseudo):
pooposvar:
int to_i(){
    this.to_i();
}
other class:
pooposvar var = new pooposvar("hi");
var.to_i();
I think this should work.  smile

Hm, not understanding this.  Do you know any other languages that you could write it in?

What I mean is that there would have to be a method in the String class that was to_i()
for it to be called as:
myString.to_i();
instead of:
to_i(myString);
So you could add this code to the String class(this would be how it would be in Java):
public String to_i()
{
    return myClass.to_i(this);
}
So you call it like this:
String myString = "2";
int myInt
myInt = myString.To_i();

That should work I think.

Oh.  Thanks.

Offline

 

Board footer