473,324 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Having trouble with char*

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!

Jul 23 '05 #1
3 1671
Ian Arnold wrote:
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.
Jul 23 '05 #2
On Sat, 02 Jul 2005 12:12:57 -0700, Ian Arnold wrote:
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;
}

Jul 23 '05 #3
Ian Arnold wrote:
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:

#include <iostream>

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.

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Don Bruder | last post by:
Got a stumper here. I imagine that for someone experienced in C++, this is too pathetic for words. For a rookie, using this project as a sort of "midterm exam" in his self-taught "how to program in...
22
by: ext_u | last post by:
I'm trying to learn C from "The C Programming Language" (by K&R) and I am about halfway through the first chapter. I am using Miracle C as my text editor and compiler. Up until this point...
12
by: Winbatch | last post by:
Hi, I'm trying to learn multithreading and it doesn't seem to be working for me. I have a feeling it has to do with the fact that I'm writing to files rather than to printf, but maybe not. ...
9
by: Jordan Tiona | last post by:
I can't get this code to work right. It seems to be skipping some of the cin functions. Can someone help me with this? ClassTrack.cpp: #include <iostream> #include "ClassTrack.h" using...
9
suzee_q00
by: suzee_q00 | last post by:
I will admit that lots of times the obvious eludes me and this is most likely one of those times but if anyone has any ideas on what I can do to make this code work, I would greatly appreciate it....
5
by: tkondal | last post by:
Hi all. I just started looking at Python's ctypes lib and I am having trouble using it for a function. For starters, here's my Python code: from ctypes import*; myStringDLL=...
2
by: spidey12345 | last post by:
what i need this program to do is to read paragraphs like "st blah blh test ere se sit blha eere w" and then it will reformat to "st blah...
1
by: yucikala | last post by:
Hello, I'm a "expert of beginner" in C#. I have a dll - in C. And in this dll is this struct: typedef struct msg_s { /* please make duplicates of strings before next call to emi_read() ! */ ...
0
by: jsimps44 | last post by:
Hi, I'm fairly new to c, and very new to piping and file descriptors and can't seem to get past this problem. The piping is very much not working, and I can't figure out for the life of me why. Any...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.