Having trouble with char*

I

Ian Arnold

Alright, so I know that you can use char pointers to store strings, and
I'm trying to make a very simple program to see how it all works that
will have the user enter 5 characters, then the computer will show
those five characters back to the user. So far I have:

#include <iostream.h>

int main()
{
char* buffer = " "; //Five spaces
char in;
char out;

while(in = *buffer++)
{
cin >> in;
}

while(out = *buffer++)
{
cout << out;
}

return 0;
}

This program works how it is supposed to until it gets to the
outputing; nothing happens. It gets the five characters, then ends.
Please help me make this work, and if possible I want it to be based
off of using the char*, and not an array or just five char variables.
Thank you!
 
S

Sensei

Ian said:
while(in = *buffer++)
{
cin >> in;
}

in is modified by cin which ends when enter is pressed (or equivalent).
while(out = *buffer++)
{
cout << out;
}

return 0;
}

Seriously, what do you want to do?

This program doesn't read 5 character, and in C++ you cannot read X
characters and stop the input, this would be platform dependent (DOS is
different from linux)... you can read strings, int, whatever, but you
can't interrupt an input.
 
A

Amadeus W. M.

Alright, so I know that you can use char pointers to store strings, and
I'm trying to make a very simple program to see how it all works that
will have the user enter 5 characters, then the computer will show
those five characters back to the user. So far I have:

#include <iostream.h>

int main()
{
char* buffer = " "; //Five spaces
char in;
char out;

while(in = *buffer++)
{
cin >> in;
}

while(out = *buffer++)
{
cout << out;
}

return 0;
}

This program works how it is supposed to until it gets to the
outputing; nothing happens. It gets the five characters, then ends.
Please help me make this work, and if possible I want it to be based
off of using the char*, and not an array or just five char variables.
Thank you!

What book from hell are you reading?

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
char buffer[5], *c=buffer; // can use the same char* for both I/O

while(c<buffer+5)
cin >> *(c++); // parantheses not necessary, but good for beginners.

c=buffer;
while(c<buffer+5)
cout << *c++;
cout << endl;

return 0;
}
 
R

Rolf Magnus

Ian said:
Alright, so I know that you can use char pointers to store strings,

You don't use pointers to store strings. You use arrays for storing them and
the pointers to point to the start or any other character of such an array.
char* is _not_ a string type. It's a pointer to a char and nothing else.
and I'm trying to make a very simple program to see how it all works that
will have the user enter 5 characters, then the computer will show
those five characters back to the user. So far I have:

#include <iostream.h>

This is an ancient non-standard header. Instead, use:

int main()
{
char* buffer = " "; //Five spaces

Five spaces (plus a '\0' character) that can _not_ be modified. String
literals are constant, and your pointer points to one.
char in;
char out;

while(in = *buffer++)

Ok, here, you copy the next character from the string into 'in'.
{
cin >> in;

Here, you overwrite 'in' again. Then you do nothing with it.
}

while(out = *buffer++)
{
cout << out;
}

return 0;
}

This program works how it is supposed to until it gets to the
outputing; nothing happens.

That's not quite right. The five spaces that are still in the string literal
are printed.
It gets the five characters, then ends.
Please help me make this work, and if possible I want it to be based
off of using the char*, and not an array or just five char variables.

I suggest you use std::string instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top