Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
c++ type conversion
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
chammer
n00b
n00b


Joined: 19 May 2003
Posts: 43
Location: Newport News, VA

PostPosted: Sun May 25, 2003 7:40 pm    Post subject: c++ type conversion Reply with quote

been a very long time since i've programmed in c/c++, but im back at it. have a problem though with some type conversions. basically my code looks like this... (doing qt/kde programming btw)...

string local_line = line; /* line gets passed to the sub via void blah (char *line) */
string lcd_text = local_line.substr (12, 4);

lCDNumber21->display (lcd_text); /*has to be const QString*/

this all works fine. the problem then becomes...

/* this doesnt work, it errors out no matter how many differnt type conversions i try */
progressBar11->setProgress (lcd_text); /* has to be int */

i've tried what i thought would be the right way ala:

int blah = atof (lcd_text);

but this fails. also i've tried making lcd_text not a string, but a char *...this fails trying to assign the substr to it, however. tried dozens of different ways, with no luck.

i know this is very sparse, but any ideas?
_________________
http://www.nasland.nu
echo '[q]sa[ln0=aln256%Pln256/snlbx]sb3135071790101768542287578439snlbxq' | dc
echo '16i[q]sa[ln0=aln100%Pln100/snlbx]sbA0D4D465452snlbxq' |dc
Back to top
View user's profile Send private message
dma
Guru
Guru


Joined: 31 Jan 2003
Posts: 437
Location: Charlotte, NC, USA

PostPosted: Sun May 25, 2003 7:52 pm    Post subject: Reply with quote

line.c_str() should make it a (char *)

Code:
int stuff = atoi(lcd_text.c_str());


(I'm presuming you mean atoi instead of atof...)

Docs for qstring:
http://doc.trolltech.com/3.1/qstring.html

Docs for std::string:
http://www.dinkumware.com/htm_cpl/string2.html
http://www.msoe.edu/eecs/ce/courseinfo/stl/string.htm
Back to top
View user's profile Send private message
chammer
n00b
n00b


Joined: 19 May 2003
Posts: 43
Location: Newport News, VA

PostPosted: Sun May 25, 2003 8:56 pm    Post subject: thanks... Reply with quote

thanks for the qstring doc, it actually lead me to something. since i usually try first thing that comes to mind versus thinking about doing something the right way (only 75% of the time :D) i ended up just copying what i needed to QString. so...

string blah = hello.substr (12, 4)
..etc etc..

QString temp = blah;
double d = atof (temp);
int i = atoi (temp);

i needed the double for the float (maybe i can just use an int instead?...its been way to long...).

anyways, this is what i was building, i was stuck on that problem for the past 3 hours while searching/researching via google and my c++ books. glad i stopped here though. anyways...

[img:53c5e15ecd]http://www.nasland.nu/nst.png[/img:53c5e15ecd]

(the progress bars are based off a set limit. 65C in this case. just to give you another way of interpreting the degrees.)

was initially going to do this in QtPerl (im a perl monger :D) or GTK (i also love GTK), but i figured should anyone want this it'd prolly be best to be as native as possible and i've yet to build a Qt/KDE application in all my years under a Unix flavor. its built around lm_sensors, so...as long as you have lm_sensors and qt...it should run. if anyone wants to give it a try let me know and i'll tar up the source and upload it to me site.
_________________
http://www.nasland.nu
echo '[q]sa[ln0=aln256%Pln256/snlbx]sb3135071790101768542287578439snlbxq' | dc
echo '16i[q]sa[ln0=aln100%Pln100/snlbx]sbA0D4D465452snlbxq' |dc
Back to top
View user's profile Send private message
chammer
n00b
n00b


Joined: 19 May 2003
Posts: 43
Location: Newport News, VA

PostPosted: Mon May 26, 2003 12:23 am    Post subject: Reply with quote

updated it a bit in case anyone is interested...

[img:e715ce3dfd]http://www.nasland.nu/nst4.png[/img:e715ce3dfd]

added the fan speeds and removed the progress bars (they were only getting in the way) :D
_________________
http://www.nasland.nu
echo '[q]sa[ln0=aln256%Pln256/snlbx]sb3135071790101768542287578439snlbxq' | dc
echo '16i[q]sa[ln0=aln100%Pln100/snlbx]sbA0D4D465452snlbxq' |dc
Back to top
View user's profile Send private message
far
Guru
Guru


Joined: 10 Mar 2003
Posts: 394
Location: Stockholm, Sweden

PostPosted: Sat May 31, 2003 9:42 pm    Post subject: Reply with quote

A more modern way to convert a string to a number:
Code:
#include <sstream>
#include <iostream>

int main() {
  using namespace std;
  stringstream foo;
  string bar("4711");
  int baz;
  foo << bar;  foo >> baz;
  cout << bar << " == " << baz << endl;
  return 0;
}

_________________
The Porthole Portage Frontend
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum