c++ variables

Discuss any other topics here
Post Reply
Matte
Posts: 321
Joined: Fri Oct 19, 2007 7:49 pm

c++ variables

Post 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!
User avatar
Juutis
Posts: 1511
Joined: Thu Jan 12, 2006 12:46 pm
Location: Finland

Re: c++ variables

Post 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.
Pain is only psychological.
User avatar
Zidane
Posts: 153
Joined: Mon Feb 11, 2008 1:21 am
Location: Spain

Re: c++ variables

Post by Zidane »

Char is for a only character.

You use char *

char *name;
Matte
Posts: 321
Joined: Fri Oct 19, 2007 7:49 pm

Re: c++ variables

Post by Matte »

Thanks alot!
User avatar
QuestOfDreams
Site Admin
Posts: 1520
Joined: Sun Jul 03, 2005 11:12 pm
Location: Austria
Contact:

Re: c++ variables

Post 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
User avatar
Zidane
Posts: 153
Joined: Mon Feb 11, 2008 1:21 am
Location: Spain

Re: c++ variables

Post 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
User avatar
QuestOfDreams
Site Admin
Posts: 1520
Joined: Sun Jul 03, 2005 11:12 pm
Location: Austria
Contact:

Re: c++ variables

Post 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.
User avatar
paradoxnj
RF2 Dev Team
Posts: 1328
Joined: Wed Mar 01, 2006 7:37 pm
Location: Brick, NJ
Contact:

Re: c++ variables

Post 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;
}
Many Bothans died to bring you this signature....
Post Reply