Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 15th, 2006, 4:57 PM   #1
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 155
Rep Power: 3 programmingnoob is on a distinguished road
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?
programmingnoob is offline   Reply With Quote
Old Apr 15th, 2006, 5:09 PM   #2
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: WA, USA
Posts: 343
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
Quote:

#include <iostream>
#include <string>
using namespace std;

int main()
{
int c = 66;
string s;

cout << "c = " << c << endl;

cout << "s is empty now" << endl;

s = c;

cout << "s is now = " << s << endl;

return 0;
}
ASCII
mrynit is offline   Reply With Quote
Old Apr 15th, 2006, 5:09 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Apr 15th, 2006, 5:19 PM   #4
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 155
Rep Power: 3 programmingnoob is on a distinguished road
Quote:
Originally Posted by mrynit
ASCII
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)
programmingnoob is offline   Reply With Quote
Old Apr 15th, 2006, 5:20 PM   #5
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 155
Rep Power: 3 programmingnoob is on a distinguished road
well ... i am still confused
programmingnoob is offline   Reply With Quote
Old Apr 15th, 2006, 5:21 PM   #6
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 155
Rep Power: 3 programmingnoob is on a distinguished road
Quote:
Originally Posted by DaWei
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.
so ... what does s = c do? i mean what value does it assign to s?
programmingnoob is offline   Reply With Quote
Old Apr 15th, 2006, 5:35 PM   #7
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 155
Rep Power: 3 programmingnoob is on a distinguished road
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?
programmingnoob is offline   Reply With Quote
Old Apr 15th, 2006, 6:00 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Apr 15th, 2006, 6:14 PM   #9
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 155
Rep Power: 3 programmingnoob is on a distinguished road
Quote:
Originally Posted by DaWei
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.
argh i got that ...

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
programmingnoob is offline   Reply With Quote
Old Apr 15th, 2006, 6:38 PM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Just make your question clear. Here's your original one:
Quote:
^^ wouldnt that give data type mismatch?
You got an answer, right? Then you got all excited, and after jabbering around like a pregnant woman in her first labor, decided you had another one:
Quote:
what does s = c do? i mean what value does it assign to s?
Then you got an answer to that, right? We can't help it if you don't know how to ask a question. Go read the "How to Ask a Question" thread, and follow that link in the Rules/FAQ to "How to ask questions the smart way." Meanwhile, don't tell me where to post. No one is forcing me, there may be other members who would like to see the answer, we didn't just come here to serve you. Go off and sit behind the barn and think a bit after you read up on things.
__________________
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:29 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC