• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

No Strings in C++?

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

klown22

New Member
Joined
Mar 11, 2004
At school, I can't get our copy of C++ do recognize strings as a data type.

I've included the .h file and stuff, and it won't work. The string.h or maybe strings.h file is in the folder. the word string won't turn blue when i declare it. I've tried doing groups of char variables instead, but thats too much work

any ideas on how I could get this to work?

thanks in advance, it would make my program so much easier to do
 
It won't turn blue in MSVS. In C++, string is a class, not a keyword. char, on the other hand is a keyword and is therefore highlighted in blue.

You may be getting a header file intended for C programs. To make sure you get the right one, leave off the .h and make sure you are using the std namespace.

#include <string>
using namespace std;
 
thanks for the reply :)

I'm used to VB and PHP I guess.

Would it be too much to ask how I could would ask for an input and store it as a string, like what to declare and stuff? We haven't done classes yet, i'm learning at school for the most part
 
No problem.

Include iostream, just like how you included string above. Only one using statement is needed for each file, usally right after all the includes.

Then, to read something in you need to use the cin object like follows:

#include <string>
#include <iostream>
using namespace std;

void main(){
string str;

cin >> str;
cout << "You entered \"" << str << "\"" << endl;
}

cin and cout are global objects defined in iostream. They represent streams connected to the console. cin is the input stream, while cout is the output stream. The << and >> are overloaded operators (just accept them for now.) There is a function that handles the << operator when a string is on the right and an istream is on the left. This function prints the string and returns a reference to the istream. The return value is important because it allows you to do multiple outputs in one statement. The order of execution is like this:

( ( (cout << "You entered \"") << str) << "\"") << endl;

Each parentises evaluates to cout and that is used in the next << operation.

You may be wondering what the \" means. It is just a place holder for the " character, which obviously you couldn't use otherwise.
 
thanks a million

we use iostream, so I am pretty familiar with it

i will try this tomorrow

the only thing thati dont get is whats the point of

using namespace std;

also, in all of our programs, we use

int main()

but i dont think that matters
 
Yeah, there are a bunch of different forms for the main function to do different things use whatever you want.

In c++ there are different namespaces. The reason is to avoid name collisions between different libraries. For example, Active Template Library and the Standard Template Library each have a class that is called vector. To avoid problems, you can have each one in a different library. The using namespace command specifies which ones to use by default. You can also access namespaces with the :: operator. For example if you didn't have the using statement, you could do std::cin instead of just cin.
 
If I recall correctly, string.h holds the functions for working with C style strings (i.e. null terminated character arrays).

string has the STL-like string class.
 
Hey, lads! Take a look at this approach… No “std namespace” is needed here. This example does not need to represent strings as instances of class std::string. Here, it is used the ideology of NULL-terminated strings (so well known to C/C++ programmers) downwards.

#include <string.h>
#include <stdio.h>

void main()
{
int iNum;
char* psz = new char[128];

/*
*scanf - grabs the input from the STDIN. Beware, for the input separator
*is SPACE, i.e. ‘\x20’ and it is up to you that psz has enough place to hold the
*incoming characters
*/
scanf("%s%d", psz, &iNum);
printf("your string is:\t%s\nyour number is:\t%d\n", psz, iNum);

strcat(psz, "-concatenated");

printf("once more:\t%s", psz);

delete [] psz;
}

You want cin and cout? No problem:

#include <string.h>
#include <iostream.h>

void main()
{
int iNum;
char* psz = new char[128];

cin >> psz >> iNum;
cout << "your string is:\t” << psz << endl << “your number is:\t” << iNum << endl;

strcat(psz, "-concatenated");

cout << "once more:\t” << psz << endl;

delete [] psz;
}

It is better to think twice before using namespaces with certain classes - everything depends upon the task being resolved...
 
Right, the major difference being that if you wrote it using the string class and the user enters something over 127 characters long it won't crash if you use the string class.

Don't get me wrong, I use null termenated character arrays all the time, but you have to do a lot of extra work to make sure you don't have a buffer overrun.

There are two ways to use character arrays. On the stack arrays can't be resized and so you must always allocate enough space for the largest possible input. Anything over that length, you will have to check and create an error message. The other option is on the heap, which takes a lot more coding to dynamically resize it with the input length.

The string class basically does all the extra work of using an on the heap character array for you.
 
I do not get you wrong, but provide an alternative classical way: strings are character null terminated arrays. I dislike arrays myself for they create a good deal of troubles when resizing (memory management stuff) or overflow handling (depends upon the source of incoming data).

When you want to use a string-like stuff it is better to keep thinking of the possible impediments and inconveniences, i.e. possibility of overflow, access/workout time, &c. You cannot use an instance of a class which “does a lot of” unnecessary “work” for you – you are to know “which work is actually needed” and how to effectively keep your data in safe. That is all.

Naturally, stacks are big enough in OSs running in protected mode. Memory paging is always available but truly it is a bad practice to allocate kilobytes of place for a damned array due memory fragmentations, isn't it? Every time we are to think what is better for us: classes or simple arrays. That’s it and nothing more :).
 
Back