Need Help With Strings N Things

This is a discussion on Need Help With Strings N Things within the C++ Programming forums, part of the General Programming Boards category; Hey guys - I'm a c++ n00b, but I'm slowly starting to get the hang of it. For a bonus ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    8

    Need Help With Strings N Things

    Hey guys -

    I'm a c++ n00b, but I'm slowly starting to get the hang of it. For a bonus assignment in class, we have to make a little survey type application, and then the output just restates what the user put in (I guess to show we know how to I/O).

    I'm having trouble with the address and college major inputs. Because they both most likely will have spaces in whatever the user inputs, it's messing me all up! Please take a look at my code and tell me what I'm doing wrong!

    Code:
    // Personal Survey
    // Jennifer Tonon
    // 10-12-05
    // This progam stores personal information entered by a user, and repeats
    // it back to them for confirmation.
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
       int main()
       
       {
          char cName [15]; // user's first name
          char cLast [15]; // user's last name
          string cAddress; // user's street address
          char cCity [20]; // user's city
          char cState [15]; // user's state
          char cZip [5]; // user's zip code
          char cTelephone [10]; // user's telephone number
          string cMajor; // user's college major
          
          cout << "What is your first name? ";
          cin >> cName;
          cout << "What is your last name? ";
          cin >> cLast;
          cout << "What is your street address? ";
          getline(cin,cAddress,'\n');
          cout << "What is your city? ";
          cin >> cCity;
          cout << "What is your state? ";
          cin >> cState;
          cout << "What is your zip code? ";
          cin >> cZip;
          cout << "What is your telephone number? ";
          cin >> cTelephone;
          cout << "What is your major in college? ";
          getline(cin,cMajor,'\n');
          
        cout <<"Your first name is " << cName <<"\n";
        cout <<"Your last name is " << cLast <<"\n";
            	cout <<"Your street address is " << cAddress << "\n";
            	cout <<"Your city is "<< cCity <<"\n";
            	 cout <<"Your state is " << cState <<"\n";
            	cout <<"Your zip code is " << cZip << "\n";
           		cout <<"Your telephone number is " << cTelephone << "\n";
          		cout <<"Your major is " << cMajor << "\n";      		
    
          
    	  system("PAUSE");
          return 0;    
              
              
       }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    usless there is a reeeeeally goood reason for doing so, you should replace all those c-stylde character arrays with c++ std::string class. getline() will accept everything typed, including spaces. But you normally need to only specify the first two arguments to getline(). And don't forget the using namespace clause, or your compiler will complain like he!!
    Code:
    using std::string;
    using std::cin;
    using std::getline;
    string cMajor;
    getline(cin,cMajor);

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,324
    You are using cin>> to get some input, then later using getline to get some more input. This poses a problem because the two methods work a little differently.

    When you get input from a user, the user types in their data and hits <enter>. When you use cin>> to get that input from the user, it reads in the data but leaves the newline from the <enter> in the input stream. Normally this isn't an issue because it is skiped over the next time you use cin>>. However, the getline function doesn't skip over the newline character. Instead, it stops as soon as it reads the first one. So if you use getline after cin>>, you will not get any information because it will stop at the leftover newline.

    The solution is to add cin.ignore() after a call to cin>> that comes before a call to getline (or just do it after every call to cin>>).

    And I agree that all your variables should be strings instead of char arrays.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    8
    Thanks guys, your advice worked like a charm! You saved my butt big time!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strings in C++
    By elad in forum C++ Programming
    Replies: 11
    Last Post: 05-20-2006, 02:27 AM
  2. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:34 AM
  3. finding strings in strings
    By watshamacalit in forum C Programming
    Replies: 14
    Last Post: 01-11-2003, 12:08 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

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