How do I get user input into a string array then display the data entered?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dbarten1982
    New Member
    • Feb 2010
    • 8

    How do I get user input into a string array then display the data entered?

    I am brand new to C++ programming, and am completely stuck on a program.
    In the program I am supposed to create a string array that stores 5 user input words, and the string constant "END_OF_ARR AY' in the last element. Then using the substr() display the first and third letter of each element. What I have now gets five word from the user, then it just displays the 1st and 3rd letter of the constant "END_OF_FIL E" I don't know if the problem is in the array itself or in the loop used to display the letters. Any help would be great!

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {	
    	
    	string word;
    	string words[6]; //initializes array
        words[5] = "END_OF_ARRAY"; // sets "END_OF_ARRAY" to las element
    	int i;
    
    	cout << "Type 5 words" << endl;
                    //Loop to get 5 words from user
    	for(i = 0; i < 5; i ++)
    	    cin >> word;
    	    word = words[i];
    		
    
    	cout << endl;
                    //Loop to display data from array
    	do
    	{
    	    
    		cout << words[i].substr(0,1) << " " << words[i].substr(2,1) << endl;//substr() to get 1st and 3rd letter
    	    
    
    	}while(i = 0, i < 5, i ++);
    
    	cin.get();
    	cin.get();
    
    	return 0;
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Line 16 - 18: Looks like you meant both lines 17 and 18 to be part of the for loop code block but without { } only line 17 is executed as part of the for loop.

    Line 18: I very much doubt that is what you mean, assigning an uninitialised string to the string containing the user input you just got.

    Line 29: You have tried to use a do while loop as though it was a for loop. It is not so this wont work. You enter the loop with i uninitialised, the only reason this doesn't result in an out of bounds array access is that you have the extra "END_OF_FIL E" entry. It appears that a simple for loop is what you require in place of that do while loop. In general you should only use a do while loop if you have a specific reason that you are able to illiterate as to why a while or for loop will not do the job.

    Comment

    • Dbarten1982
      New Member
      • Feb 2010
      • 8

      #3
      Thank you for a quick response. The only reason I am using a do...while loop is because that is what my instructor is looking for. "Using a do…while() loop, print out the 1st and 3rd letters of each word (two letters per line) using the substring function." What I have does that for the last element "END_OF_ARR AY", so is the problem printing this out in the do...while loop or in the way the data is stored into the array from the user?
      I completely overlooked the curly braces on the first "for" loop, thank you for pointing that out.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        OK well a do while loop only takes one expression (like a while loop and unlike a for loop). In a do while loop the expression is the end condition for the loop, the loop ends if the result of the expression is 0.

        You have clearly tried to put the 3 expressions of a for loop into your do while, those expressions are

        for( <InitialExpress ion>; <TestExpression >; <IterationExpre ssion>)

        Check you text books / documentation for how to for the various loops.

        Comment

        • Dbarten1982
          New Member
          • Feb 2010
          • 8

          #5
          Our text book doesn't cover string arrays in much detail, and up until now I've only had to work with int arrays. As someone wanting to learn to learn this I am thankful that you didn't just give me a line of code that works. By just pointing me in the right direction and letting me solve this for myself I feel is the best way to learn from my mistakes. Than you once again!

          Comment

          • whodgson
            Contributor
            • Jan 2007
            • 542

            #6
            Note Banfa`s comment concerning L18
            If you want the array to contain word the line should read array[i]=word not the other way round.
            Your code is not that far off the pace. Just make the suggested changes and it should be ok.

            Comment

            Working...