Follow us on Twitter!
Imagination is more valuable than knowledge - Albert Einstein
Saturday, April 19, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 26
Guests Online: 23
Members Online: 3

Registered Members: 82838
Newest Member: w1zarrd
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

C++ variable containing spaces

Uber0n
Member



Posts: 1963
Location: Sweden‭‮
Joined: 13.06.06
Rank:
Hacker Level 3
Posted on 27-09-06 09:17
Hi!

I'm writing an application which takes user input and stores it in an array of chars. But how can I make it store spaces?
Like if the user types "Hello World" (without quotes) the variable will contain "Hello World", not just "Hello" and the next time it's called it automatically fills in "World"?

I've also tried using a string to store the data, but without success.

I hope you understand what I mean... Pfft

Peace B)


img230.imageshack.us/img230/724/uber0nsig3hj6.gif
http://uber0n.web. . .
Nope http://uber0n.webs.com/
Author

RE: C++ variable containing spaces


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 27-09-06 12:37
If you're working from a command line sort of prompt:

Code

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

using namespace std;

string input;

int main(int argc, char *argv[])
{
    cout<<"Type a string\n";
   
    std::getline(std::cin, input); // Delimits at the '/n/r' (Enter Key) instead of a space
   
    cout<<input<<endl;
    system("PAUSE"); // Remove if not on WinXP
    return 0;
}





It's not the prettiest code, but I just woke up 20 minutes ago. Enjoy.
Author

RE: C++ variable containing spaces


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 01-10-06 20:12
Code
#include <iostream>
#include <string>
using namespace std;

int main()
{
    cout << "Enter your name: ";
    string name;
    getline(cin,name);
    cout << "\nYour name is " << name << endl;
    system("pause");
    return 0;
}



or you can put.....
Code
#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    cout << "What is your name?\n";
    char name[40];
    cin.getline(name,39); //39 because we need 1 byte for the terminating byte
    cout << "Your name is " << name << endl;
    system("pause");
    return 0;
}
   



Author

RE: C++ variable containing spaces

Uber0n
Member



Posts: 1963
Location: Sweden‭‮
Joined: 13.06.06
Rank:
Hacker Level 3
Posted on 01-10-06 20:18
Yes I found that the day after I posted Grin
But thanks a lot anyway!

Peace B)


img230.imageshack.us/img230/724/uber0nsig3hj6.gif
http://uber0n.web. . .

Edited by Uber0n on 01-10-06 20:18
Nope http://uber0n.webs.com/