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

Recommended Answers

All 2 Replies

#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
*/

Thank you, i have got it working now. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.