C++ Programming Question

Discussion in 'OT Technology' started by Kingfisher, May 12, 2006.

  1. Kingfisher

    Kingfisher Keepin L's lit

    Joined:
    Mar 29, 2003
    Messages:
    4,736
    Likes Received:
    0
    Location:
    Southern California
    I can't get this simple thing to work and am getting frustered :hs: Any help would be *GREATLY* appreciated. Okay, I am trying to see whether the user enters a blank entry or not, using character arrays. Modifications to this code is welcome.

    I have:

    char string[80];
    cout << "Enter name (or press 'Enter' to Quit):";
    cin.getline(string,80);

    Now how do I test for newspace characters? (Where the user just presses the Enter key?) :greddy:
     
  2. Kingfisher

    Kingfisher Keepin L's lit

    Joined:
    Mar 29, 2003
    Messages:
    4,736
    Likes Received:
    0
    Location:
    Southern California
    anyone?? :wtc:
     
  3. Glennhuricane

    Glennhuricane New Member

    Joined:
    Apr 2, 2004
    Messages:
    6,070
    Likes Received:
    0
    Location:
    nj
    you can check if the char array is empty
    if it is empty then that means he didnt enter anything(or just pressed enter)

    are you in a c++ class??
    because it is going to get alot harder than this
     
  4. Kingfisher

    Kingfisher Keepin L's lit

    Joined:
    Mar 29, 2003
    Messages:
    4,736
    Likes Received:
    0
    Location:
    Southern California
    this is just a small small portion of a bigger program that i am working on
    do you mean like this:

    int i=0;
    char string[80];
    cout << "Enter name (or press 'Enter' to Quit):";
    cin.getline(string,80);
    while (string!='\n'){
    i++;
    }
    if (i == 0)
    cout << "Enter pressed" << endl;

    ????


    EDIT: it still doesn't work.. help! :hs:
     
  5. Glennhuricane

    Glennhuricane New Member

    Joined:
    Apr 2, 2004
    Messages:
    6,070
    Likes Received:
    0
    Location:
    nj
    well what doesnt work???

    i think you need a delimiter in your getline
    something like cin.getline(string,80, '\n') that will keep reading input until you hit enter
     
  6. Kingfisher

    Kingfisher Keepin L's lit

    Joined:
    Mar 29, 2003
    Messages:
    4,736
    Likes Received:
    0
    Location:
    Southern California
    well, even with the delimiter it won't recognize when the user just presses the 'Enter' key.
     
  7. peerk

    peerk New Member

    Joined:
    Mar 14, 2005
    Messages:
    984
    Likes Received:
    0
    I think this is what you want.

    The newline is not stored when using cin.getline. Since nothing is entered cin.getline only puts the null terminator \0 into the character array at string[0] spot.

    If you put a cout test into the while loop and enter "Bob" as the name you'll see three output statements. This is because \0 is placed at the end of the character array.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int i=0;
    	char string[80];
    	cout << "Enter name (or press 'Enter' to Quit):";
    
    	cin.getline(string,80);
    
    	while (string[i]!='\0'){
    	i++;
    	}
    	if (i == 0) 
    	cout << "Enter pressed" << endl;
    
    	return 0;
    }
    
    
    And didn't you ask this before?

    http://forums.offtopic.com/showthread.php?t=2394478
     
    Last edited: May 12, 2006
  8. Kingfisher

    Kingfisher Keepin L's lit

    Joined:
    Mar 29, 2003
    Messages:
    4,736
    Likes Received:
    0
    Location:
    Southern California
    Thanks peerk. Also, I remember the last post all too well :noes:
    This time however, I am not working with a single character, but with an array of characters. Would it make any difference??


    .Net is not allowing me to do the following:

    char string[80];
    cout << "Enter name (or press 'Enter' to Quit):";
    cin.get(string)
    if (string == '\n')
    cout << "Enter pressed" << endl;
     
    Last edited: May 12, 2006
  9. peerk

    peerk New Member

    Joined:
    Mar 14, 2005
    Messages:
    984
    Likes Received:
    0
    Yeah, it is different. You could use cin.get and then cin.getline but that would be inconvienient.

    The code I posted up above should work. It tests if the \0 null terminator is placed at the beginning of the character array. This will happen when using cin.getline and only enter is hit.

    Also you could use

    cin >> noskipws;

    http://www.fredosaurus.com/notes-cpp/io/reading-chars.html



    edit:

    I just noticed the code you posted in your response.

    Code:
    .Net is not allowing me to do the following:
    
    char string[80];
    cout << "Enter name (or press 'Enter' to Quit):";
    cin.get(string)
    if (string == '\n')
    cout << "Enter pressed" << endl;
    
    This is not working for a few reasons. Here is what you would need to do for it to compile.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	char string[80];
    	
    	cout << "Enter name (or press 'Enter' to Quit):";
    	cin.get(string, 80);
    	
    	if (string[0] == '\n')
    		cout << "Enter pressed" << endl;
    
    	return 0;
    }
    
    
    The size is specified in the cin.get and string[0] is compared in the if statement. But this still will not work because when using cin.get with character arrays the deliminator(default of \n) is not exctracted.

    See http://www.cplusplus.com/ref/iostream/istream/get.html
     
    Last edited: May 12, 2006

Share This Page