Thread: problems with array

  1. #1
    Unregistered
    Guest

    problems with array

    ok i'm storing the first name in one array and the last name in a second array.
    i then add the two arrays (strcat) to get the full name.
    It works fine.
    But then, i have a function that recalls the main function.
    But when i call it back, it doesn't give me the occasion to input the first name and goes directly to the input of the last name.
    Is it because i did not clear my first array in which I stored both arrays?

    here's some of the code:

    void yourname (char n4[150], char n5[150]) {
    cout << "What is your first name?" << endl;
    cin.getline (n4,150,'\n');
    cout << "And what is your last name?" << endl;
    cin.getline (n5,150,'\n');

    strcat (n4, " ");
    strcat (n4, n5);
    cerr << "Hi " << n4 << "!" << endl;
    cout << endl;
    cout << endl;
    _sleep(1000);
    }

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    not enough code to see problem but you could try adding this line before the first getline()

    cin.ignore(80,'\n');

    that may or may not fix the problem.You have crap in the input buffer that needs clearing. probably just a newline char. do u do any cin>> before calling this func? also watch those arrays for overflow!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Unregistered
    Guest
    This solved the problem for when I call back the function but now the first time i run the program it ignores the first input i enter and asks for another.

    Can you tell me what's the command to clear an array, not delete it?

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >This solved the problem for when I call back the function but now the first time i run the program it ignores the first input i enter and asks for another.

    Then you'll have to move the cin.ignore() to the source of the problem - the input that's leaving a '\n' in the buffer.

    >Can you tell me what's the command to clear an array, not delete it?

    You could use memset (in cstring) or loop through manually, setting each element to zero.

  5. #5
    Unregistered
    Guest

    Problem solved but i don't understand

    Ok i moved the cin.ignore to where i thought the problem originated from and it was in my doAgain function (function that asks if the user wants to try the program again and calls the main function again)
    I place the ignore right before it called the main function again. But I don't understand how the '/n' remains in the buffer.
    I figure that it is the 'enter' that the user presses after the cin asking for yes or no but i don't understand why it stays.

    Plz keep in mind that I am a n00b in C++ programming. I've been at it for 3-4 days

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >I figure that it is the 'enter' that the user presses after the cin asking for yes or no but i don't understand why it stays.

    I'm assuming you're using operator >> in doAgain(). This operator leaves the '\n' in the buffer. This wouldn't be a problem if you were using operator >> again for the next input as it will ignore it, but as cin.getline() doesn't you need to manually remove it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  3. Problems passing an array to a function
    By ndenklau in forum C Programming
    Replies: 5
    Last Post: 09-20-2006, 08:14 AM
  4. Helllppp!!! I/O and array problems
    By dorky in forum C++ Programming
    Replies: 3
    Last Post: 07-02-2005, 09:24 AM
  5. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM