Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What I want in my program is instead of
C++
cout << "Text";

which prints out: Text, I want another command like cout << that will print out each character as a shortcut instead of
char str[] = "Hello world!";
    int i = 0;
    
    while (str [i] != '\0')
    {
        cout << str[i++] << ' ';
        cout.flush();
        usleep (100000);
        cout << ' ';
    }

So i can set str to a variable and easly access, or some other shorter way to do so.

~Thanks!

What I have tried:

C++
#include <unistd.h>
#include <iostream>
#include <string>
using namespace std;

int main ()
{
    char str[] = "Hello world!";
    int i = 0;
    
    while (str [i] != '\0')
    {
        cout << str[i++] << ' ';
        cout.flush();
        usleep (100000);
        cout << ' ';
    }
    return 0;
}
and
C++
int main ()
{
    
    char str[] = "Hello world!";
    int i = 0;
    
    while (str [i] != '\0')
    {
        cout << str[i++] << ' ';
        cout.flush();
        usleep (100000);
        cout << ' ';
    }
    str [] = "Hello world2!";
    return 0;
}
This one returns an error.
Posted
Updated 25-Aug-18 21:48pm
Comments
Dave Kreskowiak 25-Aug-18 19:29pm    
So, were you planning on telling anyone what the error message is? You know, the most important piece of information there is when troubleshooting a problem.

And you might want to explain what you mean by "print out each character as a shortcut" because that doesn't mean anything.
Afzaal Ahmad Zeeshan 25-Aug-18 19:33pm    
As a shortcut? What? That is quite confusing.
[no name] 25-Aug-18 20:09pm    
Show us an example of an input and the expected output.

C++ do not dynamically resize arrays when you store new values inside.
C++
char 
str[] = "Hello world!";
...
str[] = "Hello world2!";  // so this will not fit in actual array, 1 chat too long.
 
Share this answer
 
v2
Comments
CPallini 26-Aug-18 10:06am    
My 5.
Patrice T 26-Aug-18 10:18am    
Thank you
You are using the wrog type for your operations. Use std::string instead of the char[].
 
Share this answer
 
Comments
Member 13961615 26-Aug-18 8:48am    
firstly, If I do that I will get an error. Also, the goal of my program is to print out each individual character with a slight pause between them.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900