Notices

Overclockers Forums > Software > Programming Tips and Tricks
Programming Tips and Tricks
Forum Jump

No Strings in C++?

Post Reply New Thread Subscribe Search this Thread
 
 
Thread Tools
Old 04-07-04, 02:11 PM Thread Starter   #1
klown22
New Member



Join Date: Mar 2004

 
No Strings in C++?


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
klown22 is offline   QUOTE Thanks
Old 04-07-04, 03:01 PM   #2
mccoyn
Senior Member

 
mccoyn's Avatar 

Join Date: Nov 2003
Location: Michigan, USA

 
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;

__________________
Once there was a caterpillar who had commas for legs. Which meant he had to allow a literary pause after each step. The other caterpillars really respected him for it and he came to have quite a commanding presence. Oh, and talk about a philanthropist! He was notorious for giving fresh leaves to those less-fortunate.
mccoyn is offline   QUOTE Thanks
Old 04-07-04, 03:11 PM Thread Starter   #3
klown22
New Member



Join Date: Mar 2004

 
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
klown22 is offline   QUOTE Thanks
Old 04-07-04, 03:29 PM   #4
mccoyn
Senior Member

 
mccoyn's Avatar 

Join Date: Nov 2003
Location: Michigan, USA

 
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.

__________________
Once there was a caterpillar who had commas for legs. Which meant he had to allow a literary pause after each step. The other caterpillars really respected him for it and he came to have quite a commanding presence. Oh, and talk about a philanthropist! He was notorious for giving fresh leaves to those less-fortunate.
mccoyn is offline   QUOTE Thanks
Old 04-07-04, 03:48 PM Thread Starter   #5
klown22
New Member



Join Date: Mar 2004

 
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
klown22 is offline   QUOTE Thanks
Old 04-07-04, 07:05 PM   #6
mccoyn
Senior Member

 
mccoyn's Avatar 

Join Date: Nov 2003
Location: Michigan, USA

 
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.

__________________
Once there was a caterpillar who had commas for legs. Which meant he had to allow a literary pause after each step. The other caterpillars really respected him for it and he came to have quite a commanding presence. Oh, and talk about a philanthropist! He was notorious for giving fresh leaves to those less-fortunate.
mccoyn is offline   QUOTE Thanks
Old 04-07-04, 09:58 PM   #7
MRD
Senior Member

 
MRD's Avatar 

Join Date: Feb 2003
Location: CT

10 Year Badge
 
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.
MRD is offline   QUOTE Thanks
Old 04-08-04, 12:52 AM   #8
lex57ukr
Registered



Join Date: Feb 2004
Location: Orem, UT

 
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...
lex57ukr is offline   QUOTE Thanks
Old 04-08-04, 07:02 AM   #9
mccoyn
Senior Member

 
mccoyn's Avatar 

Join Date: Nov 2003
Location: Michigan, USA

 
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.
mccoyn is offline   QUOTE Thanks
Old 04-08-04, 09:07 AM   #10
lex57ukr
Registered



Join Date: Feb 2004
Location: Orem, UT

 
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 .
lex57ukr is offline   QUOTE Thanks

Post Reply New Thread Subscribe


Overclockers Forums > Software > Programming Tips and Tricks
Programming Tips and Tricks
Forum Jump

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Mobile Skin
All times are GMT -5. The time now is 07:47 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
You can add these icons by updating your profile information to include your Heatware ID, Benching Profile ID or your Folding/SETI profile ID. Edit your profile!
X

Welcome to Overclockers.com

Create your username to jump into the discussion!

New members like you have made this the best community on the Internet since 1998!


(4 digit year)

Why Join Us?

  • Share experience
  • Max out your hardware
  • Best forum members anywhere
  • Customized forum experience

Already a member?