Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 c++ String HEEEELP!!!
Index -> Programming, C++ -> C++ Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Homer_simpson




PostPosted: 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;
}
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Sun Jul 06, 2003 1:47 pm   Post subject: (No 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;
}
rizzix




PostPosted: Sun Jul 06, 2003 1:50 pm   Post subject: (No 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;
}
Tony




PostPosted: Sun Jul 06, 2003 3:19 pm   Post subject: (No subject)

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

strVariable.substr(whatever)
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
SilverSprite




PostPosted: Sun Jul 06, 2003 3:50 pm   Post subject: (No subject)

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




PostPosted: Sun Jul 06, 2003 3:54 pm   Post subject: (No subject)

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




PostPosted: Sun Jul 06, 2003 4:06 pm   Post subject: (No 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... =/
octopi




PostPosted: Sun Jul 06, 2003 4:40 pm   Post subject: (No 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?
Sponsor
Sponsor
Sponsor
sponsor
Homer_simpson




PostPosted: Sun Jul 06, 2003 5:04 pm   Post subject: (No subject)

that gives me an error for some reason...
rizzix




PostPosted: Sun Jul 06, 2003 5:24 pm   Post subject: (No 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;
}
Homer_simpson




PostPosted: Sun Jul 06, 2003 5:41 pm   Post subject: (No 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
krishon




PostPosted: Sun Jul 06, 2003 5:47 pm   Post subject: (No 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
rizzix




PostPosted: Sun Jul 06, 2003 5:51 pm   Post subject: (No 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;
}
UBC_Wiskatos




PostPosted: 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;
}
Homer_simpson




PostPosted: Sun Jul 06, 2003 8:24 pm   Post subject: (No 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
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 29 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: