Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

easy question

Posted on 2000-04-03
10
Medium Priority
?
168 Views
Last Modified: 2010-04-02
hi,
in visual basic the String data type can store a lot of characters.
in c++ the char datatype can only store one character.
what datatype is the same as the STRING in visual basic
0
Comment
Question by:adam8
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 2

Expert Comment

by:bbousquet
ID: 2682669
You have a number of options:

1. use an array of chars - in this case, 30 characters:
char somestring[30];

2. use the standard template library's (STL) 'string' type

3. use the MFC 'CString' class - if you're using Windows, that is


0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2682743
that looks like an answer bbousquet :-)

BTW: when using an array of char's, you need to remember the C wants an ascii 0 character (nul) at the end of the string.

So if you want a variable that can hold a 30 character string, make the array at least 31 char's long (to take into account the nul terminator.

string (and MFC CString) don't need to have their size pre-determined .. they will grow and shrink as required (like VB strings do).
0
 
LVL 1

Expert Comment

by:ntdragon
ID: 2683063
sorry if i'm saying something that already has been said

1)char str[10];
2)char *str;
3)String class that in string.h
4)String class in STL
5)Cstring in MFC
6)AnsiString in C++Builder

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:anemos
ID: 2684510
if you know the size of your string,
you need to allocate the necessary memory to a character pointer:

char *ptr;

ptr=(char *)malloc(string_length+1);

the plus 1 is for the null character that marks the end of a string.
0
 
LVL 2

Accepted Solution

by:
bbousquet earned 80 total points
ID: 2684629
You're right, RONSLOW...

I should have submitted my comment as an answer.

Here. It's corrected now.
0
 
LVL 1

Author Comment

by:adam8
ID: 2685528
i have just started using it. it is fine to tell me what to use but i don't even know how to use arrays yet.

how can i user the MFC CString class.
i just need something that will work on Windows 95, 98 NT and the main OS's
0
 
LVL 2

Expert Comment

by:bbousquet
ID: 2685716
The MFC CString class is specific to Windows. If you want your code to be portable to other platforms, you can use either a good old char array or the STL's string type (standard template libraries are usually available for most platforms/compilers).

The main difference between the two is that with a char array you have to specify the maximum size beforehand (or dynamically allocating it, but I don't think you're up to that right now as you seem to be just starting with C).

Then again, the STL's 'string' template can be a little confusing at first, especially since you're probably not familiar with templates yet.

Therefore, I suggest you start by understanding what a char array is:

For instance, if you define the following:

char mystring[30];

....defines a 30 characters array called 'mystring'.

In order to assign it a value you can do the following:

strcpy(mystring, "Hello there");

You can't just assign a string value to a char array with the equal sign - you must use the strxxx functions (check out your compiler's help system for a complete list).

Also keep in mind that C string operations operate on a NULL-terminated char array.

I hope this helps.
0
 
LVL 1

Author Comment

by:adam8
ID: 2686379
ok that will do.
if you don't specify the size is the default 1 character?
0
 
LVL 1

Author Comment

by:adam8
ID: 2686382
thanks for helping.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2686391
Not quite

  char ch;
and
  char string[1];

are very different things.

The first is a char (an integral value, usually one byte, conatining the ascii code for a character).

The second is a string that can hold 1 char (and that should be the nul, terminator anyway .. a string big enough for one character would really be char string[2];)

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question