Connecting Tech Pros Worldwide Forums | Help | Site Map

Having trouble with char*

Ian Arnold
Guest
 
Posts: n/a
#1: Jul 23 '05
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!


Sensei
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Having trouble with char*


Ian Arnold wrote:[color=blue]
> while(in = *buffer++)
> {
> cin >> in;
> }[/color]

in is modified by cin which ends when enter is pressed (or equivalent).
[color=blue]
>
> while(out = *buffer++)
> {
> cout << out;
> }
>
> return 0;
> }[/color]

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.
Amadeus W. M.
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Having trouble with char*


On Sat, 02 Jul 2005 12:12:57 -0700, Ian Arnold wrote:
[color=blue]
> 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![/color]

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;
}

Rolf Magnus
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Having trouble with char*


Ian Arnold wrote:
[color=blue]
> Alright, so I know that you can use char pointers to store strings,[/color]

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.
[color=blue]
> 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>[/color]

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

#include <iostream>

[color=blue]
> int main()
> {
> char* buffer = " "; //Five spaces[/color]

Five spaces (plus a '\0' character) that can _not_ be modified. String
literals are constant, and your pointer points to one.
[color=blue]
> char in;
> char out;
>
> while(in = *buffer++)[/color]

Ok, here, you copy the next character from the string into 'in'.
[color=blue]
> {
> cin >> in;[/color]

Here, you overwrite 'in' again. Then you do nothing with it.
[color=blue]
> }
>
> while(out = *buffer++)
> {
> cout << out;
> }
>
> return 0;
> }
>
> This program works how it is supposed to until it gets to the
> outputing; nothing happens.[/color]

That's not quite right. The five spaces that are still in the string literal
are printed.
[color=blue]
> 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.[/color]

I suggest you use std::string instead.

Closed Thread