• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

stupid question in c++ (user input)

sigmend

[H]ard|Gawd
Joined
Aug 6, 2003
Messages
1,303
I am a nub at C++, and I have a stupid question. Let me say that I have also googled, but that managed to confuse me more.
In the simple program I am trying to get to work, I want to ask the person their name, then print a response.

Where I need help is, is how to let variable A be a string of text, I have no idea how to do this, or if I should be even using a variable.
hep?

Code:
#include <iostream>

int main (int argc, char * const argv[]) {
	char a;
	std::cout << "what is your name?\n";
	std::cin >> a;
	std::cout << a;
	std::cout << " is a nub\n";
    return 0;
}
outputs
Code:
what is your name?
matt
m is a nub

Is it because I touch myself at night? :(
 
Why not make "a" a string instead of a char? Don't have a compiler on this comp handy, but it shouldn't be a problem. Of course, you'd have to #include <string> to do so. And can't you just use "using namespace std;" below the includes to not have to use "std::"
on all those instructions?


EDIT: To be more helpful:

Code:
#include <iostream>
#include <string>

using namespace std;

int main () {
	string a;
	cout << "what is your name?" << endl;
	cin >> a;
	cout << a << " is a nub" << endl;
    return 0;
}
 
Thanks, that worked great.
I am teaching myself C++ by examples. Thats how I learned PHP, HTML, and bash scripting
 
The reason why the input 'matt' didn't work is because it was a char type. The char type will hold only one character, which is why m, the first character of 'matt' was displayed.

Also, if you're going to be entering strings, the <string> header file is much better than using say, char or a char string (C-style strings).

The other way you could have done this (using the C way if you really wanted, would have been:

Code:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{
    char myString[10];
    cout << "What is your name?" << endl;
    cin >> myString;
    cout << myString << " is a nub" << endl;

    return 0;
}

However, whenever possible, you should favor C++'s <string> instead of C-style srings.

Also, like Anarchonixx says, you should put the line 'using namespace std' so you don't have to do std:: everytime you access a standard library function. In my code though, you see that I use 'using std::cout' and such. This is just to import those functions from the std namespace so I can use cout, cin, and endl without using the std::, but with other standrd namespace names, I'd still have to use std::.
 
In addition, the preferred c++ string method example

Code:
#include <iostream>
#include <string>

using namespace std;

int main () {
    cout << endl << "What is your name? ";
    string name;
    getline(cin,name);
    cout << endl << name << " is a n00b!" << endl;
}
 
Back
Top