-
Ah now you've provided code so its ok for us to give you some :)
Code:
for (index=0; string1[index] != '\0'; index++)
; // Empty body. We are just trying to get index to be at the null character
for (--index; // index was pointing at the null, we need to go to the previous character
index >= 0; // when index == 0 we are at the first character
index--) // step back to the previous character
cout << string1[index] << endl;
-
So close....
I haven't done it as a function yet, I usually try t get it working w/o using a function and then do the function...but I don't know why my output is doing funky things. I got it to output the word backwards...but it also does a bunch of weird characters. Another problem is if I type in a 5 letter word like 'angel' and then a 3 letter word like 'fie' it comes out eifna it is not clearing the memory after each run through. I thought the endl; cleared the buffer ? The last problem is in trying to break the WHILE loop. I have tried setting it to != '1' and it yells at me, I tried != 'x' and it errored...so for now I have left it at !='\0' but theres no way to escape the program. I am sorry for being such a bother. I can't even sleep because I have code swimming through my brain and until I get this fixed it is relentless....is that sad or what? :o
Output 2
Code:
//Heather Conn
//Program 7
//This program will read in a word of 1-10 letters and output the word reversed
#include <iostream>
#include <iomanip>
#include <fstream>
//Declare std
using std::cout;
using std::cin;
using std::endl;
using std::ios;
using std::cerr;
using std::ofstream;
//Start the program
int main ()
{
//Declare the variables
char string1[10];
int index;
int idx;
char reverse[10];
//Open the file for output
ofstream data_out ("output.txt", ios::out);
//Error routine if file can not be created
if (!data_out) {
cerr << "File could not be opened" << endl;
exit (1);
}
//Describe program to user
cout << "This progam will output any word consisting of 1-10 characters in reverse." << endl;
data_out << "This progam will output any word consisting of 1-10 characters in reverse." << endl;
//WHILE loop to loop program--This is not working but I am having trouble finding a value to end the loop
while (string1 != "\0"){
//Get word and output to file
cout << "\nPlease enter a word 1-10 characters in length: ";
cin >> string1;
data_out << "\nThe word is: " << string1 << endl;
//Initialize idx
idx=0;
//Set up loop
for (index=0; string1[index] != '\0'; index++); //starts at 0 and counts up till it finds '\0'
for (--index; index >= 0; index--) {
reverse[idx] = string1[index];
++idx;
}
//Output reversed text
cout << "The word reversed is: " << reverse << endl;
data_out << "\nThe word reversed is: " << reverse << endl;
//End WHILE
}
//End program
return 0;
}
-
The wierd characters are because you didn't add a null to the end of the string. Remember a string by definition is a series of characters terminated with a null.
Quzah.
-
In order for the strings to have 10 possible visible char and still have the null terminating char, each of these need to be 11 elements long, not 10.
char string1[10];
char reverse[10];
When you get done reading the char of string1 into reverse, you need to give it a null char to make reverse a string, not just a char array.
Code:
for (--index; index >= 0; index--)
{
reverse[idx] = string1[index];
++idx;
}
//reverse[idx] is now the first available "empty" index in reverse.
reverse[idx] = '\0';
-
*lightbulb* I forgot that since there was no carriage return associated with reverse that it wouldn't automatically have a null at the end.
Yall thanks for the help...strings/arrays are making a little more sense now. I don't know why this particular concept is so difficult for me, but at least now I can say I don't fall into the "as clear as mud" category.