944,045 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 4650
  • C++ RSS
Jun 3rd, 2005
0

Pressing Enter on an empty string - Help

Expand Post »
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.
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
evil_dude_01 is offline Offline
15 posts
since Jan 2005
Jun 3rd, 2005
0

Re: Pressing Enter on an empty string - Help

C++ Syntax (Toggle Plain Text)
  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. */
C++ Syntax (Toggle Plain Text)
  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. */
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 4th, 2005
0

Re: Pressing Enter on an empty string - Help

Thank you, i have got it working now.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
evil_dude_01 is offline Offline
15 posts
since Jan 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: OpenGL, help packaging to allow others to run it
Next Thread in C++ Forum Timeline: openGL problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC