dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Using cin and string array... Problems

  1. #1
    Join Date
    Oct 2008
    Posts
    17

    Using cin and string array... Problems

    I have this program and it is outputing odd characters after I enter sentances in the console.

    Code:
    int main()
    {
    	char myString[SIZE];
    	
    	cout << "Please enter a paragraph of sentances: " << endl;
    	cin >> myString[SIZE]; 
    	
    	cout << myString << endl;
    How can I change my code so that the string will out put the right letters?

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,561

    Re: Using cin and string array... Problems

    SIZE is how many elements the array contains. Valid indexes for the array are 0 through SIZE - 1. You'll need to set up a for loop incrementing a value used as an index each time through the loop and putting a sentence into the array at that index.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Using cin and string array... Problems

    Here's what you are currently doing....

    Code:
    int main()
    {
    	char myString[SIZE];// Declare a single C-style string which can hold a maximum of SIZE-1 characters.
    	
    	cout << "Please enter a paragraph of sentances: " << endl;
    	cin >> myString[SIZE]; // myString[SIZE] is not a valid index. However, the type checker doesn't know that; it sees the type here as char. Therefore, this statement reads a single character from cin and puts it in invalid memory.
    	
    	cout << myString << endl;// The char array is still uninitialized since you didn't change any valid part of it. Therefore, cout will print the random characters present (possibly beyond the valid bounds of the array) until it encounters a 0 byte.
    How can I change my code so that the string will out put the right letters?
    Well, that depends on exactly what you're trying to do. If you want each read a line full of words and then process the words individually you could do
    Code:
    #include <string>
    #include <sstream>
    
    using std::string;
    using std::stringstream;
    
    int main()
    {
            string myString;
    	
    	cout << "Please enter a paragraph of sentences: " << endl;
            getline(cin,myString);
            stringstream strm(myString);
    	
            while (strm >> myString)
            	cout << myString << endl;
            return 0;
    }

  4. #4
    Join Date
    Jan 2006
    Location
    Belo Horizonte, Brazil
    Posts
    405

    Re: Using cin and string array... Problems

    As Lindley's example show you should be using std::string for C++ programs. But if for some reason you need to use a char array, be careful when applying std::cin to it. If the user inputs a string larger than your buffer you'll get a buffer overflow. That's not good.

    A more secure way to use a char array with std::cin would be like this:

    Code:
    char myString[SIZE];
    cout << "Please enter a string: " << endl;
    cin >> setw(SIZE) >> myString;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)