Not understanding string array & do while loop together

This is a project that I currently working on this is where I am struggling trying to put this together the string array with a do..while loop! Listed below under next add code to 5 words etc....What is throwing me off is putting together the string and loop part together. I have no programming experience this is my first time ever and boy talk about am I lost. Any assistance in this area would be so greatly appreciated.



//First, write a simple program that will read 5 numbers from the Console (user input). Store these values into an array of int []. Using the length of the array in a for() loop and decrementing the loop counter, write the array values in reverse order to the screen.


#include <iostream>
#include <string>

using namespace std;

const int ARRAY_SIZE = 6;

int main()

{
int array[5];
int counter;
int i = 0;
int string[ARRAY_SIZE];


cout << "Please enter five numbers: ";


for (counter = 0; counter < 5; counter++)
{
cin >> array[counter];

}

cout << endl;


cout << "Counting down the numbers: ";

for(counter = 0; counter < 5; counter = counter++)
cout << array[counter] << " ";

cout << endl;



cout << "The numbers in reverse order are: ";

for (counter = 4; counter >= 0; counter--)
cout << array[counter] << " ";

cout << endl;

________________________________________________________________________________
//Next, add code to read 5 words from the Console (user input). Store these values into an array of string []. Make sure the string[] array is large enough to hold at least 6 values. Store the string constant “end_of_array” into the last element of the array. Using a do…while() loop, print out the 1st and 3rd letters of each word (two letters per line) using the substring function.


cout << "Please enter five words: ";

for (counter = 0; counter < 6; counter++)
{
cin >> string[counter];

}

cout << endl;
do
{
cout << "Print out 1st letter for each word: ";
cout << string[counter]<< " ";

}while(string <= 6);

cout << endl;


//Finally, modify the above program to read the numbers in from a text file (numbers.txt), write the values in reverse order to a second file, and read the words in from a third file, displaying the results to the Console as described above.



return 0;

}

The condition on your do .. while
} while (string <=6)
is not going to work. In a for loop the last paramater (counter++) increments the counter variable so that eventually the middle parameter (counter < 6) will evaluate to false and the program will stop going around the loop.
In a do while the while condition has to be true to go around the loop again.
Your condition
String <= 6
makes no sense as string is an array of integers, which I suspect you don't want.....

your line
int string[ARRAY_SIZE];

is creating an array called string which is an array of 6 int's I suspect you want something like:

string str_array[ARRAY_SIZE];

which is an array of 6 strings called str_array

In the do while loop you are indexing the array using the counter variable
cout << string[counter]<<" ";
but you are not changing the value of counter in the loop so each time round it will write cin to the same string.
You need to put code in the loop to increment counter. Once you've done this then you can use in the while condition to test it for being less than 6. One point for good programming practice would be to use ARRAY_SIZE for this test rather than hard coding the number 6. That goes for the for loop as well.


Hope this helps


Bertha
Thank you Bertha. Where I am really struggling with is the finally part with creating text file.

//Finally, modify the above program to read the numbers in from a text file (numbers.txt), write the values in reverse order to a second file, and read the words in from a third file, displaying the results to the Console as described above.
Use the fstream objects. You want the ifstream to read from, and the ofstream to write to files. You use them in a similar way to cin and cout. There are loads of tutorials and examples of their use all over the internet.

Bertha
Topic archived. No new replies allowed.