Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Spaces?

novasheepnovasheep Member Posts: 29
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

  • Geeth AsokanGeeth Asokan Member Posts: 127
    : 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]
    :

    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,


  • novasheepnovasheep Member Posts: 29
    [b][red]This message was edited by novasheep at 2002-10-20 14:50:45[/red][/b][hr]
    any other ways? also ranks can have spaces it looks like there so that wont work, I think

  • mfeikmfeik Member Posts: 24
    : [b][red]This message was edited by novasheep at 2002-10-20 14:50:45[/red][/b][hr]
    : 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.


  • mfeikmfeik Member Posts: 24
    [b][red]This message was edited by mfeik at 2002-10-20 22:17:59[/red][/b][hr]
    : 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!



  • Geeth AsokanGeeth Asokan Member Posts: 127
    : [b][red]This message was edited by novasheep at 2002-10-20 14:50:45[/red][/b][hr]
    : 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,

  • Hacker4Hacker4 Member Posts: 3
    : 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?
    : 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;
    : }

  • PeterTheMasterPeterTheMaster Member Posts: 636
    [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]
    :



  • Geeth AsokanGeeth Asokan Member Posts: 127
    : [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].

  • PeterTheMasterPeterTheMaster Member Posts: 636
    im not sure what this reply refers to, so i make it clear again:

    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].
    :
    :

  • Shawn CarterShawn Carter Member Posts: 0

    ___ \ http://forcoder.org \ free ebooks and video tutorials about < Delphi Visual Basic C Go Scratch Objective-C MATLAB PL/SQL Visual Basic .NET C# PHP R Ruby Perl Swift Assembly Python C++ Java JavaScript ML Transact-SQL LabVIEW COBOL Erlang Alice F# Lisp D Clojure VBScript Crystal Bash SAS Apex ABAP Fortran Scheme Julia Dart Scala FoxPro Rust Lua Logo Awk Ada Prolog Kotlin Hack /> ____________

Sign In or Register to comment.