User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,755 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,580 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1660 | Replies: 2 | Solved
Reply
Join Date: Jan 2005
Posts: 15
Reputation: evil_dude_01 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
evil_dude_01 evil_dude_01 is offline Offline
Newbie Poster

Help Pressing Enter on an empty string - Help

  #1  
Jun 3rd, 2005
Hi all, I'm completly useless at C++ and was wondering if anyone can tell me how to tell a program that when I have pressed Enter, if the string is empty do nothing.

Its for updating details. I'm using an Input header file that my lecturer wrote.

This works fine for updating a student number.
//Amend Student Number
	cout << "Change Student Number From [" << oldrecord.stuno << "] to: ";
	Input(tmprecord.stuno);
		
	if ( tmprecord.stuno == 0 )	// if return pressed only
		newrecord.stuno = oldrecord.stuno;
	else
		newrecord.stuno = tmprecord.stuno;	

But i am not sure how to do it for a string
//Amend First Name
	cout << "Change Student First Name From [" << LEFT(21) << oldrecord.firstname.Out << "] to: ";
		
	Input(tmprecord.firstname,21);
		
	if ( tmprecord.firstname,21 == LEFT(1) << " " )	// if return pressed only
		newrecord.firstname = oldrecord.firstname;
	else
		newrecord.firstname = tmprecord.firstname;

I know that LEFT(1) << " " doesn't work. Using == 0 the program works but the fields are empty after updating.

Thanks
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,473
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 141
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Pressing Enter on an empty string - Help

  #2  
Jun 3rd, 2005
#include <iostream>
#include <string>
using namespace std;

int main(void)
{
   string text;
   do
   {
      cout << "prompt: ";
      getline(cin, text);
   } while ( text[0] == '\0' );
   cout << "text = " << text << endl;
   return 0;
}

/* my output
prompt: 
prompt: 
prompt: 
prompt: hello world
text = hello world
*/
#include <iostream>
#include <string>
using namespace std;

int main(void)
{
   string text("text"), response(text);
   do
   {
      cout << "prompt <" << text << "> : ";
      getline(cin, response);
   } while ( response[0] == '\0' );
   text = response;
   cout << "text = " << text << endl;
   return 0;
}

/* my output
prompt <text> : 
prompt <text> : 
prompt <text> : hello
text = hello
*/
Reply With Quote  
Join Date: Jan 2005
Posts: 15
Reputation: evil_dude_01 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
evil_dude_01 evil_dude_01 is offline Offline
Newbie Poster

Re: Pressing Enter on an empty string - Help

  #3  
Jun 4th, 2005
Thank you, i have got it working now.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 8:30 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC