Pressing Enter on an empty string - Help

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2005
Posts: 15
Reputation: evil_dude_01 is an unknown quantity at this point 
Solved Threads: 0
evil_dude_01 evil_dude_01 is offline Offline
Newbie Poster

Pressing Enter on an empty string - Help

 
0
  #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.
  1. //Amend Student Number
  2. cout << "Change Student Number From [" << oldrecord.stuno << "] to: ";
  3. Input(tmprecord.stuno);
  4.  
  5. if ( tmprecord.stuno == 0 ) // if return pressed only
  6. newrecord.stuno = oldrecord.stuno;
  7. else
  8. newrecord.stuno = tmprecord.stuno;

But i am not sure how to do it for a string
  1. //Amend First Name
  2. cout << "Change Student First Name From [" << LEFT(21) << oldrecord.firstname.Out << "] to: ";
  3.  
  4. Input(tmprecord.firstname,21);
  5.  
  6. if ( tmprecord.firstname,21 == LEFT(1) << " " ) // if return pressed only
  7. newrecord.firstname = oldrecord.firstname;
  8. else
  9. 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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Pressing Enter on an empty string - Help

 
0
  #2
Jun 3rd, 2005
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main(void)
  6. {
  7. string text;
  8. do
  9. {
  10. cout << "prompt: ";
  11. getline(cin, text);
  12. } while ( text[0] == '\0' );
  13. cout << "text = " << text << endl;
  14. return 0;
  15. }
  16.  
  17. /* my output
  18. prompt:
  19. prompt:
  20. prompt:
  21. prompt: hello world
  22. text = hello world
  23. */
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main(void)
  6. {
  7. string text("text"), response(text);
  8. do
  9. {
  10. cout << "prompt <" << text << "> : ";
  11. getline(cin, response);
  12. } while ( response[0] == '\0' );
  13. text = response;
  14. cout << "text = " << text << endl;
  15. return 0;
  16. }
  17.  
  18. /* my output
  19. prompt <text> :
  20. prompt <text> :
  21. prompt <text> : hello
  22. text = hello
  23. */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 15
Reputation: evil_dude_01 is an unknown quantity at this point 
Solved Threads: 0
evil_dude_01 evil_dude_01 is offline Offline
Newbie Poster

Re: Pressing Enter on an empty string - Help

 
0
  #3
Jun 4th, 2005
Thank you, i have got it working now.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 3155 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC