Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Write a program that prompts the user for a string and uses a recursive function to print the string backward. Do not use any global variables; use the appropriate parameters. Could you give me some hints, like pseudocode?
int stringbackwards(string a){
if()
else
}
int main(){
string name;
cout<<"Write a name: ";
cin>>name;
cout<<"Backwards "<<stringbackwards(name);
return 0;
}
Why do you use a recursion for that?
There is a good concept in c++ called iterators that already has this functionality implementet :)
http://www.cplusplus.com/reference/string/string/rbegin/
So in your case:
cout<<"Backwards ";
for (std::string::reverse_iterator rit=name.rbegin(); rit!=name.rend(); ++rit)
{
cout << *rit;
}
But to make it recursive, i would do it like this (Pseudocode).
function backwards(std::string& name, unsigned int length)
{
unsigned int length = name.length();
unsigned int currLength = length - 1;
if (length > 0)
{
backwards(name, currLength);
}
std::cout << name[length - currLength - 1];
}
Hint:
Say the string is "abcd". You would like to print "dcba". In other words, you are printing the last letter first.
Hence you will first go deep into recursion and then after coming back, print the letter 'a'.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to find index of '-' in string "book-Buch". What should I do? I mustn't use this function or any others. Any ideas?
int indexOf(int ch, int fromIndex)
#include <iostream>
#include <string>
int main() {
std::string word("book-Buch");
// The easy way
std::cout << word.find("-", 0) << '\n';
// The manual way
for (std::size_t i = 0; i < word.length(); ++i) {
if (word[i] == '-') {
std::cout << i << '\n';
}
}
}
If you just want to find the index that a certain character occurs in, you just need to look at each character and check if it's the one you want.
A string can be treated as an array of characters. It's unknown whether you actually want an array of string objects, or are just confused.
Other questions that would need to be answered: do you need to find all occurrences, or just the first? Are you reading the words out of a file? You don't clearly explain how a file comes into play.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm creating a little program in order to test the vector class.
I'm using a string vector then i read a text file and i'm tring to write each word in the vector (1 word for each space).
When I try to use the push_back in order to put a string into the vector appear an error that says "There isn't a function to convert a string to a char".
If i made some english error sorry.
Thanks for the help.
I read some guide that explain how work the push_back but in all of this tutorial the use this declaration.
vector<string> v_of_string;<br/>
//allocate some memeory<br/>
v_of_string[1].pushback(string to punt in the vector);<br/>
My Code
int main() {
vector<string> str;
//allocate some memory
ifstream iFile("test.txt");
int i = 0;
if (iFile.is_open()) {
while (!iFile.eof()) {
string temp;
iFile >> temp;
str[i].push_back(temp);
cout << str[i];
i++;
}
iFile.close();
}
return 0;
}
So
str[i].push_back(temp);
is an error, you meant
str.push_back(temp);
You push_back to the vector as a whole, not to one particular element of the vector, so there is no need for [i]. I expect if you go back to your guide then it will say the same.
You could also replace cout << str[i]; with cout << str.back(); to always output the last element of the vector. So actually you don't need the variable i at all.
Also
while (!iFile.eof()) {
string temp;
iFile >> temp;
is incorrect, it should be
string temp;
while (iFile >> temp) {
See here for an explanation. I'd be interested if you got this code from your guide as well. Using eof in a while loop in C++ must be single commonest error we see on stack overflow.
Incidentally str is a poor choice of name for a vector variable IMHO.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am trying to write a program that takes a string and an integer in a class object. The program will then sort the class objects, in an array of objects, by the integer, allowing me to then display the names. Unfortunately, when I try to build the array, I have an error on my assignment operator.
My questions are: Do I need to overload the = operator, and if so, how (somehow I've never figured out how to overload operators)? If not, where am I going wrong?
Here is the code:
void InitiativeList::makeList(size_type physicalSize, size_type logicalSize)
{
string sNewActor;
int iNewOrder;
for (size_t index = 0; index < physicalSize; index++)
{
if (logicalSize == physicalSize)
{
grow(physicalSize);
}
cout << "Enter character name: ";
cin >> sNewActor;
if (sNewActor == "Exit")
{
return;
}
cout << "Enter initiative roll: ";
cin >> iNewOrder;
actorOrder[index] = new Actor(iNewOrder, sNewActor);
logicalSize++;
}
}
Thank you for your help.
You don't need new there, because you have an array of actorData and not actorData pointers, and the error is saying that it can't convert actorData pointer to actorData. So replace this line:
actorOrder[index] = new actorData(iNewOrder, sNewActor);
with this:
actorOrder[index] = actorData(iNewOrder, sNewActor);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want my program to accept numbers only between 1 and 4 as input. And if input is an alphabet or or any other number instead of 1, 2, 3 or 4, then it should show an error and prompt the sir user again to input correct values. This is what i'm doing right now.
if (x < 1 || x > 4)
{
cout << "Invalid input!";
}
else if (x == 1)
{
// rest of the program
}
do something like this
unsigned char x;
int num=0;
input:
num =0;
cout<<"Enter input value"<<endl;
cin>>x;
num = x - 48;
if(num>4||num<1)
{
//enter again
goto input;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to read an input by user.
Example: If user inputs 233 245 (consider the space).
i need to assign it to variable like
a=233;
b=245;
How do i do this in c++?
You can do following:
#include <iostream>
int main(int argc, char **argv)
{
int a,b;
// Get values
std::cin >> a >> b;
// Print out values
std::cout << a << ' ' << b << '\n';
}
This reads user input from standard input to the variables a and b, and then prints them to standard output