Jump to content

Storing names

- - - - -

  • Please log in to reply
20 replies to this topic

#1
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts
hello Guys I don't know if the title is confusing but this description of what I'm trying to do might be more understandable.

So Im trying to get something like this to work

cout<<"hello my name is the computer, what is your name?<<endl;

cin >> name;

I have name has an ( int) and it gives me a value not a letter, and when i say (double name) I receive a huge decimal number.

So how can i fix this

#2
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
Well, if you want a string, then you shouldn't declare it as an int. an int is for number values only. you would declare it something like
char name[STRING_LENGTH];
where STRING_LENGTH is how many characters are in the string.
Latinamne loqueris?

#3
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts

mebob said:

Well, if you want a string, then you shouldn't declare it as an int. an int is for number values only. you would declare it something like
char name[STRING_LENGTH];
where STRING_LENGTH is how many characters are in the string.

Sorry I haven't been far ahead on the tutorials I'm watching and i only have done int, doubles, while loops, bool, ( I still can't understand bools that well)

Character input and output.

Any good references on how to use STRING_LENGTH?

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
It is just a definition. If your name is to be eight characters, you can define it as such:
#define NAME_LEN 8

int main() {
  char string[NAME_LEN];
}

The compiler will automatically substitute NAME_LEN for 8 as if the definition were never there (your code will see 8)

Although I would recommend you look up on how to use std::string (C++ strings), using char pointers, i.e. char* or char foo[] is mainly in C and C++ provides easier string classes.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
That was just a placeholder for a number. An equivalent of what Alexander said:

int main()

{

char string[8];

}


Latinamne loqueris?

#6
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts

Alexander said:

It is just a definition. If your name is to be eight characters, you can define it as such:
#define NAME_LEN 8


int main() {

  char string[NAME_LEN];

}

The compiler will automatically substitute NAME_LEN for 8 as if the definition were never there (your code will see 8)

Although I would recommend you look up on how to use std::string (C++ strings), using char pointers, i.e. char* or char foo[] is mainly in C and C++ provides easier string classes.

mebob said:

That was just a placeholder for a number. An equivalent of what Alexander said:

int main()

{

char string[8];

}


How about if I want the computer to say a name which was stored like how are you: ( name stored)

without having too worry how big the name is

#7
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts

Alexander said:

It is just a definition. If your name is to be eight characters, you can define it as such:
[/CODE]

The compiler will automatically substitute NAME_LEN for 8 as if the definition were never there (your code will see 8)

Although I would recommend you look up on how to use std::string (C++ strings), using char pointers, i.e. char* or char foo[] is mainly in C and C++ provides easier string classes.

mebob said:

That was just a placeholder for a number. An equivalent of what Alexander said:

Never mind guys I Got this right

#include <iostream>

using namespace std;


int main ()

{

    cout<<"Hello Please tell me your name:";

 char name[6]; 

 cin >> name;

 cout<<" Nice to meet you " <<name<<endl;

  

 system("pause");

 return 0;   

}



#8
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
that has a name length limit of 6 characters, not enough for many names. Actually, only 5 characters, because the null terminator takes up one. If you don't want to worry about the length, use C++ strings. Here is a "conversion" of your program for C++ strings:

#include <iostream>

#include <string>

using namespace std;


int main ()

{

 cout<<"Hello Please tell me your name:";

 string name;

 cin >> name;

 cout<<" Nice to meet you " <<name<<endl;

  

 system("pause");

 return 0;   

}

Personally, I prefer C strings like you have currently. But with C++ strings you won't have to worry about the length, it expands automatically.
Latinamne loqueris?

#9
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
@vaironl, note that std::cin only reads up to white space whereas getline reads the whole line (until enter is pressed) or up to delimiter (if you specify one).
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#10
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
Yes, then you could take a full name with spaces. You would use the getline function like this with C++ strings:

#include <iostream>

#include <string>

using namespace std;


int main ()

{

 cout<<"Hello Please tell me your name:";

 string name;

 getline(cin,name);

 cout<<" Nice to meet you " <<name<<endl;

  

 system("pause");

 return 0;   

}

With C strings, you would use cin.getline()
Latinamne loqueris?

#11
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts

mebob said:

Yes, then you could take a full name with spaces. You would use the getline function like this with C++ strings:

#include <iostream>

#include <string>

using namespace std;


int main ()

{

 cout<<"Hello Please tell me your name:";

 string name;

 getline(cin,name);

 cout<<" Nice to meet you " <<name<<endl;

  

 system("pause");

 return 0;   

}

With C strings, you would use cin.getline()

Sorry to ask this but I got very confused with the
getline (cin, name);
If I say string name; do I have to say getline?

#12
wwarren

wwarren

    Learning Programmer

  • Members
  • PipPipPip
  • 60 posts

Quote

If I say string name; do I have to say getline?

When you define name like this

string name;

It's like creating a bucket. There's nothing in it, and your computer can't just guess what's supposed to go in there.

Then this line:

getline(cin, name);

This tells your program to look at cin (which is the computer's input), get its value (up to the end of the line) and put it's value into your empty bucket: name.

Does this clarify it for you?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users