I have a very simple question, which I solved yesterday but forgot how I did it today! LOL

So it is like this:

I have a program written in C++ which in CMD writes: " Type your name to proceed"...

And in the program i set a special name, and when the user types it in the program, then it proceeds.

The problem is, that...I cannot state the variable as:

int z; nor char z;

BECAUSE I want to type in some letters in CMD and the program stores the TEXT into the variable..and none of them (int, char) can do it. Yesterday I somehow guessed, it was another name in front of the variable but now I cannot find it ( I know I am lame ).


:::::::::::::::::::::::::::::::::::::::::::::::::
#include <iostream>

using namespace std;

int main()

{
char z; We want to change this " char" into a word that would be compatibile as a variable with text, not numbers.

cout<<"Please enter a number:""\n";
cin>>z;
cin.get();
cout<<"You entered: " << z << "\n";
cin.get();
}

And people, please do not give me very complicated solutions, the whole thing is about stating the variable "z" as something else but "int" or "char". It is one word that would make the whole thing work, please help me :P

The best solution would be store the input in an STL string object. If you aren't familiar with them, then you could use a null terminated char array to store the same information.

string stlString; //include the string header file to do this type

const int MAXSIZE = 1086;
char szSttring[MAXSIZE];

The best solution would be store the input in an STL string object. If you aren't familiar with them, then you could use a null terminated char array to store the same information.

string stlString; //include the string header file to do this type

const int MAXSIZE = 1086;
char szSttring[MAXSIZE];

I know this one, yes.
But this is a very specific case, and I want to know what to state the variable as to make the problem store the text I type in the CMD window as the variable.

Yesterday I wrote ONE word, instead of " int z; " I wrote some other word and it all worked.

Any ideas :) ?

What are you trying to again? Just need a bit more explanation.

Try using char *z;

I know this one, yes.
But this is a very specific case, and I want to know what to state the variable as to make the problem store the text I type in the CMD window as the variable.

Yesterday I wrote ONE word, instead of " int z; " I wrote some other word and it all worked.

Any ideas :) ?

I would use getline ( cin, z );

Sequences of characters (i.e. 'words' or 'plain text') are known as strings. In C++, strings are represented as a type of data called string

#include <string>  //allows you to use 'string'
#include <iostream>

int main()
{
    using namespace std;

    string name;
    cout << "What is your name?" << endl;
    getline(cin, name);
    cout << "Hello, " << name << endl;
}

Output

What is your name?
Joe Bloggs
Hello, Joe Bloggs

You can use >> aswell if you really want to, but be careful that >> only reads up to the first 'whitespace' character and leaves the rest of your input alone to potentially trip you up later on. (Which means that if you're on a modern desktop system, the next time you try to do something with 'cin' the first input you'll encounter will be whatever was left over)

#include <string>  //allows you to use 'string'
#include <iostream>

int main()
{
    using namespace std;

    string name;
    cout << "What is your name?" << endl;
    cin >> name;
    cout << "Hello, " << name << endl;
}

Output

What is your name?
Joe Bloggs
Hello, Joe
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.