how to input two names same string

This is a discussion on how to input two names same string within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <string> using namespace std; int main(void) { string name; cout << "What is your name?" << ...

  1. #1
    Shadow12345
    Guest

    how to input two names same string

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(void) {
    	string name;
    	cout << "What is your name?" << endl;
    	getline(cin,name,'\n');
    	
    	cout << "Your name is " << name << endl;
    
    	return 0;
    }
    This code works, but for some reason you have to press enter twice, and that stinks, is there any way not to have to press enter twice?

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(void)
    {
    	string name;
    	cout << "What is your name?" << endl;
    	cin >> name;
    	
    	cout << "Your name is " << name << endl;
    
    	return 0;
    }

  3. #3
    Shadow12345
    Guest
    no no no, when you do it that way you can ONLY enter one word until you hit a space. with my way you can enter as many words with as many spaces but you must his enter twice, i wanted to know how to get around hitting enter twice.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    use cin.getline(), or the tried and true, (and reliable ) fgets().
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  5. #5
    Sir Mister Insane Sako Klinerr1's Avatar
    Join Date
    May 2002
    Posts
    608
    using the dogs way you can enter multiple spaces into your variable. you can do that wiht any cin!
    Email: Klinerr1@nc.rr.com || AIM: MisterSako || MSN: sakotheinsane@hotmail.com

    -the shroom has spoken

  6. #6
    Shadow12345
    Guest
    fgets()? i've never heard of that before but okay

    also cin.getline() is used for character arrays not strings
    Last edited by Shadow12345; 07-28-2002 at 03:23 PM.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Ahhh..."strings". I thought you meant char array "strings". Well fgets() won't work there!
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  8. #8
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    There is still a getline free function.

    Code:
    #include <iostream>
    #include <string>
    
    int main() {
    	std::string line;
    	
    	std::cout << "Enter your name: ";
    	std::getline(std::cin, line);
    	
    	std::cout << "Your name is " << line << std::endl;
    	
    	return 0;
    }

  9. #9
    Shadow12345
    Guest
    well I guess no one reads previous posts anymore. The way Silent did it works fine EXCEPT for the fact that I already did it that way (read the first post of this thread). I am trying to find a way that works so you don't have hit the enter key twice.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    68

    Try This

    Hi U,

    I did only press enter once. I don't what you are taking about but according to your title

    >>>how to input two names same string \

    I think you what these
    Thanks

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(void) {
            string name1 = "",name2="";
    
            cout << "What is your name?" << endl;
            cin>>name1>>name2;
    
            cout << "Your name is " << name1<<","<<name2<< endl;
    
            return 0;
    }

  11. #11
    Shadow12345
    Guest
    that seems to work fine, but I was wondering if there was a way to use the getline(cin,name) function so that you only have to press enter once to go to the next line.

  12. #12
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    It only requires one press of enter for me.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - String input header
    By Nextstopearth in forum C Programming
    Replies: 10
    Last Post: 06-10-2009, 11:25 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. c++ string input
    By R.Stiltskin in forum C++ Programming
    Replies: 4
    Last Post: 02-22-2003, 03:25 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21