Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I made a function that will reverse the string, but the output of the reversed string always shifts towards the right by one character.
#include <iostream>
#include <string>
using namespace std;
void reverse(string string1)
{
cout << endl;
for (int i = string1.size(); i >= 0; i--)
{
cout << string1[i];
}
cout << endl;
}
int main()
{
string string1;
getline(cin, string1);
reverse(string1);
system("pause");
return 0;
}
Your first output is of a character that does not exist.
std::string's leaky abstraction means that your first iteration is printing '\0', which apparently looks like a space in your configuration.
Begin at string1.size() - 1.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
What is wrong with my code? I try to get a line break using both \n and endl but it shows an error.
#include <iostream>
using namespace std;
int main()
{
const int MinutesPerHour = 60;
const float PI = 3.14;
cout << MinutesPerHour;endl;
cout << PI;
return 0;
}
You should place the endl after the output stream operator <<.
Like this:
cout << MinutesPerHour << endl;
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
my knowledge of c++ is pitiful. I've been stumped on this simple problem for a long time and would just a point to the right the direction. The basis of the program is to have the user chose a number 1-5 and then based on their decision print out a quote that many times. So if they chose the number 4 it will display the quote 4 times.
Here is what I have so far:
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
int PickNumber()
{
int i;
cout << "Please Enter a Number From 1 to 5:";
cin >> i;
for (int j = 0; j < i; j++)
{
cout << "Congrats!";
}
return i;
}
int main()
{
_getch();
return 0;
}
Just to add an answer to this question already solved by #molbdnilo, #Rietty and #Gox: the PickNumber() function is not called in the main function.
The PickNumber() call just needs to be added to the main function.
int main() {
PickNumber();
return 0;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm trying to append some characters from one string to another, but I couldn't do it. I tried something like this:
std::string fooz = "fooz";
std::string foo;
int i = 0;
while(i< fooz.length()){
if(fooz[i] != 'z'){
foo.push_back(fooz[i]);
}
i++;
}
foo after the while its empty.
You're taking length from the target string, which is still empty and the while loop won't be executed at all.
Change
while(i< foo.length()){
to
while(i< fooz.length()){
The STL can help you in this kind of scenarios.
This one uses the remove algorithm which provide a range of element to erase.
#include <string>
#include <iostream>
#include <algorithm>
int main()
{
std::string str("aaazbbb");
std::cout << str << std::endl;
str.erase(std::remove(str.begin(), str.end(), 'z'), str.end());
std::cout << str << std::endl;
}
std::string fooz = "fooz";
std::string foo;
int i = 0;
int len=fooz.size();
while(i< len){
if(fooz[i] != 'z'){
foo.push_back(fooz[i]);
}
i++;
}
Don not call std::string.size() or length() in while loop.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I wrote a simple code to reverse a string but I get a strange output. For example, if I type "hello" I get at output " qlleh" and I sincerely don't know why.
Here is the code:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
int a = 0;
char s[1024],r[1024];
cout<<"Enter a string:";
cin>>s;
for(int i = char_traits<char>::length(s); i >= 0; i--){
//if(isalpha(s[i]))
r[a++] += s[i];
}
cout<<r;
}
You want to use a '=' only. A '+=' will add onto and increment the actual character value. Therefore, 'a' + 2 = 'c'.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Whenever I try to run this program it returns an error saying:
no operator "<<" matches these operands
Also note that the program only runs into this problem in the getChoice() function.
#include <iostream>
#include "utilities.h"
using namespace std;
int getChoice(string inChoices[]){
int numOfChoices = sizeof(inChoices) / sizeof(inChoices[0]);
string x = inChoices[0];
string y = inChoices[1];
cout << x << endl << y << endl;
return numOfChoices;
}
int main()
{
string choices[2] = { "Happy Day", "Even Better Day" };
cout << utilities::getChoice(choices) << endl;
cout << endl << sizeof(choices) / sizeof(choices[0]) << endl;
}
You need also to include the string header:
#include <string>
You need to #include <string>
And your calculation of numOfChoices in getChoice() is wrong, since the parameter inChoices is actually a "pointer to string" instead of "array of strings".