string variables?

string variables?

Post by chrisx16x2008 on Mon Apr 06, 2009 8:13 pm
([msg=21332]see string variables?[/msg])

I'm kinda new to C++ and I haven't been able to find any "string" variables. The closest I have found is Char but its only supposed to hold one byte. Can someone help me out?


I just realized I posted this in the Programming mission section. :oops: Moderator please move.
chrisx16x2008
New User
New User
 
Posts: 36
Joined: Tue Jan 13, 2009 9:53 pm
Blog: View Blog (0)


Re: string variables?

Post by TheMindRapist on Mon Apr 06, 2009 9:10 pm
([msg=21333]see Re: string variables?[/msg])

http://tinyurl.com/cay5lo
Also, moved to General Programming forum.
Edit: The moral of this post is that Google is your friend.
Image
User avatar
TheMindRapist
Contributor
Contributor
 
Posts: 585
Joined: Mon Apr 14, 2008 4:57 pm
Blog: View Blog (0)


Re: string variables?

Post by chrisx16x2008 on Mon Apr 06, 2009 9:35 pm
([msg=21335]see Re: string variables?[/msg])

I tried google before this post. maybe this would help more...

Code: Select all
#include "stdafx.h"
#include "iostream"
#include <windows.h>
#include <string>

using namespace std;

int i = 1;

string shiftkey()
{
   if (GetAsyncKeyState(VK_SHIFT) & 0x8000)
   {
      for(int a = 0; a < 1; a++)
      {
      string str3("it works!");
      }
   }
return str3;
}

int main()
{

cin.get();
return 0;
}


this is my code and i keep getting an 'str3' undeclared identifier error

Also I know there is some stuff I don't need in there like int i = 1. That is just some stuff I used for some code i deleted from it.
chrisx16x2008
New User
New User
 
Posts: 36
Joined: Tue Jan 13, 2009 9:53 pm
Blog: View Blog (0)


Re: string variables?

Post by mischief on Tue Apr 07, 2009 3:57 am
([msg=21346]see Re: string variables?[/msg])

in C and C++ you can use C-style strings like the following:

Code: Select all
char *str = "this is a string literal";


this is a character pointer initialized from a string literal.
in c-style strings, the string is termiated with a null ("\0") so that a program knows when to stop processing it, because it goes through the memory sequentially, and stops when it hits a null.

in the standard, however, there is a string class. you can use them like so:

Code: Select all
#include <string>

int main() {
std::string s = "i am another string literal";
}


this will have s holding a copy of the string literal.

you can find more information on how to use the string class here -> http://www.cplusplus.com/reference/string/string/
The whole secret of existence is to have no fear. Never fear what will become of you, depend on no one. Only the moment you reject all help are you freed.
--Buddha
User avatar
mischief
Poster
Poster
 
Posts: 355
Joined: Wed Jan 07, 2009 4:16 pm
Blog: View Blog (0)


Re: string variables?

Post by damage_case on Wed Apr 08, 2009 12:48 am
([msg=21389]see Re: string variables?[/msg])

chrisx16x2008 wrote:I tried google before this post. maybe this would help more...

Code: Select all
#include "stdafx.h"
#include "iostream"
#include <windows.h>
#include <string>

using namespace std;

int i = 1;

string shiftkey()
{
   if (GetAsyncKeyState(VK_SHIFT) & 0x8000)
   {
      for(int a = 0; a < 1; a++)
      {
      string str3("it works!");
      }
   }
return str3;
}

int main()
{

cin.get();
return 0;
}


this is my code and i keep getting an 'str3' undeclared identifier error

Also I know there is some stuff I don't need in there like int i = 1. That is just some stuff I used for some code i deleted from it.


The problem is that you are trying to use object str3 ouside of it's scope (I'm not sure if that is a correct english term). You declared a local variable inside of 'for'-block so it's exists only inside of that block. See Stroustrup 3rd edition 4.9.4. But actually this is not a C++ specific thing, it's a general concept in programming.
damage_case
New User
New User
 
Posts: 7
Joined: Wed Mar 18, 2009 3:41 am
Blog: View Blog (0)


Re: string variables?

Post by chrisx16x2008 on Sun Apr 12, 2009 11:13 pm
([msg=21667]see Re: string variables?[/msg])

thanks!
chrisx16x2008
New User
New User
 
Posts: 36
Joined: Tue Jan 13, 2009 9:53 pm
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests

cron