Page 1 of 1

c++ variables

Posted: Mon Feb 02, 2009 8:28 pm
by Matte
Hi, I was trying to get a very little program to ask for your name, and then write it down. The problem is that I didn't know what variable to use, since my tutorial only says the following three things:
int stores numbers without decimals
char stores one character
float stores numbers with decimals
Then how do I store words?

Thanks!
PS Here's my code, if it can help:

Code: Select all

#include <iostream>

using namespace std;

int main()
{
char geschreven;

cout<<"Please type your name\n";
cin>> geschreven;
cin.ignore();

cout<<"Hi "<<geschreven <<"\n";
    cin.get();
}
EDIT: I'm sorry for the wrong forum, didn't notice after posting, reported my own post already. Please move this to general discussions, thanks!

Re: c++ variables

Posted: Mon Feb 02, 2009 8:39 pm
by Juutis
You should use a string. I don't have much experience in C++ but I believe it's actually an object, just like in Java. I'm sure a quick Google search on 'C++ string' or something will give what you're after.

Re: c++ variables

Posted: Mon Feb 02, 2009 8:50 pm
by Zidane
Char is for a only character.

You use char *

char *name;

Re: c++ variables

Posted: Mon Feb 02, 2009 9:04 pm
by Matte
Thanks alot!

Re: c++ variables

Posted: Mon Feb 02, 2009 11:00 pm
by QuestOfDreams
Zidane wrote:Char is for a only character.

You use char *

char *name;
Well, without further explanation this is not correct ... the correct answer would be a character array or a string, e.g.
char str[200];
std::string str;
char* is just a pointer that needs to point to another variable (either a static array like above or dynamically allocated memory) before it can be used

Suggested reading:
http://www.cprogramming.com/tutorial/lesson9.html
http://www.cprogramming.com/tutorial/string.html

Re: c++ variables

Posted: Mon Feb 02, 2009 11:41 pm
by Zidane
Bueno sin mas explicacion , tengo que decir que es totalmente correcto para hacer un hola munod. Un char * es un puntero que aputna a la direccion donde empieza la cadena, o sea que es un array o vector al igual que string. Lo que pasa que no se reserva memoria dinamicamente pues para hacer un simple hola munod no creo que merezca la pena, pues como todo el mundo sabe char *str apunta a la primera direccion, o sea char *str es igual a char str[0];
char *cad1=""; char *cad1="cadena de texto"; son declaraciones correctas para manejar vectores de caracteres


Sin mas explicacion... lo pongo en espaƱol poruqe no se explicaro bien en ingles. Si te molesta lo siento mucho

Re: c++ variables

Posted: Tue Feb 03, 2009 1:56 am
by QuestOfDreams
Just to remind you ... there's a reason for the board posting rules ...
6. All posts must be in English.
If I understand you correctly you're talking about pointers to compiler allocated memory/strings. This is of course possible but irrelevant in this case as he wants to read a string into a variable. Using a pointer to a compiler allocated string will most probably result into access violations and/or memory leaks if the input string is larger than the compiler allocated memory.

Re: c++ variables

Posted: Wed Feb 04, 2009 4:39 pm
by paradoxnj

Code: Select all

#include <string>
#include <iostream>

int main(int argc, char **argv)
{
    std::string          name;

    std::cout << "What is your name??  ";
    std::cin >> name;
    std::cout << std::endl << "Your name is " << name << std::endl;
    return 0;
}