Having an unexpected output when reversing a string in C++ [closed] - 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 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'.

Related

why when run my code the function doesn't work as expected [closed]

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 months ago.
Improve this question
I made a function to get the larger number out of two when I run it it prints a random large number
#include<iostream>
using namespace std;
int larger(int num1,int num2);
int main()
{
int n1,n2,result;
result=larger(n1,n2);
cout<<"enter two number\n";
cin>>n1>>n2;
cout<<"the larger number is "<<result<<endl;
}
int larger(int num1,int num2)
{
int max;
if (num1>=num2)
max=num1;
else
max=num2;
return max;
}
It's because you are calling the function before inputting 'n1' and 'n2'.
result=larger(n1, n2)
should come after the cin >>...

How to make a proper line break in a C++ program [closed]

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;

Printing Quote Through Loops C++ [closed]

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;
}

what is compilation error in c++? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
i am attending a online competition. my code is working in my compilier visual studio 2013. but the online judges giving me compilation error.
here is my code.
#include<iostream>
#include<string>
#include<cmath>
#include<fstream>
using namespace std;
void main() {
int first_number;
int second_number;
string str;
char ch;
int count = 0;
ifstream yfile("q4.txt");
while (!yfile.eof())
{
yfile >>first_number;
if (first_number < 0)
first_number = abs(first_number);
yfile >> ch;
yfile >> second_number;
if (second_number < 0)
second_number = abs(second_number);
int gcd;
for (int i = 1; i <= first_number&&i <= second_number; i++){
if (first_number%i == 0 && second_number%i == 0){
gcd = i;
}
}
cout << "Output: " << gcd << endl;
}
can anyone please tell me solution? I will be thankful to you.
}
The code posted has one missing curly brace at the end.
error: ‘::main’ must return ‘int’
Try using
int main() {
instead. And add a return 0; statement at the end (or whatever return value you want).

The output begins with space [closed]

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.