Menu

Help with strings

2002-10-18
2012-09-26
  • Nobody/Anonymous

    I need some help with stringing...it seems that "string" itself isn't a keyword and i tried including string

     
    • Nobody/Anonymous

      Maybe others; but here is just one way...

      // Creates a file and prints the string you type into it.
      #include <iostream>
      #include <fstream> 
      #include <cstring> // ... For 'strcmp' ect
      #include <cstdlib> // ... For system("PAUSE")

      using namespace std;

      int main()
      {
         ofstream outfile; 

         char word[200]; // Char array name 'word'
                        
         outfile.open("file.txt", ios::out); 

         do
         {
         cout << "Type a word.  ";
         cin.getline(word,200);
         outfile << word <<endl;
         }
        
        
         while(strcmp(word, "" ) != 0); // strcmp compares two strings: Uses <cstring>
         outfile.close();

          system("PAUSE");
         return 0;
      }

      j@ck_

       
    • Nobody/Anonymous

      Another way ?...

      Smaller and simple.

      // Creates a file and prints the string you type into it.
      #include <iostream>
      #include <fstream> 
      #include <cstring>
      #include <cstdlib>

      using namespace std;

      int main()
      {
         ofstream outfile; 

          string myBff; // Enter single line with no spaces...             
           outfile.open("file.txt", ios::out); 

         cout << "Type a word:  ";
         cin >> myBff;
        
            outfile << myBff <<endl;
            outfile.close();

         system("PAUSE");
         return 0;
      }

      j@ck_

       
    • Nobody/Anonymous

      Thanks a bunch

       
    • Nobody/Anonymous

      #include<string>

      string  xxx;

      cin>>xxx;

      // using strings in your program

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.