CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2013
    Location
    Gastonia, NC
    Posts
    11

    concatenation of strings when entering city, state and zip code

    I have written my code, but can't seem to figure out why it doesn't enter the while loop and start asking for Input. Any insight would be greatly appreciated. Just need a direction to go in.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	//declare variables
    	string cityName = "";
    	string stateName = "";
    	string zipCode = "";
    	string fullAddress = "";
    	string loopValue = "";
    
    	cout << "Enter -1 to end or press enter to key in a date: ";
    	getline(cin, loopValue);
    
    	while (loopValue != "-1");
    	{
    		cout << "Enter city: ";
    		getline(cin, cityName);
    
    		cout << "Enter state: ";
    		getline(cin, stateName);
    
    		cout << "Enter zip code: ";
    		getline(cin, zipCode);
    
    		fullAddress = cityName + ", " + stateName + "  " + zipCode;
    		cout << fullAddress << endl;
    
    		cout << "Enter -1 to end or press enter to key in a date: ";
    		getline(cin, loopValue);
    	}
    
    	cin.get();
    	return 0;
    } //end of main function

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: concatenation of strings when entering city, state and zip code

    Took all of 2 or 3 seconds to figure out using the debugger.

    while (loopValue != "-1");

    Typically you'd use an int or a bool to control a loop rather than a string, but that's not your problem here, just a good habit.

  3. #3
    Join Date
    Jan 2013
    Location
    Gastonia, NC
    Posts
    11

    Re: concatenation of strings when entering city, state and zip code

    Lol something so simple...thank you so much. I must be getting tired.

  4. #4
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: concatenation of strings when entering city, state and zip code

    Quote Originally Posted by f0rumh4x View Post
    Code:
    	string cityName = "";
    What is the reason for explicitly initializing string variables with the empty string?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured