Computer Science Canada

c++ String HEEEELP!!!

Author:  Homer_simpson [ Sun Jul 06, 2003 1:26 pm ]
Post subject:  c++ String HEEEELP!!!

wtf is wrong with this... Mad
code:
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
        char s[]="fassa" ;
        s= "abcde";
        cout << s[1]<<endl;
        return 0;
}

and this
code:
#include <string>
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
        string s[]="fassa" ;
        s= "abcde";
        cout << s[1]<<endl;
        return 0;
}

Author:  rizzix [ Sun Jul 06, 2003 1:47 pm ]
Post subject: 

the new ISO standards forbid the use of arrays to represent char* pointers.
this rule comes into play specially when u reassign the char[] a new string.
explictly specify char* pointers likewise
code:

#include <iostream>
using namespace std;

int main()
{
   char* s = "fassa" ;
   s = "abcde";
   cout << s[1] << endl;
   system("PAUSE");
   return 0;
}

Author:  rizzix [ Sun Jul 06, 2003 1:50 pm ]
Post subject: 

second...
code:

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

int main()
{
   string s = "fassa" ;
   s = "abcde";
   cout << s[1]<<endl;
   system("PAUSE");
   return 0;
}

Author:  Tony [ Sun Jul 06, 2003 3:19 pm ]
Post subject: 

for 2nd, string is an object Surprised and I think you access parts of it using

strVariable.substr(whatever)

Author:  SilverSprite [ Sun Jul 06, 2003 3:50 pm ]
Post subject: 

stringvariable[i] accesses the (i-1)'th character..

Author:  SilverSprite [ Sun Jul 06, 2003 3:54 pm ]
Post subject: 

also tony.. stringvariable.substr(i,j) prints out a substring starting at the (i-1)th character for j characters

Author:  Homer_simpson [ Sun Jul 06, 2003 4:06 pm ]
Post subject: 

thx for the codes above... i still have problems with the second one tho Confused...
but it's ok i have another question now...
let's say i have

char s="home";
and i want to change it to "homer"...
s[6]="r"; doesn't work...
what should i do... =/

Author:  octopi [ Sun Jul 06, 2003 4:40 pm ]
Post subject: 

I am really rusty, but...

code:
#include <iostream>
#include <string>

int main()  {
   char s[6]="home";
   s[4]='r';
   s[5]='\0'; 
   std::cout << s;
   return 0;
}


Is that what you want?

Author:  Homer_simpson [ Sun Jul 06, 2003 5:04 pm ]
Post subject: 

that gives me an error for some reason...

Author:  rizzix [ Sun Jul 06, 2003 5:24 pm ]
Post subject: 

Homer_simpson wrote:
thx for the codes above... i still have problems with the second one tho Confused...
but it's ok i have another question now...
let's say i have

char s="home";
and i want to change it to "homer"...
s[6]="r"; doesn't work...
what should i do... =/


k for the second what really happens... what are the results... what warnings does the compiler print... what errors do you get?

now for u next qtz..

first of all start counting from Zero for arrays in c/c++/java/and a hell a lot of other programming languages

code:

#include <iostream>
using namespace std;

int main()
{
    char *s= "home";
    cout << s;
    s = "homer";
    cout << s;
    system("PAUSE");
    return 0;
}

Author:  Homer_simpson [ Sun Jul 06, 2003 5:41 pm ]
Post subject: 

rizzix wrote:

now for u next qtz..

first of all start counting from Zero for arrays in c/c++/java/and a hell a lot of other programming languages

code:

#include <iostream>
using namespace std;

int main()
{
    char *s= "home";
    cout << s;
    s = "homer";
    cout << s;
    system("PAUSE");
    return 0;
}


well i know how to do what u just did... but i need add one character to the end of my string for example in this case "r" is being added to "home"... but "r" is not always the character i want to add to the end on my string... in fact i want the user to input a character and then i want to add it to the end of my string...
+10 bits to rizzix for his helps so far

Author:  krishon [ Sun Jul 06, 2003 5:47 pm ]
Post subject: 

whoa, what happened to ur name, lol....i think u changed the strength too much.

and bout the thing, dun't u just read in like

code:

include <iostream.h>
using namespace std;

int main()
{
char *s= "home";
char t
cout << "\nEnter a letter.\n " << endl
cin >> t
cout << "\nThe word u have formed is " << s << t << endl
system("PAUSE");
    return 0;
}

i'm not too sure..cuz i haven't used c++ in a while

Author:  rizzix [ Sun Jul 06, 2003 5:51 pm ]
Post subject: 

Homer_simpson wrote:


well i know how to do what u just did... but i need add one character to the end of my string for example in this case "r" is being added to "home"... but "r" is not always the character i want to add to the end on my string... in fact i want the user to input a character and then i want to add it to the end of my string...
+10 bits to rizzix for his helps so far



i'm a mod now dude Wink

try this .. i've noticed that i can't do it when i use char* pointer but only char[] arrays wierd. c++ is never implemented according to a standard so this worked on my mac hopefully it works on ur pc (if u use devc++ it will, since devc++ uses the port of the unix compiler g++)

code:

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

int main()
{
    char s[5] = "home";
    cout << s << endl;
    strcat(s, "r");
    cout << s << endl;
    //system("PAUSE"); // devc++ users un-comment this
    return 0;
}

Author:  UBC_Wiskatos [ Sun Jul 06, 2003 8:01 pm ]
Post subject:  Re: c++ String HEEEELP!!!

Homer_simpson wrote:
wtf is wrong with this... Mad
code:
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
        char s[]="fassa" ;
        s= "abcde";
        cout << s[1]<<endl;
        return 0;
}

and this
code:
#include <string>
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
        string s[]="fassa" ;
        s= "abcde";
        cout << s[1]<<endl;
        return 0;
}


I'm not sure what you want, but I'll just work on assumptions. I'm assuming you want to create a string that says something, then access one letter from it and return that letter. Here is one way of doing that which involves using an array (you initialized the array incorrectly in your example):

code:

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
        char s[]= {'f','a','c','d','e', '\0'};
   cout << s[1]<<endl;
   return 0;
}


This is a zero-delimited string, and it will output the character a. The other way to do this is to use a pointer to a char, and thus can be initialized more easily. This is how to do the example above using this method:

code:

int main(int argc, char* argv[])
{
        char *lpsz = "facde";
        char tempchar;

        for(int i=0;i<2;i++)
        {
                tempchar = *lpsz;
                lpsz++;
        }

   cout << tempchar <<endl;
   return 0;
}


I'm not sure what else you wish to do to a string, but that's basically the stuff you wanted originally. Here's how to add stuff to a string:

code:

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

int main(int argc, char* argv[])
{
        char str[80];
        strcpy (str,"hey ");

        cout << "Original Text: " << str << endl;

        char cool[] = "this is ";

        strcat (str,cool);
        strcat (str,"cool ");
       
        cout << "New Text: " << str << endl;

        return 0;
}

Author:  Homer_simpson [ Sun Jul 06, 2003 8:24 pm ]
Post subject: 

i'm sure as hell that strcat(s, "r"); is just what i want but for some friggin reason the exe file that's being made wont run.. i get some critical error crap... could some1 try
code:
#include <iostream>
#include <stdio.h>
#include <conio.h>

using namespace std;

int main()
{
        char* ch;
        ch="home";
        strcat(ch, "r");
        cout << ch<<endl;
        return 0;
}

on their compiler and see if it works or not... thx...
Quote:
i'm a mod now dude Wink

i already know that... but i dont think that mods are aloud to give free bits to them selves.. are they? Wink

Author:  UBC_Wiskatos [ Sun Jul 06, 2003 8:37 pm ]
Post subject: 

