Hey working on a little program here and when i enter a full name like BOB JONES it comes up with name BOB rank JONES and if i enter just BOB for a name and Master Sgt for rank it just has Master. How do I fix this? So there can be spaces?
[code]
#include #include #include main (void)
{
std::string name;
std::cout << "Enter name: ";
std::cin >> name;
std::string rank;
std::cout << "Enter Rank: ";
std::cin >> rank;
system("CLS");
std::cout << "Your name is: " << name << std::endl;
std::cout << "Rank of " << name << " is " << rank << std::endl;
return 0;
}
[/code]
Comments
:
: [code]
: #include
: #include
: #include
:
: main (void)
:
:
:
:
: {
: std::string name;
: std::cout << "Enter name: ";
: std::cin >> name;
:
: std::string rank;
: std::cout << "Enter Rank: ";
: std::cin >> rank;
:
: system("CLS");
:
: std::cout << "Your name is: " << name << std::endl;
:
: std::cout << "Rank of " << name << " is " << rank << std::endl;
:
: return 0;
: }
: [/code]
:
Hi,
You could use [b]cin.getline( , )[/b] in order to include spaces. Try out the following:
[code]
#include <iostream>
#include
int main()
{
// Declaring the variables
char name_and_surname_and_rank[50];
std::cout << "Please enter your name and rank in the form:" << std::endl;
std::cout << "Name Surname Rank: ";
std::cin.getline(name_and_surname_and_rank, 50);
std::cout << name_and_surname_and_rank << std::endl;
return 0;
}
[/code]
Note that [b]cin.getline( , )[/b] requires the header file [b]#include<cstring>[/b]
Hope this helps,
any other ways? also ranks can have spaces it looks like there so that wont work, I think
: any other ways? also ranks can have spaces it looks like there so that wont work, I think
:
:
Geeth gave you a good direction to follow. use cin.getline() and set the terminating char to '
' it will include any white space entered (space or ). That will allow for spaces in names and ranks.
: Hey working on a little program here and when i enter a full name like BOB JONES it comes up with name BOB rank JONES and if i enter just BOB for a name and Master Sgt for rank it just has Master. How do I fix this? So there can be spaces?
:
: [code]
: #include
: #include
: #include
:
: main (void)
:
:
:
:
: {
: std::string name;
: std::cout << "Enter name: ";
: std::cin >> name;
:
: std::string rank;
: std::cout << "Enter Rank: ";
: std::cin >> rank;
:
: system("CLS");
:
: std::cout << "Your name is: " << name << std::endl;
:
: std::cout << "Rank of " << name << " is " << rank << std::endl;
:
: return 0;
: }
: [/code]
:
also... consider using namespaces, it makes for more easily readable code.
REGARDS
-Mel
-------------------------------------------
Of all the things I've lost, I miss my mind the most!
: any other ways? also ranks can have spaces it looks like there so that wont work, I think
:
:
Hi,
Yes, there is another way using [b]strcat( , )[/b]. There is a good example online on the cprogramming website. The web link is as follows:
http://www.cprogramming.com/tutorial/print/lesson9.html/
But I still think [b]cin.getline( , )[/b] is the best way in order to overcome this problem. Note that you need the header file [b]#include [/b] in order to use the functions like [b]cin.getline( , )[b], [b]strcat( , )[/b].
Hope this helps,
: the changes that i made are just adding cin.getline(char*,int len);
len is how many characters MAX that you want to pull in. btw why are you using the standard templet library?
: [code]
: #include
: #include
: #include
:
: main ()
:{
: std::string name;
: std::cout << "Enter name: ";
: std::cin.getline(name,100);
: std::string rank;
: std::cout << "Enter Rank: ";
: std::cin.getline(rank,100);
:
: system("CLS");
:
: std::cout << "Your name is: " << name << std::endl;
:
: std::cout << "Rank of " << name << " is " << rank << std::endl;
:
: return 0;
: }
this way it works. getline(istream,string) is what you need.
something about the other replies:
there is no need for more namespaces in such a small program. usually namespaces are used when making libraries to prevent conflicts with names of user variables.
cin.getline(char*,int) has nothing to do with string.h
try to avoid the use of char*, std::string should be preferred.
:
: [code]
: #include
: #include
: #include
:
: main (void)
:
:
:
:
: {
: std::string name;
: std::cout << "Enter name: ";
: std::getline(std::cin, name);
:
: std::string rank;
: std::cout << "Enter Rank: ";
: std::getline(std::cin, rank);
:
: system("CLS");
:
: std::cout << "Your name is: " << name << std::endl;
:
: std::cout << "Rank of " << name << " is " << rank << std::endl;
:
: return 0;
: }
: [/code]
:
: this way it works. getline(istream,string) is what you need.
:
: something about the other replies:
:
: there is no need for more namespaces in such a small program. usually namespaces are used when making libraries to prevent conflicts with names of user variables.
:
: cin.getline(char*,int) has nothing to do with string.h
:
: try to avoid the use of char*, std::string should be preferred.
:
:
: :
: : [code]
: : #include
: : #include
: : #include
: :
: : main (void)
: :
: :
: :
: :
: : {
: : std::string name;
: : std::cout << "Enter name: ";
: : std::getline(std::cin, name);
: :
: : std::string rank;
: : std::cout << "Enter Rank: ";
: : std::getline(std::cin, rank);
: :
: : system("CLS");
: :
: : std::cout << "Your name is: " << name << std::endl;
: :
: : std::cout << "Rank of " << name << " is " << rank << std::endl;
: :
: : return 0;
: : }
: : [/code]
: :
:
:
:
:
I think the header file should be [b]#include <cstring>[/b].
cstring and string.h are (except std) the same, the latter is deprecated.
string is something different. string is needed for getline(istream,string), cstring is not.
: : [b][red]This message was edited by PeterTheMaster at 2002-10-31 13:7:4[/red][/b][hr]
: : this way it works. getline(istream,string) is what you need.
: :
: : something about the other replies:
: :
: : there is no need for more namespaces in such a small program. usually namespaces are used when making libraries to prevent conflicts with names of user variables.
: :
: : cin.getline(char*,int) has nothing to do with string.h
: :
: : try to avoid the use of char*, std::string should be preferred.
: :
: :
: : :
: : : [code]
: : : #include
: : : #include
: : : #include
: : :
: : : main (void)
: : :
: : :
: : :
: : :
: : : {
: : : std::string name;
: : : std::cout << "Enter name: ";
: : : std::getline(std::cin, name);
: : :
: : : std::string rank;
: : : std::cout << "Enter Rank: ";
: : : std::getline(std::cin, rank);
: : :
: : : system("CLS");
: : :
: : : std::cout << "Your name is: " << name << std::endl;
: : :
: : : std::cout << "Rank of " << name << " is " << rank << std::endl;
: : :
: : : return 0;
: : : }
: : : [/code]
: : :
: :
: :
: :
: :
:
: I think the header file should be [b]#include <cstring>[/b].
:
: