Simple Question C++ Strings

This is a discussion on Simple Question C++ Strings within the C++ Programming forums, part of the General Programming Boards category; A quick question... How to add one string to another in c++, example.... "The Cat" "The Dog" "The Mouse" I ...

  1. #1
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    121

    Simple Question C++ Strings

    A quick question... How to add one string to another in c++, example....

    "The Cat"
    "The Dog"
    "The Mouse"

    I want to add these strings together to get one string...

    "The Cat\nTheDog\nTheMouse"

    This is what i want to do in psedo-code style...

    Code:
    string one;
    string two;
    string three;
    string output;
    
    output = ???
    
    return output;

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Code:
    #include <string>
    #include <iostream>
    
    int main(void)
    {
    	std::string s1 = "The Cat",
    				s2 = "The Dog",
    				s3 = "The Mouse";
    
    	
    	std::string out = s1 + '\n' + s2 + '\n' + s3;
    
    	std::cout << out << std::endl;
    
    	return 0;
    }

  3. #3
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    121
    Thanks!!! ...Just one more quick question... How do I do a string compare in C++, because its an object, can i just do...

    Code:
    str::string1;
    str::string2;
    
    if (string1 == string2) {
      return 0;
    }
    else {
      return 1;
    }

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Code:
    str::string1;
    str::string2;
    Yes! ...except for how you've declared the variables.
    Last edited by hk_mp5kpdw; 05-05-2006 at 05:06 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    121
    HA!!! Oh yeah!!! ...typo error!! Cheers!!!!

  6. #6
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Code:
    std::string out = s1 + '\n' + s2 + '\n' + s3;
    That should be

    Code:
    std::string out = s1 + "\n" + s2 + "\n" + s3;
    or am I wrong?
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  7. #7
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    It's a newline character, either way is fine.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Operators are overloaded for both chars and char arrays, in std::string. I believe using char is actually faster, since you're only passing (and copying and stack-pushing) a byte, and the string doesn't need to iterate over the chars to find the elusive '\0'.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21