Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
#include<bits/stdc++.h>
using namespace std;
int main()
{
int x,y;
while(cin)
cin>>x>>y;
cout<<"YES";
}
here for which input it will print out "YES"?
is cout is logic 1 or 0?
This will print "YES" for any input (except for an infinite stream of valid input, in which case it'll run forever and never print anything).
You forgot braces for your loop, so the cout statement is not actually in the loop.
Your cout is in no way conditional on the values you read in.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
This post was edited and submitted for review last month and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have following program and i want the output to be 10.0
what line of code I have to add in function fun so that i have desired output.
#include<bits/stdc++.h>
using namespace std;
float fun(float a)
{
return a;
}
int main()
{
float a = 10;
cout << fun(a);
return 0;
}
I tried using setprecision() but it is often used with cout. How it can be used when returning the output? I am stuck here.
Nothing to stop this
float fun(float a)
{
cout << fixed << setprecision(1);
return a;
}
But that's stupid code for a stupid puzzle. What does this have to do with real programming?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to write a simple console game.
I want to refresh console 30 times per second. Usually this is not a problem, but this time I'm working with an array of size 30x30, and printing it using two loops is simply not fast enough.
I noticed that
<code>printf( "%s\n", myarray );</code>
is quick enough, but it doesn't work properly with 2d arrays.
Is there a function that will make my array appear "instantly" on screen?
I'm using this function to print my array:
void draw(char screen[32][31]){
for (int x=0;x<32;x++){
for (int y=0;y<31;y++){
cout<<screen[x][y];
}
cout<<endl;
}
}
This should be faster:
void draw(char screen[32][31]){
for (int x = 0; x < 32; x++){
cout.write(screen[x], 31);
cout << '\n';
}
cout << flush;
}
As noted in a comment above, endl is the wrong way to insert a newline, because it also flushes the stream and so removes the benefits of buffering done by the I/O library. See endl vs '\n' for more information.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am trying to print a list in C++.
My code is as follows:
void MovieFunctions::printMovieList(list<Movie> movies)
{
for (Movie m: movies)
{
cout << m << endl;
}
}
I am getting this error for the cout << m << endl line:
cannot convert 'm' (type 'Movie') to type 'const unsigned char*'
What's going wrong?
You need to implement the operator<<() (here) for this purpose.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Can someone help me with my code ? I'm trying to use different algorithm but it returns way to big numbers, when i use algorithm between /* */ it works perfect, anyone can see whats wrong with my new code ? (same on java works)
int* czynnikiPierwsze(int n)throw (string){
if(n<0){
string wyjatek1="Nie mozna rozlozyc ujemnej liczby";
throw wyjatek1;
}
int b=0;
while(n>2){
n=n/tab[n-2];
b++;
}
dzielniki=new int[b]();
int j=0;
while(n>2){
dzielniki[j]=tab[n-2];
n=n/tab[n-2];
j++;
}
/* int a=n;
int*dzielniki=new int[30]();
for(int j=0;j<n+a;j++){
while(n>2){
dzielniki[j]=tab[n-2];
n=n/tab[n-2];
break;
}
}*/
return dzielniki;
}
There's no chance your second while(n>2) loop runs even once, since the first loop only exited when that same condition was no longer true.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
void skaitoInformacija(){
ifstream duomenys("duom.txt");
int eil_nr;
duomenys >> eil_nr;
string eil[eil_nr];
string nereikalinga_eilute;
getline(duomenys, nereikalinga_eilute);
for(int i=0; i<eil_nr; i++){
getline(duomenys, eil[i]);
if(salinamTarpus(eil[i]) == "good"){ //this if statement doesn't work
}
}
}
void salinamTarpus(string eil) {
...
}
void salinamTarpus(string eil)
your function is not returning anything that you can compare with "good" string
you need to change it to return at least some result if you want to compare it...
string salinamTarpus(string eil) {
if(eil == "okString") // string eil is the right one
{
return "good";
}
return "bad";
}
also if your function salinamTarpus(string eil) returns only 2 values("good","bad") it might be better idea to return boolean,char or so. string is a little bit too much overkill