This question already has answers here:
std::cout not printing the value with out endl (newline specifier)
(1 answer)
std::cout won't print
(4 answers)
Closed 3 years ago.
I was just watching a video on youtube about an akm function and i tried to implement it.
I actually wrote a code and forgot to make spaces between the variables(for an easy reading), but the program didn't print anything but it kept on calculating.
I thought that a similar syntax would work just fine. Is there something I am doing wrong ?
Here is the code :
#include <bits/stdc++.h>
using namespace std;
int akm(int m,int n) {
if(m==0) return n+1;
else if(n==0) return akm(m-1,1);
else return akm(m-1 , akm(m,n-1));
}
int main() {
for(int i=0;i<6;i++)
for(int j=0;j<6;j++) {
cout<<i<<" "<<j ;
cout<<akm(i,j);
}
}
You may need to explicitly flush the output stream or print a newline which may flush the buffer on some streams:
std::cout << std::flush;
std::cout << std::endl;
Related
This question already has answers here:
Getting a weird percent sign in printf output in terminal with C
(3 answers)
Closed last month.
I am writing code and its output is a little different from the regular ones.
#include <iostream>
using namespace std;
int main()
{
int x;
cin >> x;
cout << "Value of x: " << x;
return 0;
}
I expected only a integer but i got an output like this:Output of the above code
I would say that's you shell's prompt. You didn't print a new-line character ('\n').
This question already has answers here:
How to handle wrong data type input
(4 answers)
Closed 2 years ago.
I'm sorry if this question is stupid, but it's been kind of bugging me. I have written a program that is supposed to accept user input 5 times and then print out the result each time (i am using a while loop.) Here is the code I wrote:
#include <iostream>
int main()
{
int x = 1;
int number;
while (x <= 5)
{
std::cin >> number;
std::cout << number << std::endl;
x++;
}
return 0;
}
However, after compiling and running (i'm using clang) the program only lets me insert user input once and then it just prints a bunch of 0's:
jakdfjaksdfjk
0
0
0
0
0
I am really confused why this behavior happens. Shouldn't you be able to pass in user input 5 times? Why does this behavior happen? Help would really be appreciated.
You are trying to read an integer and "jakdfjaksdfjk" would be a string, that's why that happens. Type something like 1 4 8 35 42 and it'll work as you expect
You should consider checking the validation of std::cin:
#include <iostream>
int main(void) {
int x = 1;
int number;
while (x++ <= 5) {
std::cin >> number;
// If the input isn't an integer, the breaks the loop and quit
if (!std::cin.good()) {
std::cout << "Numbers only please.\n";
break;
}
// Otherwise, simply print...
std::cout << number << std::endl;
}
return 0;
}
This question already has answers here:
Why does printf not flush after the call unless a newline is in the format string?
(10 answers)
How does std::flush work?
(4 answers)
Cout won't print text without endl inside while loop?
(1 answer)
Closed 6 years ago.
first i used this code:
void pause(long n)
{
clock_t at=clock();
while(clock()-at<=n)
;
}
int main()
{
cout<<1;
pause(100000);
cout<<2;
}
what do u expect?
output will be 1(some time gap)2 right?
NO..!!
its (some time gap)12 how??
Anyways, i changed the pause function as follows-
void pause(long n)
{
for(long i=1; i<=n*n; i++)
;
}
Still same thing..!!
why this blank for loop executing first??
but if i do this
void pause(long n)
{
for(long i=1; i<=n*n; i++)
cout<<0 ;
}
Now it happily executes output as 10000000000....(many 0s)....00002
You have forgotten that cout is buffered stream too. If you want to see 1 and 2 immediately you need to call flush
cout.flush()
or
cout << flush;
Note that std::endl also calls flush implicitly
This question already has answers here:
Compare equality of char[] in C
(5 answers)
Closed 6 years ago.
The for loop reverses original string 's' and stores it in 'temp.'Temp is printed correctly. After which, s and temp are compared, but the result always shows NO. :(
#include<cstring>
#include <iostream>
using namespace std;
int main()
{
char s[100], temp[100];
cin >> s;
cout << strlen(s);
for(int i=0;i<strlen(s);i++)
{
temp[i]=s[strlen(s)-i];
}
cout << "temp is" << temp;
if(temp==s)
{
cout << "YES";
}
else
{
cout << "NO";
}
return 0;
}
You should use strcmp instead of == because the latter is merely a pointer comparison. Also, '\0' has to be used to end your string. And your current code would actually create an empty string because your string at position strlen(s) contains '\0'.
Then again, you should not work with char arrays in C++ on your own, rather use std::string as already pointed out in comments.
This question already has answers here:
How do you reverse a string in place in C or C++?
(21 answers)
Closed 7 years ago.
Hey guys I'm new here and a programming noob so bear with me here.
This is for my C++ class which sadly my teacher is terrible at teaching so many things confuse me so I need some help here.
We have a lab that is called 'Reverse Sentence' and this is what it wants In this lab.
Write the function "ReverseSentence" that takes a string parameter and changes it, by reversing it.
For example:
INPUT: the first test
OUTPUT: tset tsrif eht
The function must not use an extra string, but must reverse the elements of the input string.
#include <iostream>
#include <string>
using namespace std;
void ReverseSentence( string& inputSentence){
/* Body of Function Here */
}
int main(){
string inputSentence;
cout << "Input your sentence: ";
getline(cin, inputSentence);
cout << endl;
ReverseSentence(inputSentence);
cout << "Reversed Sentence:" << endl;
cout << inputSentence << endl;
return 0;
}
Can someone please help me what function is because I'm having trouble with it.
Just use std::reverse:
void ReverseSentence( string& inputSentence){
std::reverse(inputSentence.begin(), inputSentence.end());
}
Half of the cycle and swap.
#include<algorithm>
#include<string>
void ReverseSentence(std::string &s){
for (int i = 0; i < s.size()/2; ++i)
std::swap(s[i], s[s.size() - i - 1]);
}