473,395 Members | 1,623 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,395 software developers and data experts.

C-String Arrays

I am a C++ newbie and am a little confused on CString arrays. I wrote an
example below of what I am trying to do below. It seems that if I enter in a
model number like A123 or B345, that when the cout statement comes around
that it only outputs the first character. Is there no way to do this with
type char and have the cout output the entire 4 character model number? If
not would the only choice be a type string? I want to do an input validation
on each character (isdigit, isalpha and strlen) so that is why I am trying
to make it a char type. Thanks for any input :).

#include <iostream>
using namespace std;

char modelNum[4];

int main ()
{
for (int index = 0; index < 4; index++)
{
cout << "Please enter a model number " << endl;
cin >> modelNum[index];
cin.ignore(4);
}

for (int IDX = 0; IDX < 4; IDX++)
{
cout << modelNum[IDX] << endl;
}

return 0;
}

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Jul 22 '05 #1
2 1597
Usenet Text wrote:
I am a C++ newbie and am a little confused on CString arrays. I wrote an char modelNum[4];


That is not a string array - it is an array of characters, which are
individual letters or symbols.

char modelNum[4] = { 'y', 'o', '\0' };
cout << modelNum << endl

That will print "yo" to the console.

This is an array of strings:

std::string modelNum[4];

You must return to your textbooks (plural!) before attempting to code again.
It's not as easy as just replacing 'char' with 'string'...

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #2
Usenet Text wrote:
I am a C++ newbie and am a little confused on CString arrays. I wrote
an example below of what I am trying to do below. It seems that if I
enter in a model number like A123 or B345, that when the cout
statement comes around that it only outputs the first character.
It prints the character you specify. You have an array of 4 char, and in
your first for loop, you let the user enter the character for each of
those 4 and ignore the rest. Later, you print those four characters.
Is there no way to do this with type char and have the cout output the
entire 4 character model number?
Not with char, no. A char can hold exactly one character. You can use
arrays of char for holding more than one, but I'd recommend
std::string.
If not would the only choice be a type string?
If you mean std::string, then no, it's not the only choice, but it's
probably the best choice.
I want to do an input validation on each character (isdigit, isalpha
and strlen) so that is why I am trying to make it a char type.


And? What stops you from using them with std::string?

Jul 22 '05 #3

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

Similar topics

13
by: franky.backeljauw | last post by:
Hello, following my question on "std::copy versus pointer copy versus member copy", I had some doubts on the function memcpy, as was used by tom_usenet in his reply. - Is this a c++ standard...
10
by: Joel | last post by:
Hi all, I have a class CMyClass with some member vars like a couple of arrays of char *'s and some integers. In another class header file (call it CClass for simplicity), I declare three arrays...
8
by: Oskar | last post by:
Hi. I`m new in cpp and i have a litlle problem. i have a CString from Edit Box (eg."aaa bbb ccc 7327373 d feaf 323 dvjiv 234") and i want to put the data (space separated) into an array.It shuld...
5
by: Tim Wong | last post by:
All: I am trying to convert a CString value to an unsigned char array. I found some code online that will allow me to compile, but when I try to print out...i get a whole mess. /*Begin Code*/...
1
by: dave | last post by:
Hi, I would apprecaite the followinghelp; I need to pass a TCHAR * s = foo*.* \0 *h\0" to CString operator, but it will stop reading input at teh first \0, how do I work around this ?? ...
10
by: ellie | last post by:
How do you add elements to an array defined within another class? For instance, class CEntity { public: int num_inPort; int num_outPort;
7
by: Klaus Bonadt | last post by:
I have an existing VC6 application using the MFC. I am able to pass CString and other parameters from such a VC6 dll to an unmanaged MFC dll (compiled in Visual Studio .NET). Now I want to use...
2
by: Christoph Conrad | last post by:
Hi, given the following test case, with CString from Microsofts MFC: ================================================== file 1: CString arr; ...
9
by: kylie991 | last post by:
Hi there just wondering if I am on the right track. I need to load a file in a program and then store the stata in an array. I am now testing this function to see if it is working... when I run the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.