0

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

2
Contributors
2
Replies
3
Views
11 Years
Discussion Span
Last Post by evil_dude_01
0
#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
*/
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.