![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 155
Rep Power: 3
![]() |
c++ string ... interesting...
so i am looking at a code snippet written by my prof.
and it says : int c; string s; .... s = c; ^^^ wouldnt that give data type mismatch? but how could my prof do it? |
|
|
|
|
|
#2 | |
|
Hobbyist Programmer
|
Quote:
|
|
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
No. The assignment operator for the string class is overloaded to accept that. Don't expect to be able to predict what goes in the string for large integers unless you're more of a mental mathematical-conversion whiz than I.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 155
Rep Power: 3
![]() |
Quote:
int main ()
{
int c = 5;
string s = "runsilol";
s = c;
cout << c;
cin.get ();
}^^ you know what it gave me?? clubs! (yeah the clubs of card games) |
|
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 155
Rep Power: 3
![]() |
well ... i am still confused
|
|
|
|
|
|
#6 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 155
Rep Power: 3
![]() |
Quote:
|
|
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 155
Rep Power: 3
![]() |
SymTab<Attributes> ST1;
int remWS() // result is first non-white space char
{int c;
c = cin.get(); cout.put(c);
if (isspace(c)) return remWS();
else return c;}
void appnd(char c, string & s) // appends c to the end of s
{string s1;
s1 = c;
s.append(s1);}
item<Attributes>* Scanner::get() // returns next token
{int c; // tokens: ( ) + - * / Int
string s;
item<Attributes>* x;
c = remWS();
s = c;
if (isdigit(c))
{c = cin.peek();
while (isdigit(c))
{appnd (c, s);
c = cin.get(); cout.put(c);
c= cin.peek();}
x = ST1.insert(s);
x->attr.type = Int;
x->attr.value = atoi(s.c_str());}
else {x = ST1.find(s); // ( ) + - * / prestored in ST1
if (x == NULL)
{ // another possibility is to scan
// until next white space with
// while (!isspace(c))
// {appnd(c, s);
// c = cin.get(); cout.put(c);}
x = ST1.insert(s); // s not ( ) + - * /
x->attr.type = error;}}
return x;}look at the function item<Attributes>* Scanner::get() what is s = c trying to do? |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Don't get your panties in a wad and don't expect us to be hanging on your thread with bated breath. Leave the monologues to Jay Leno. It assigns the value of c to the string. If c represents a printable character, then that's what you get. You would get 'B' if c were 66 and your system dealt in ASCII.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#9 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 155
Rep Power: 3
![]() |
Quote:
calm down first ... what i dont understand is how that code is using it ... like sometimes it would use c as a char as well ... and that doesnt make much sense ... but hey i am a programming noob oh btw if you dont wanna post in my thread, then nobody is forcing you bro |
|
|
|
|
|
|
#10 | ||
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Just make your question clear. Here's your original one:
Quote:
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
||
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|