C++ - Character array output remove loop error
I wish to make a function to remove a character array output, one at a time. The character array is pre-defined, but there is a problem.
void anim_rev(char str)
{
clrscr();
int i;
for(i = 1; str[i] != ''; i++)
{
cout << "b b";
delay(25);
}
}
The cout
statement is being executed first for all the iterations, and then the delay
is executed. The function is supposed to remove a character from the screen every 10 or 25 milliseconds.
Please help fix it.
c++ c++11
add a comment |
I wish to make a function to remove a character array output, one at a time. The character array is pre-defined, but there is a problem.
void anim_rev(char str)
{
clrscr();
int i;
for(i = 1; str[i] != ''; i++)
{
cout << "b b";
delay(25);
}
}
The cout
statement is being executed first for all the iterations, and then the delay
is executed. The function is supposed to remove a character from the screen every 10 or 25 milliseconds.
Please help fix it.
c++ c++11
Can you please be more precise in your question? We kind of need to guess what you need to do. And please, don't say the loop logic is correct when you ask the community to tell you what's wrong with it.
– Sirmyself
Nov 12 '18 at 16:14
Why not just "b b" at once? Can you add more context? How do you run it? What is the expected output and what it actually outputs?
– Ayxan
Nov 12 '18 at 16:16
@Sirmyself The first statement in the block is executed first, and this actually removes all the text at once, and then executed thedelay
is executed, which is not what I need.
– Darsh K
Nov 12 '18 at 16:17
@Ayxan There were a few different versions before coming to this one, and still the error. It is run inside the main() function. and it is supposed to remove one character from the output screen every 10 or 25 milliseconds
– Darsh K
Nov 12 '18 at 16:19
And why do you clear screen withclrscr()
before removing characters? Would there still be something to remove?
– Ayxan
Nov 12 '18 at 16:21
add a comment |
I wish to make a function to remove a character array output, one at a time. The character array is pre-defined, but there is a problem.
void anim_rev(char str)
{
clrscr();
int i;
for(i = 1; str[i] != ''; i++)
{
cout << "b b";
delay(25);
}
}
The cout
statement is being executed first for all the iterations, and then the delay
is executed. The function is supposed to remove a character from the screen every 10 or 25 milliseconds.
Please help fix it.
c++ c++11
I wish to make a function to remove a character array output, one at a time. The character array is pre-defined, but there is a problem.
void anim_rev(char str)
{
clrscr();
int i;
for(i = 1; str[i] != ''; i++)
{
cout << "b b";
delay(25);
}
}
The cout
statement is being executed first for all the iterations, and then the delay
is executed. The function is supposed to remove a character from the screen every 10 or 25 milliseconds.
Please help fix it.
c++ c++11
c++ c++11
edited Nov 12 '18 at 16:21
asked Nov 12 '18 at 16:08
Darsh K
2319
2319
Can you please be more precise in your question? We kind of need to guess what you need to do. And please, don't say the loop logic is correct when you ask the community to tell you what's wrong with it.
– Sirmyself
Nov 12 '18 at 16:14
Why not just "b b" at once? Can you add more context? How do you run it? What is the expected output and what it actually outputs?
– Ayxan
Nov 12 '18 at 16:16
@Sirmyself The first statement in the block is executed first, and this actually removes all the text at once, and then executed thedelay
is executed, which is not what I need.
– Darsh K
Nov 12 '18 at 16:17
@Ayxan There were a few different versions before coming to this one, and still the error. It is run inside the main() function. and it is supposed to remove one character from the output screen every 10 or 25 milliseconds
– Darsh K
Nov 12 '18 at 16:19
And why do you clear screen withclrscr()
before removing characters? Would there still be something to remove?
– Ayxan
Nov 12 '18 at 16:21
add a comment |
Can you please be more precise in your question? We kind of need to guess what you need to do. And please, don't say the loop logic is correct when you ask the community to tell you what's wrong with it.
– Sirmyself
Nov 12 '18 at 16:14
Why not just "b b" at once? Can you add more context? How do you run it? What is the expected output and what it actually outputs?
– Ayxan
Nov 12 '18 at 16:16
@Sirmyself The first statement in the block is executed first, and this actually removes all the text at once, and then executed thedelay
is executed, which is not what I need.
– Darsh K
Nov 12 '18 at 16:17
@Ayxan There were a few different versions before coming to this one, and still the error. It is run inside the main() function. and it is supposed to remove one character from the output screen every 10 or 25 milliseconds
– Darsh K
Nov 12 '18 at 16:19
And why do you clear screen withclrscr()
before removing characters? Would there still be something to remove?
– Ayxan
Nov 12 '18 at 16:21
Can you please be more precise in your question? We kind of need to guess what you need to do. And please, don't say the loop logic is correct when you ask the community to tell you what's wrong with it.
– Sirmyself
Nov 12 '18 at 16:14
Can you please be more precise in your question? We kind of need to guess what you need to do. And please, don't say the loop logic is correct when you ask the community to tell you what's wrong with it.
– Sirmyself
Nov 12 '18 at 16:14
Why not just "b b" at once? Can you add more context? How do you run it? What is the expected output and what it actually outputs?
– Ayxan
Nov 12 '18 at 16:16
Why not just "b b" at once? Can you add more context? How do you run it? What is the expected output and what it actually outputs?
– Ayxan
Nov 12 '18 at 16:16
@Sirmyself The first statement in the block is executed first, and this actually removes all the text at once, and then executed the
delay
is executed, which is not what I need.– Darsh K
Nov 12 '18 at 16:17
@Sirmyself The first statement in the block is executed first, and this actually removes all the text at once, and then executed the
delay
is executed, which is not what I need.– Darsh K
Nov 12 '18 at 16:17
@Ayxan There were a few different versions before coming to this one, and still the error. It is run inside the main() function. and it is supposed to remove one character from the output screen every 10 or 25 milliseconds
– Darsh K
Nov 12 '18 at 16:19
@Ayxan There were a few different versions before coming to this one, and still the error. It is run inside the main() function. and it is supposed to remove one character from the output screen every 10 or 25 milliseconds
– Darsh K
Nov 12 '18 at 16:19
And why do you clear screen with
clrscr()
before removing characters? Would there still be something to remove?– Ayxan
Nov 12 '18 at 16:21
And why do you clear screen with
clrscr()
before removing characters? Would there still be something to remove?– Ayxan
Nov 12 '18 at 16:21
add a comment |
2 Answers
2
active
oldest
votes
Try the following, note the flush
in the end of each cout
.
Otherwise it will be written only at the end of line, or at the end of program.
note: delay
is not standard function so I used sleep
instead.
#include <iostream>
#ifdef WIN32
#include <Windows.h>
#define sleep(X) Sleep(X*1000)
#else
#include <unistd.h>
#endif
using namespace std;
void anim_rev(const char str)
{
int i;
for(i = 0; str[i] != ''; i++)
{
cout << "b" << " " << "b"<<flush;
sleep(1);
}
}
int main(void)
{
const char* p="Helooooooo";
cout<<p<<flush;
anim_rev(p);
return 0;
}
delay()
can be used when you includedos.h
– Darsh K
Nov 12 '18 at 16:30
@DarshK Most of IDEs doesn't support it. anyway you should add theflush
– SHR
Nov 12 '18 at 16:33
The code does not completely work for multi line array strings.
– Darsh K
Nov 12 '18 at 16:36
@DarshK Correct, printingb
works only for the current line. it won't cancel the line feed.
– SHR
Nov 12 '18 at 16:38
Then how do I cancel the line feed?
– Darsh K
Nov 12 '18 at 16:43
|
show 4 more comments
std::cout
use a buffer and usually it flushes the buffer when there is a std::endl
.
If you force the flush
of std::cout
it should work.
Here the documentation of how to flush with an example:
https://en.cppreference.com/w/cpp/io/basic_ostream/flush
I use C++ 11 for coding, and it does notstd::cout
– Darsh K
Nov 12 '18 at 16:27
@DarshK what?std::cout
is part of the C++ language since 1998!
– hellow
Nov 12 '18 at 16:28
@Simon please format your code, e.g. put variables and other programming related terms (e.g.std::cout
) in backticks (`
)
– hellow
Nov 12 '18 at 16:28
I know but I also use Turbo C++ IDE, which is kind of ancient, and does not supportstd::cout
. Learnt that the hard way.
– Darsh K
Nov 12 '18 at 16:29
@DarshK Turbo C++ does not support any C++. Not C++11, not C++98, not any. It uses the long extinct non-standard C++ variant from 1992.
– Cubbi
Nov 20 '18 at 16:39
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265957%2fc-character-array-output-remove-loop-error%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try the following, note the flush
in the end of each cout
.
Otherwise it will be written only at the end of line, or at the end of program.
note: delay
is not standard function so I used sleep
instead.
#include <iostream>
#ifdef WIN32
#include <Windows.h>
#define sleep(X) Sleep(X*1000)
#else
#include <unistd.h>
#endif
using namespace std;
void anim_rev(const char str)
{
int i;
for(i = 0; str[i] != ''; i++)
{
cout << "b" << " " << "b"<<flush;
sleep(1);
}
}
int main(void)
{
const char* p="Helooooooo";
cout<<p<<flush;
anim_rev(p);
return 0;
}
delay()
can be used when you includedos.h
– Darsh K
Nov 12 '18 at 16:30
@DarshK Most of IDEs doesn't support it. anyway you should add theflush
– SHR
Nov 12 '18 at 16:33
The code does not completely work for multi line array strings.
– Darsh K
Nov 12 '18 at 16:36
@DarshK Correct, printingb
works only for the current line. it won't cancel the line feed.
– SHR
Nov 12 '18 at 16:38
Then how do I cancel the line feed?
– Darsh K
Nov 12 '18 at 16:43
|
show 4 more comments
Try the following, note the flush
in the end of each cout
.
Otherwise it will be written only at the end of line, or at the end of program.
note: delay
is not standard function so I used sleep
instead.
#include <iostream>
#ifdef WIN32
#include <Windows.h>
#define sleep(X) Sleep(X*1000)
#else
#include <unistd.h>
#endif
using namespace std;
void anim_rev(const char str)
{
int i;
for(i = 0; str[i] != ''; i++)
{
cout << "b" << " " << "b"<<flush;
sleep(1);
}
}
int main(void)
{
const char* p="Helooooooo";
cout<<p<<flush;
anim_rev(p);
return 0;
}
delay()
can be used when you includedos.h
– Darsh K
Nov 12 '18 at 16:30
@DarshK Most of IDEs doesn't support it. anyway you should add theflush
– SHR
Nov 12 '18 at 16:33
The code does not completely work for multi line array strings.
– Darsh K
Nov 12 '18 at 16:36
@DarshK Correct, printingb
works only for the current line. it won't cancel the line feed.
– SHR
Nov 12 '18 at 16:38
Then how do I cancel the line feed?
– Darsh K
Nov 12 '18 at 16:43
|
show 4 more comments
Try the following, note the flush
in the end of each cout
.
Otherwise it will be written only at the end of line, or at the end of program.
note: delay
is not standard function so I used sleep
instead.
#include <iostream>
#ifdef WIN32
#include <Windows.h>
#define sleep(X) Sleep(X*1000)
#else
#include <unistd.h>
#endif
using namespace std;
void anim_rev(const char str)
{
int i;
for(i = 0; str[i] != ''; i++)
{
cout << "b" << " " << "b"<<flush;
sleep(1);
}
}
int main(void)
{
const char* p="Helooooooo";
cout<<p<<flush;
anim_rev(p);
return 0;
}
Try the following, note the flush
in the end of each cout
.
Otherwise it will be written only at the end of line, or at the end of program.
note: delay
is not standard function so I used sleep
instead.
#include <iostream>
#ifdef WIN32
#include <Windows.h>
#define sleep(X) Sleep(X*1000)
#else
#include <unistd.h>
#endif
using namespace std;
void anim_rev(const char str)
{
int i;
for(i = 0; str[i] != ''; i++)
{
cout << "b" << " " << "b"<<flush;
sleep(1);
}
}
int main(void)
{
const char* p="Helooooooo";
cout<<p<<flush;
anim_rev(p);
return 0;
}
edited Nov 12 '18 at 16:32
answered Nov 12 '18 at 16:25
SHR
5,73562341
5,73562341
delay()
can be used when you includedos.h
– Darsh K
Nov 12 '18 at 16:30
@DarshK Most of IDEs doesn't support it. anyway you should add theflush
– SHR
Nov 12 '18 at 16:33
The code does not completely work for multi line array strings.
– Darsh K
Nov 12 '18 at 16:36
@DarshK Correct, printingb
works only for the current line. it won't cancel the line feed.
– SHR
Nov 12 '18 at 16:38
Then how do I cancel the line feed?
– Darsh K
Nov 12 '18 at 16:43
|
show 4 more comments
delay()
can be used when you includedos.h
– Darsh K
Nov 12 '18 at 16:30
@DarshK Most of IDEs doesn't support it. anyway you should add theflush
– SHR
Nov 12 '18 at 16:33
The code does not completely work for multi line array strings.
– Darsh K
Nov 12 '18 at 16:36
@DarshK Correct, printingb
works only for the current line. it won't cancel the line feed.
– SHR
Nov 12 '18 at 16:38
Then how do I cancel the line feed?
– Darsh K
Nov 12 '18 at 16:43
delay()
can be used when you include dos.h
– Darsh K
Nov 12 '18 at 16:30
delay()
can be used when you include dos.h
– Darsh K
Nov 12 '18 at 16:30
@DarshK Most of IDEs doesn't support it. anyway you should add the
flush
– SHR
Nov 12 '18 at 16:33
@DarshK Most of IDEs doesn't support it. anyway you should add the
flush
– SHR
Nov 12 '18 at 16:33
The code does not completely work for multi line array strings.
– Darsh K
Nov 12 '18 at 16:36
The code does not completely work for multi line array strings.
– Darsh K
Nov 12 '18 at 16:36
@DarshK Correct, printing
b
works only for the current line. it won't cancel the line feed.– SHR
Nov 12 '18 at 16:38
@DarshK Correct, printing
b
works only for the current line. it won't cancel the line feed.– SHR
Nov 12 '18 at 16:38
Then how do I cancel the line feed?
– Darsh K
Nov 12 '18 at 16:43
Then how do I cancel the line feed?
– Darsh K
Nov 12 '18 at 16:43
|
show 4 more comments
std::cout
use a buffer and usually it flushes the buffer when there is a std::endl
.
If you force the flush
of std::cout
it should work.
Here the documentation of how to flush with an example:
https://en.cppreference.com/w/cpp/io/basic_ostream/flush
I use C++ 11 for coding, and it does notstd::cout
– Darsh K
Nov 12 '18 at 16:27
@DarshK what?std::cout
is part of the C++ language since 1998!
– hellow
Nov 12 '18 at 16:28
@Simon please format your code, e.g. put variables and other programming related terms (e.g.std::cout
) in backticks (`
)
– hellow
Nov 12 '18 at 16:28
I know but I also use Turbo C++ IDE, which is kind of ancient, and does not supportstd::cout
. Learnt that the hard way.
– Darsh K
Nov 12 '18 at 16:29
@DarshK Turbo C++ does not support any C++. Not C++11, not C++98, not any. It uses the long extinct non-standard C++ variant from 1992.
– Cubbi
Nov 20 '18 at 16:39
add a comment |
std::cout
use a buffer and usually it flushes the buffer when there is a std::endl
.
If you force the flush
of std::cout
it should work.
Here the documentation of how to flush with an example:
https://en.cppreference.com/w/cpp/io/basic_ostream/flush
I use C++ 11 for coding, and it does notstd::cout
– Darsh K
Nov 12 '18 at 16:27
@DarshK what?std::cout
is part of the C++ language since 1998!
– hellow
Nov 12 '18 at 16:28
@Simon please format your code, e.g. put variables and other programming related terms (e.g.std::cout
) in backticks (`
)
– hellow
Nov 12 '18 at 16:28
I know but I also use Turbo C++ IDE, which is kind of ancient, and does not supportstd::cout
. Learnt that the hard way.
– Darsh K
Nov 12 '18 at 16:29
@DarshK Turbo C++ does not support any C++. Not C++11, not C++98, not any. It uses the long extinct non-standard C++ variant from 1992.
– Cubbi
Nov 20 '18 at 16:39
add a comment |
std::cout
use a buffer and usually it flushes the buffer when there is a std::endl
.
If you force the flush
of std::cout
it should work.
Here the documentation of how to flush with an example:
https://en.cppreference.com/w/cpp/io/basic_ostream/flush
std::cout
use a buffer and usually it flushes the buffer when there is a std::endl
.
If you force the flush
of std::cout
it should work.
Here the documentation of how to flush with an example:
https://en.cppreference.com/w/cpp/io/basic_ostream/flush
edited Nov 15 '18 at 22:18
answered Nov 12 '18 at 16:22
Simon Bertho
12
12
I use C++ 11 for coding, and it does notstd::cout
– Darsh K
Nov 12 '18 at 16:27
@DarshK what?std::cout
is part of the C++ language since 1998!
– hellow
Nov 12 '18 at 16:28
@Simon please format your code, e.g. put variables and other programming related terms (e.g.std::cout
) in backticks (`
)
– hellow
Nov 12 '18 at 16:28
I know but I also use Turbo C++ IDE, which is kind of ancient, and does not supportstd::cout
. Learnt that the hard way.
– Darsh K
Nov 12 '18 at 16:29
@DarshK Turbo C++ does not support any C++. Not C++11, not C++98, not any. It uses the long extinct non-standard C++ variant from 1992.
– Cubbi
Nov 20 '18 at 16:39
add a comment |
I use C++ 11 for coding, and it does notstd::cout
– Darsh K
Nov 12 '18 at 16:27
@DarshK what?std::cout
is part of the C++ language since 1998!
– hellow
Nov 12 '18 at 16:28
@Simon please format your code, e.g. put variables and other programming related terms (e.g.std::cout
) in backticks (`
)
– hellow
Nov 12 '18 at 16:28
I know but I also use Turbo C++ IDE, which is kind of ancient, and does not supportstd::cout
. Learnt that the hard way.
– Darsh K
Nov 12 '18 at 16:29
@DarshK Turbo C++ does not support any C++. Not C++11, not C++98, not any. It uses the long extinct non-standard C++ variant from 1992.
– Cubbi
Nov 20 '18 at 16:39
I use C++ 11 for coding, and it does not
std::cout
– Darsh K
Nov 12 '18 at 16:27
I use C++ 11 for coding, and it does not
std::cout
– Darsh K
Nov 12 '18 at 16:27
@DarshK what?
std::cout
is part of the C++ language since 1998!– hellow
Nov 12 '18 at 16:28
@DarshK what?
std::cout
is part of the C++ language since 1998!– hellow
Nov 12 '18 at 16:28
@Simon please format your code, e.g. put variables and other programming related terms (e.g.
std::cout
) in backticks (`
)– hellow
Nov 12 '18 at 16:28
@Simon please format your code, e.g. put variables and other programming related terms (e.g.
std::cout
) in backticks (`
)– hellow
Nov 12 '18 at 16:28
I know but I also use Turbo C++ IDE, which is kind of ancient, and does not support
std::cout
. Learnt that the hard way.– Darsh K
Nov 12 '18 at 16:29
I know but I also use Turbo C++ IDE, which is kind of ancient, and does not support
std::cout
. Learnt that the hard way.– Darsh K
Nov 12 '18 at 16:29
@DarshK Turbo C++ does not support any C++. Not C++11, not C++98, not any. It uses the long extinct non-standard C++ variant from 1992.
– Cubbi
Nov 20 '18 at 16:39
@DarshK Turbo C++ does not support any C++. Not C++11, not C++98, not any. It uses the long extinct non-standard C++ variant from 1992.
– Cubbi
Nov 20 '18 at 16:39
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265957%2fc-character-array-output-remove-loop-error%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you please be more precise in your question? We kind of need to guess what you need to do. And please, don't say the loop logic is correct when you ask the community to tell you what's wrong with it.
– Sirmyself
Nov 12 '18 at 16:14
Why not just "b b" at once? Can you add more context? How do you run it? What is the expected output and what it actually outputs?
– Ayxan
Nov 12 '18 at 16:16
@Sirmyself The first statement in the block is executed first, and this actually removes all the text at once, and then executed the
delay
is executed, which is not what I need.– Darsh K
Nov 12 '18 at 16:17
@Ayxan There were a few different versions before coming to this one, and still the error. It is run inside the main() function. and it is supposed to remove one character from the output screen every 10 or 25 milliseconds
– Darsh K
Nov 12 '18 at 16:19
And why do you clear screen with
clrscr()
before removing characters? Would there still be something to remove?– Ayxan
Nov 12 '18 at 16:21