Thread: Createing a function with string?

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    100

    Createing a function with string?

    can someone show me the code to make a function that takes a string from the user and print it on the screen (using cout)

    (i know there is no point to this but i need it fro a whole different thing... a how do you count how many chars and spaces there are in the string... thanks

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    this is about as basic programming gets (besides basic math) ... check out the tutorial on this site.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    100
    wait... thats not what i meant... i mean kinda like this...

    Code:
    void functioninmydreams (string)
    {
    cout << string;
    }
    
    int main ()
    {
    functioninmydreams(pie is good);//in my dreams this would print pie is good
    }
    thats what i need

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    void PrintString(char* String)
    {
       cout << String;
    }
    
    PrintString("Hello");
    But it would be simplier (and faster) to use cout << "Hello"; directly without the use of such function.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    317
    here:

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    void showstring(char*);
    
    int main(){
    	showstring("Pie is good");
    	cin.get();
    	return(0);
    }
    
    void showstring(char* s){
    	cout<<s;
    }
    Check the tutorial for counting char and spaces. It's easy enough to follow.

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    100
    nm... i got it on my own... thanks anyway

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    here you go.

    Code:
    #include <iostream>
    #include <string.h>
    
    void printstring(char* str1){ cout<< str1;}
    
    int main(){
     cout<<" Enter any string to be printed";
     getline(cin, str1);
     printsting(str1); 
    
     return 0 ;// as we know.
     }// end main
    I think this will cover everything,
    C++
    The best

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: here you go.

    Originally posted by NANO
    I think this will cover everything
    Even typos
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Replies: 6
    Last Post: 04-21-2006, 08:49 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM