Join us at IRC!
You cannot teach a man anything; you can only help him find it within himself. - Galileo
Thursday, May 24, 2012
Navigation
Members Online
Total Online: 34
Web Spiders: 16
Guests Online: 34
Members Online: 0

Registered Members: 70188
Newest Member: nuk3d
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Help with inputting strings into arrays in c++?

bostikpaste1
Member

Posts: 11
Location: Australia
Joined: 05.12.08
Rank:
Moderate
Posted on 01-12-09 13:18
I want to be able to receive string input from a user and store it in a way that will allow me to operate with individual characters in the string. An array was my first thought but that does not allow me to put spaces in. The other option was a string using getline() to allow for spaces but this does not allow me to operate with individual characters. Any help would be appreciated.
Thanks.
freddymcfudge@hotmail.com
Author

RE: Help with inputting strings into arrays in c++?

yours31f
Second to one



Posts: 1678
Location: Dallas Texas
Joined: 27.04.07
Rank:
Satan
Posted on 01-12-09 14:28
Stings are arrays


string example = "This is a string.";

cout << example[6]



i



Debugging is what programmers do to beta software to make it take up more room on your hard drive if it is running too efficiently.





Edited by yours31f on 01-12-09 14:29
yours31f@live.com yours31f@yahoo.com rpwd.info
Author

RE: Help with inputting strings into arrays in c++?

x_5631
Member

Posts: 156
Location: Uk
Joined: 22.06.07
Rank:
Mad User
Posted on 01-12-09 15:01
yours31f wrote:
Stings are arrays


string example = "This is a string.";

cout << example[6]



i


Like he said, they are arrays.. so you just need a simple for loop


#include <iostream>
#include <string>

using namespace std;

int main(){
string test;
test = "Whatever the string is to say";

int i;
char c;
for (i=0;i <= len(test);i++){
// do whatever you need to the char
c = test[i];
}
return 0;
}



[Edit]
Just found a useful little site for compiling C/C++. Useful for checking quick scblockedripts if you're on a public computer or whatever.
http://www.comeaucomputing.com/tryitout/

Edited by x_5631 on 01-12-09 15:02
Author

RE: Help with inputting strings into arrays in c++?

ynori7
Future Emperor of Earth



Posts: 1481
Location: #valhalla
Joined: 08.10.07
Rank:
Diabolical
Posted on 01-12-09 16:03
Here are the two best ways that I see for you to approach this:
#include<iostream>
#include<string>
#include<cstdio>
using namespace std;

char changeChar(char);

int main()
{
string test;
getline(cin, test);
cout<<test[1]<<endl;
test[1]='a';
cout<<test<<endl;

char input[255];
fgets(input, 255, stdin);
cout<<input[1]<<endl;
input[1]='a';
cout<<input<<endl;
}

Both methods should have the same output.




ynori7 http://halls-of-valhalla.org
Author

RE: Help with inputting strings into arrays in c++?

bostikpaste1
Member

Posts: 11
Location: Australia
Joined: 05.12.08
Rank:
Moderate
Posted on 02-12-09 03:36
Ok, I feel like a bit of an idiot now. Thanks guys.
freddymcfudge@hotmail.com
Guest
Username

Password

Remember Me


Bookmark This Page
Affiliates
Adverts

 

 

Links
By using, viewing or obtaining any information contained on this site, you agree to the disclaimer.

© HellBound Hackers 2008- 2009. Since 3rd December 2004.