| | |
backward string
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2005
Posts: 47
Reputation:
Solved Threads: 0
Hi, I'm trying to write a program that accepts a string (a line, sentence, or phrase) and then the program has to display the contents of that string backward. for example, if I entered "starting out" the program has to convert it to "tuo gnitrats".
This is my code. But I don't know what's wrong with it.
can anyone help please?
This is my code. But I don't know what's wrong with it.
C++ Syntax (Toggle Plain Text)
//project #2 backward String (follow page 515, 519) //write a function that accepts a pointer to a C-string as an argument and displays its contents backwards. # include <iostream.h> //function Prototype void Backward (char *); void main (void) { char line[201]; cout << "This program will display the contents of the entered phrase backward.\n"; cout << "Please enter a phrase of no more than 200 characters, followed by a period.\n"; cin.getline(line, 201); cout << "The entered string displayed backward: \n"; cout << Backward (line) << endl; } void Backward (char *sentencePtr) { char *sentencePtr = set; while (*sentencePtr != '\0' && *sentencePrt > set) { sentencePtr--; cout << *sentencePtr << endl; //or return sentencePtr; } }
can anyone help please?
># include <iostream.h>
#include <iostream>
using namespace std;
>void main (void)
int main()
>char *sentencePtr = set;
What is set? Why are you redeclaring sentencePtr?
>sentencePtr--;
Yea, that'll work. NOT!
Try walking to the end of the string, then back:
#include <iostream>
using namespace std;
>void main (void)
int main()
>char *sentencePtr = set;
What is set? Why are you redeclaring sentencePtr?
>sentencePtr--;
Yea, that'll work. NOT!
Try walking to the end of the string, then back:
C++ Syntax (Toggle Plain Text)
char *p = sentencePtr; while ( *p != '\0' ) ++p; while ( p != sentencePtr ) cout.put ( *--p );
I'm here to prove you wrong.
•
•
Join Date: Jul 2005
Posts: 47
Reputation:
Solved Threads: 0
Ok
I don't know why I used "set." I'm looking at the examples in my book and just kinda finding my way around. It's trial and error for me, guys. I guess I was trying to give the array a name so that the computer knows how many characters it has and works backward from there...ignore this. I don't know what I was doing.
anyway,
from the suggestions, I recode it:
and I got the following error:
X:\dtran5.pds\Chapter 10 HWa\Project #2 backward string.cpp(20) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
So what's wrong now?
I don't know why I used "set." I'm looking at the examples in my book and just kinda finding my way around. It's trial and error for me, guys. I guess I was trying to give the array a name so that the computer knows how many characters it has and works backward from there...ignore this. I don't know what I was doing.
anyway,
from the suggestions, I recode it:
C++ Syntax (Toggle Plain Text)
//project #2 backward String (follow page 515, 519) //write a function that accepts a pointer to a C-string as an argument and displays its contents backwards. #include <iostream> using namespace std; //function Prototype void Backward (char *); int main () { char line[201]; cout << "This program will display the contents of the entered phrase backward.\n"; cout << "Please enter a phrase of no more than 200 characters, followed by a period.\n"; cin >> line; cout << "The entered string displayed backward: \n"; cout << Backward(line) << endl; } void Backward (char *sentencePtr) { char *p = sentencePtr; while ( *p != '\0' ) ++p; while ( p != sentencePtr ) cout.put ( *--p ); }
and I got the following error:
X:\dtran5.pds\Chapter 10 HWa\Project #2 backward string.cpp(20) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
So what's wrong now?
•
•
Join Date: Jul 2005
Posts: 244
Reputation:
Solved Threads: 5
You're calling backward out of context.
It doesn't return anything (It's void backward(char*)), so you can't get anything out of it. What you need to do is just print out the modified array.
Be careful, the way Narue's written it, it will automatically output the letters, so you can't just leave in your original cout.
Just call backward(line) after you say "The line printed backwards:"
It doesn't return anything (It's void backward(char*)), so you can't get anything out of it. What you need to do is just print out the modified array.
Be careful, the way Narue's written it, it will automatically output the letters, so you can't just leave in your original cout.
Just call backward(line) after you say "The line printed backwards:"
•
•
Join Date: Jun 2005
Posts: 60
Reputation:
Solved Threads: 5
Dont try to print out a void function.
change to
[CODE]
Backward(line);
cout << endl;
[CODE]
change
C++ Syntax (Toggle Plain Text)
cout << Backward(line) << endl;
[CODE]
Backward(line);
cout << endl;
[CODE]
•
•
Join Date: Jul 2005
Posts: 47
Reputation:
Solved Threads: 0
Ok, I fixed that. And the problem right now is that the program only shows invert the first word of the sentence and disregards the rest of the sentence.
How do I fix that?
C++ Syntax (Toggle Plain Text)
//project #2 backward String (follow page 515, 519) //write a function that accepts a pointer to a C-string as an argument and displays its contents backwards. #include <iostream> using namespace std; //function Prototype void Backward (char *); int main () { char line[201]; cout << "This program will display the contents of the entered phrase backward.\n"; cout << "Please enter a phrase of no more than 200 characters, followed by a period.\n"; cin >> line; cout << "The entered string displayed backward: \n"; Backward(line); cout << endl; } void Backward (char *sentencePtr) { char *p = sentencePtr; while ( *p != '\0' ) ++p; while ( p != sentencePtr ) cout.put ( *--p ); }
How do I fix that?
![]() |
Other Threads in the C++ Forum
- Previous Thread: Problems casting a const char* to char*
- Next Thread: Beginner with C++ and goofy run time problem with DBL Linked List
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