Homer_simpson wrote:
i'm sure as hell that strcat(s, "r"); is just what i want but for some friggin reason the exe file that's being made wont run.. i get some critical error crap... could some1 try
code:
#include <iostream>
#include <stdio.h>
#include <conio.h>

using namespace std;

int main()
{
        char* ch;
        ch="home";
        strcat(ch, "r");
        cout << ch<<endl;
        return 0;
}

on their compiler and see if it works or not... thx...
Quote:
i'm a mod now dude Wink

i already know that... but i dont think that mods are aloud to give free bits to them selves.. are they? Wink


Look at my example above, it shows you how to do it. I think your pointer stuff is messing your compiler up, it's tricky because you're adding one more character to the space in memory, and it's not as simple as just adding it (you need to reallocate memory and whatnot). What you need to do is make a char of a fixed size that can hold the size of the original string, plus the new string, that'll work.

Author:  Homer_simpson [ Sun Jul 06, 2003 11:36 pm ]
Post subject: 

aaaahhh...
strcpy and strcat were all i needed thanx man.. here have some bits =Þ...
+50 bits to UBC_Wiskatos

Author:  Homer_simpson [ Sun Jul 06, 2003 11:51 pm ]
Post subject: 

ok here's another question... is there a command that will turn an integer or a float to a string...?

Author:  SilverSprite [ Sun Jul 06, 2003 11:53 pm ]
Post subject: 

yes for chars its atoi and the related functions and you can also use sprintf

Author:  Homer_simpson [ Mon Jul 07, 2003 12:05 am ]
Post subject: 

and the correct answer is..........sprintf..
silversprite is the winner... have some bits boy Razz Razz Razz
+20 bits to silver
ty for your helps ppl...

Author:  UBC_Wiskatos [ Mon Jul 07, 2003 12:21 am ]
Post subject: 

Homer_simpson wrote:
aaaahhh...
strcpy and strcat were all i needed thanx man.. here have some bits =Þ...
+50 bits to UBC_Wiskatos


No prob! Smile

Author:  SilverSprite [ Mon Jul 07, 2003 4:01 am ]
Post subject: 

so why is it that i only have one circle on my name?? how do you get more?? by posting?? or by the more bits you have??

Author:  krishon [ Mon Jul 07, 2003 8:43 am ]
Post subject: 

wut circle??

Author:  Homer_simpson [ Mon Jul 07, 2003 1:24 pm ]
Post subject: 

I get more circles... cuz i'm a programming god!!! read my name title 8)

Author:  AsianSensation [ Mon Jul 07, 2003 1:35 pm ]
Post subject: 

SilverSprite wrote:
so why is it that i only have one circle on my name?? how do you get more?? by posting?? or by the more bits you have??


it's something wrong with the site, dan is trying to fix it though.

and your status is suppose to change as you post more, i think right now you should be a NewBe Hacker.

but it gets frozen somehow....and doesn't change....

well, im assuming the changes are going to be made once the new compsci.ca comes out.....assuming they will release info on that....

Author:  Tony [ Mon Jul 07, 2003 2:42 pm ]
Post subject: 

your rank gets frozen if you have title effects for your rank... Confused

its messed, lets just wait and see how v2 of the site will turn out.

Author:  rizzix [ Mon Jul 07, 2003 3:26 pm ]
Post subject: 

man i think dan has a lot on his shoulders,,
if we could only lessen the load for him... Confused

Author:  Mazer [ Mon Jul 07, 2003 5:33 pm ]
Post subject: 

rizzix wrote:
man i think dan has a lot on his shoulders,,
if we could only lessen the load for him... Confused


chop off his shoulders? Confused
that'd surely lessen the load... everything would fall off! but somehow i get the idea that it could cause more problems in the long run.

Author:  SilverSprite [ Mon Jul 07, 2003 6:15 pm ]
Post subject: 

hahahaha... oh wait..not very funny.. Razz


: