How do I print a specific section of a char array?

May 6, 2013 at 3:50pm
Hi im new to the forum and this is my first post.
I want to print burp first, then followed by b, followed by u, followed by rp.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <windows.h>
using namespace std;
void main (void) {
string things = burp;
cout << things;
char *a=new char[things.size()+1];
a[things.size()]=0;
memcpy(a,things.c_str(),things.size());

cout << //cout b;
Sleep (100);
cout << //cout u;
Sleep (100);
cout << //cout rp;
}
Last edited on May 6, 2013 at 3:51pm
May 6, 2013 at 4:04pm
What are your b, u, and rp?!
May 6, 2013 at 4:24pm
'b', 'u' and 'rp' are the letters in 'burp', which is the value of the string variable 'things'
May 6, 2013 at 4:28pm
Firstly rp is a not a char but instead two secondly you are setting things to an object called burp mot a string of characters eg "burp" and third a string is just an array of chars so 'b' is things[0]
Last edited on May 6, 2013 at 4:30pm
May 9, 2013 at 10:35am
for example i have a character array a hundred characters long.
char things[100];
then after the user cin the chars, i want it to cout them in this way:

i want to cout characters 1 to 5, then sleep for 100milisec, then cout characters 6 to 66, then sleep for 100milisec, then cout characters 67 to 100.

rather than
cout << things[1] << things [2] << things[3] << things[4] << things[5];

you could imagine how long the .cpp file will be.

how do i make it so that
1
2
3
4
5
cout << things[1until5];
Sleep (100);
cout << things[6until66]
Sleep (100);
cout << things[67until100]
?
May 9, 2013 at 4:21pm
Use a for loop
May 9, 2013 at 4:35pm
closed account (3qX21hU5)
First lets take care of your errors.

1) You need to use int main() not void main(). The main function always returns a integer so always declare it as int. If your compiler doesn't give you a error or warning for that switch to a standard compliant compiler.


2) string things = burp; Is in error to assign a string literal to a std::string you need to enclose the literal in "Literal Here".


3) You have a memory leak on line 9 char *a=new char[things.size()+1]; you use new but never use delete. Always remember that whenever you call new you always need to call delete unless a classes destructor handles it for you.


Now lets move onto your question. To print a string's characters you can index a certain character like so things[0] will print the first character.

Or if you want to print out a range of characters from the string you can use the string.substr() function which std::string provides.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
    string things = "burp";
    cout << things << endl;

    cout << things[0] << endl;
    Sleep (1000);
    cout << things[1] << endl;
    Sleep (1000);
    
    // Prints every character from things[2] till the end of the string
    cout << things.substr(2) << endl;
}


That should give you all the tool you need :) All you got to do is figure out how to use them to do what you want.

I would also recommend you use C++ techniques that are less error prone then C techniques. Meaning use the C++ features like vectors and strings instead of char arrays. I'm not sure why you need to use C strings in this situation since you are already using std::string and that provides all the functionality you will need.
Last edited on May 9, 2013 at 4:39pm
Topic archived. No new replies allowed.