Thread: Using arrays for makeing strings??

  1. #1
    Registered User Machewy's Avatar
    Join Date
    Apr 2003
    Posts
    42

    Question Using arrays for makeing strings??

    Here is an example program I just made. Please look at it.

    PHP Code:
    #include <iostream.h>

    int main()

    {
     
    char quit;
     
     
    quit '/0';
     while (
    quit != 'q')
     {
      
    char string1[256];

      
    cout<<"Hello my name is SideShow Bob.";
      
    cout<<" What is your name?";

      
    cin.getline(string1256'\n');
      
      
    cout<<" I'm please to meet you, ";
      
    cout<<string1<<"."<<endl;

      
    cout<<" Type 'q' to quit. "<<endl;
      
    cin>>quit;
     }
      
     return 
    0;

    Ok, as you can see, it looks like a very complicated version of making a string for the users input.
    Is there any easier way of doing this???

    "All things come to an end"

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    you are using a c-string. that is fine. you could also use std::string, you'll need to #include <string>

  3. #3
    Registered User sikamikaniko's Avatar
    Join Date
    Mar 2003
    Posts
    28
    Yes. Try this:
    Code:
    cin >> string1;
    cout << string1;

  4. #4
    Registered User Machewy's Avatar
    Join Date
    Apr 2003
    Posts
    42
    It doesn't work...
    I did this:
    PHP Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <string>
    #include <string.h>
    int main()
    {
     
    std::string1;
     
    cin>>string1;
     
    cout<<string1;
     
    system("PAUSE");
     return 
    0;

    Why??
    "All things come to an end"

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Code:
    #include <string>
    #include <iostream>
    //you don't need to #include <string.h> since you #include <string>
    
    int main()
    {
      std::string string1; //you need an object
    
      std::cin >> string1;
      std::cout << string1;
    
      return 0;
    }
    edit: forgot end code tag.

  6. #6
    Registered User sikamikaniko's Avatar
    Join Date
    Mar 2003
    Posts
    28
    Code:
    #include<iostream.h>
    main()
    {
    char ln[256];
    cin >> ln;
    cout << ln;
    return 0;
    }
    Machewy, it is working just fine on my PC. I am using V studio 6.
    Try this

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by sikamikaniko
    Code:
    #include<iostream.h>
    main()
    {
    char ln[256];
    cin >> ln;
    cout << ln;
    return 0;
    }
    Machewy, it is working just fine on my PC. I am using V studio 6.
    Try this
    he wanted to know if there was another way to do this other than using a char array

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    33
    Originally posted by Machewy
    It doesn't work...
    I did this:
    PHP Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <string>
    #include <string.h>
    int main()
    {
     
    std::string1;
     
    cin>>string1;
     
    cout<<string1;
     
    system("PAUSE");
     return 
    0;

    Why??
    Just another way to do it is as follows:
    PHP Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <string>

    using namespace std;

    int main()
    {
     
    string string1;
     
    cin>>string1;
     
    cout<<string1;
     
    system("PAUSE");
     return 
    0;


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  2. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  3. working with strings arrays and pointers
    By Nutka in forum C Programming
    Replies: 4
    Last Post: 10-30-2002, 08:32 PM
  4. strings or character arrays
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 07-21-2002, 10:55 AM
  5. Searching arrays for strings
    By Zaarin in forum C++ Programming
    Replies: 14
    Last Post: 09-03-2001, 06:13 PM