Thread: String Input & Output

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    37

    String Input & Output

    Code:
    // Stopping input with an empty string
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    int main()
    {
        int i = 1;
        char temp[80];
    	char string[80]; // room for 255 strings
    	
    
        cout << "\n\nEnter some strings - (blank to exit)"
             << " \nfirst string: ";
        cin.getline(temp, 80);
        while (temp[0] != '\0')
        {
            cout << "String " << i << " : " << temp << "\nnext string: ";
            cin.getline(temp, 80);
            string == strcat(string," ");
    		string == strcat(string,temp);
    		i++;
    
        }
    
        cout << "\n\n";
        
    	//for (int index = 0; index < i; index++)
        cout << string ;
    
    }
    i updated the code

    input: bill loves to eat
    output: $ loves to eat

    $ is a weird character that show up. i don't know what it is.
    Last edited by nicz888; 12-16-2007 at 08:13 PM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Don't use char arrays unless you want to get into dynamic memory (At this level, I don't think you want to). Use std::string instead.

    Currently you have an array named array of char *'s. That means each element of the array isn't a char, but a pointer to a char. This could be fine, except your char *'s aren't pointing anywhere significant and it's amazing it works as well as it does. You have to allocate memory and then copy. Otherwise, you need a two dimensional char array to hold everything (and even that might not work). As I said earlier, std::string is probably the best way to go about it.

    Don't use void main(). Use int main().

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    Quote Originally Posted by MacGyver View Post
    Don't use char arrays unless you want to get into dynamic memory (At this level, I don't think you want to). Use std::string instead.

    Currently you have an array named array of char *'s. That means each element of the array isn't a char, but a pointer to a char. This could be fine, except your char *'s aren't pointing anywhere significant and it's amazing it works as well as it does. You have to allocate memory and then copy. Otherwise, you need a two dimensional char array to hold everything (and even that might not work). As I said earlier, std::string is probably the best way to go about it.

    Don't use void main(). Use int main().
    not sure what you by the std::string, can you give a example.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    i updated the code.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    i notice that i can change both line 20 and 21 to
    strcat_s(string, " ");
    and
    strcat_s(string, temp);
    that will get rid of all the warnings.

    and the result is the same.
    but my "bill" is still not showing up, why?
    Last edited by nicz888; 12-16-2007 at 08:13 PM.

  6. #6

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    ok i put the namespace after the #include.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by nicz888 View Post
    not sure what you by the std::string, can you give a example.
    http://www.cprogramming.com/tutorial/string.html

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    Quote Originally Posted by MacGyver View Post
    i kind of fix the concat string issue i had, now can you help me on the "bill" issue?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
            string == strcat(string," ");
    		string == strcat(string,temp);
    What's this supposed to do?
    == is comparison and not assignment. Plus strcat appends to the string for you; you don't have to assign.
    And it's still better if you change to std::string; this is C++, after all.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Replies: 4
    Last Post: 04-03-2007, 05:57 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM