Printing Quote Through Loops 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 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;
}

Related

C++ different output on computer and on school coderunner [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 days ago.
Improve this question
I have an exercise to do to implement the Gregory-Leibniz series approximation of Pi. This is the code I have:
#include <iostream>
#include <iomanip>
#include <cmath>
int main() {
int k = 0;
int d = 0;
std::cin >> k;
std::cin >> d;
double sum = 0;
for(int i;i<=k;i++){
sum = sum + (pow(-1, i) / (2*i + 1));
}
sum = 4 * sum;
std::cout << std::fixed << std::setprecision(d) << sum << "\n";
return 0;
}
On my computer, for example, the input "1 3" outputs result "2.667", but on the school coderunner website, the output is always 0s, so in this it's "0.000". Can someone please help me?

cout<<endl; not working for printing a 2d array [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
I am trying to print a 2d array like this.
1,2
3,4
5,6
7,8
until 20
and this is the code
#include <iostream>
using namespace std;
int main()
{
int A[10][2]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
for(int i=0;i<10;i++)
for(int j=0;j<2;j++)
{
cout<<A[i][j]<<" ";
}
cout << endl;
}
But everytime it prints it prints them in straight line , like 1 2 3 4 5 6............. What could I be doing wrong?
Hey there you forgot to add {} after first for loop.
Here's solution
#include <iostream>
using namespace std;
int main()
{
int A[10][2]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
for(int i=0;i<10;i++)
{
for(int j=0;j<2;j++)
{
cout<<A[i][j]<<" ";
}
cout << endl;
}
}

Having an unexpected output when reversing a string in 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 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'.

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.

Why doesn't my c++ code work? [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 8 years ago.
Improve this question
What have I done wrong with the following code?
#include <iostream>
using namespace std;
main ()
{
int a;
int b;
int sum;
cout << "Enter first number \n";
cin >> a;
cout << "Enter second number \n";
cin >> b;
sum = a+b;
cout << "The sum of both numbers is" << sum << endl;
return 0;
}
Does the editor you are using tells errors, so the code is not executing? Or som exception rises? Or it is executing but nothing is shown? Please specify your problem accurately.
Anyway, you must use
int main ()
instead of
main()
Notice that your code returns a value. The last line of you code is:
return 0;
Thus, you must specify an int return type.
Check your initial lines with this.
#include <iostream>
using namespace std;
int main ()
{