Not understanding string array & do while loop together
| gem925 (14) | |||
| 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; } | |||
| bnbertha (243) | ||||||
The condition on your do .. while
In a do while the while condition has to be true to go around the loop again. Your condition
your line
is creating an array called string which is an array of 6 int's I suspect you want something like:
which is an array of 6 strings called str_array In the do while loop you are indexing the array using the counter variable
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 | ||||||
| gem925 (14) | |||
| 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. | |||
| bnbertha (243) | |||
| 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 | |||
This topic is archived - New replies not allowed.
