So I made a char array and I have to make a pointer. The pointer moves through the array to print out a line of text. I need to make a for loop for this. How do I use '\0' in order to accomplish this or am I way off here?
^^ remember that \0 char is just the integer 0 and that a char is just a 1 byte int type. so char x = 0; is the same as char x = '\0'; Now recall that 0 is false, else true, in c++. so in the above, *p means that *p is true, which means that *p is not zero...
/***************************************************************************************/
//Function displays what the user is prompt to enter.
void printOriginal(char words[])
{
char *wordsptr = words;
for(int = 0; i < 256; i++)//stuck here but you see what I mean.
{
}
}
/***************************************************************************************************************/
void printReverse(char words[])//Prints data backwards
{
char *wordsptr = words;
cout<<"Reverse Order"<<endl;
for(int i = 0; i < '\0'; i++)
{
words--;
cout<<words[i]<<endl;
}
}
/**********************************************************************************************************************/
Oh ok, what would you change it to then? I hate asking for help. I just want to learn this stuff. I do appreciate the help. I guess I don't know what question to ask. I have been learning the for loop like that. Perhaps I should show you my code as a whole:
/*Preprocessors*/
#include <iostream> //includes basics
#include <string>
#include <cstring>
#include <iomanip>
#include <cstdlib>
//Chris Rachal ITP 232 Lesson 3 assignment
usingnamespace std;
//Function Prototypes
void getText(char words[]);
void printOriginal(char words[]);
void printReverse(char words[]);
int main()
{
/****************************************************************************
*This is such a fun class. I really enjoy learning all there is to know about
*C++. Array stores this text.
*****************************************************************************/
char words[256];//Array to hold text
getText(words);//Calls the function to store data into the array.
printOriginal(words);
printReverse(words);
return 0;
}
//function definitions
//Function definition to have the user type text and store it into the array.
void getText(char words[])
{
cin.getline(words, 256);//saves the data entered by the user.
}
/***************************************************************************************/
//Function displays what the user is prompt to enter.
void printOriginal(char words[])
{
char *wordsptr = words;
for(int i = 0; i < strlen(words); i++))
}
/***************************************************************************************************************/
void printReverse(char words[])//prints data in reverse
{
char *wordsptr = words;
cout<<"Reverse Order"<<endl;
for(int i = 0; i < strlen(words); i++)
{
words--;
cout<<words[i]<<endl;
}
}
/**********************************************************************************************************************/
I know if doesn't look like much, but I work on it every day lol. I start erasing stuff and going back over it again and again.
My advice would be to slow down and make sure your syntax is correct before anything else.
Your loop on line 49 still has an extra right parentheses, ). And the for loop has no body.
Give it a body
1 2 3 4
for (int i = 0; i < strlen(words); i++)
{
// print wordsptr[i]
}
For lines 60 - 63, I would remove line 62, and instead of starting from int i = 0, reverse the direction you're iterating for:
- start at an index i of strlen(words) - 1
- loop on i >= 0
- decrement (as opposed to increment) i each loop.
Thanks @Ganado I'm trying. You guys make it seem like second nature. I erased the one that had no body, because I couldn't figure that part out. I did at first but once. I started working on the reverse. It didn't compile right.
That's very close.
1. Declare i within your for loop (int i instead of just i).
2. The second statement of the for loop is the condition to keep looping on.
It is what must be true to loop again.
3. Don't put a semi-colon at the end of the for loop. Put a body { }
i is initially some positive number, so it needs to loop on i being greater or equal to 0.