Thread: Problem with arrays

  1. #1
    Registered User
    Join Date
    Jul 2008
    Location
    Vodice, Croatia
    Posts
    13

    Problem with arrays

    Using the code below, I tried to make the program get what user types in, then prints the text he typed in, gets what user typed in, prints the text he typed, gets what user typed in, prints the text...

    Code:
    #include <stdafx.h>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    char input[99999];
    int n=0;
    do {
    	cin>>input[n];
    	cout<<input[n]<<endl;
    	n++;
    	} while (n<=99999);
    	
    cin.get();
    }
    It works but the strange part is that it goes to a new line after each letter.
    The result looks like this if the user types in "asdf" twice:
    Code:
    asdf
    a
    s
    d
    f
    asdf
    a
    s
    d
    f
    If I remove "endl" part, it wont go to a new line after each letter, but then this will happen after user types "asdf" twice:
    Code:
    asdf
    asdfasdf
    asdf
    And I want the result to look like this:
    Code:
    asdf
    asdf
    asdf
    asdf
    What've I done wrong?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Print the endl ONCE, not inside the loop.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Location
    Vodice, Croatia
    Posts
    13
    I dont think I understand, it should be outside of the loop? But then the cursor wont go to a new line every time program prints what the user has typed?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Okay, I didn't read your code carefully enough and didn't realize your input loop was as broken as it is. Your read loop doesn't stop on new lines as such, only when 99999 characters are read -- the only reason you're seeing something like what you want is that the input is buffered. If you want to read a line at a time, use getline(); if you're trying to read one character at a time without them having to hit enter, then you'll need to get into system-specific things.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Location
    Vodice, Croatia
    Posts
    13
    Quote Originally Posted by tabstop View Post
    Okay, I didn't read your code carefully enough and didn't realize your input loop was as broken as it is. Your read loop doesn't stop on new lines as such, only when 99999 characters are read -- the only reason you're seeing something like what you want is that the input is buffered. If you want to read a line at a time, use getline(); if you're trying to read one character at a time without them having to hit enter, then you'll need to get into system-specific things.
    I'm trying to read one line user types in, whether its one character, or a whole sentence and then print out that character/sentence and make the cursor go to a new line, wait untill user types another character/sentence and repeat the whole process

    So you say I'd have to user getline();? Could you elaborate because I still dont understand, what else is necessary?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why would anything else be necessary? getline(input, 99999). (Read the documentation for getline in your C++ book. If you don't have a C++ book, read the documentation for getline online, such as cplusplus.com or cppreference.com.)

  7. #7
    Registered User
    Join Date
    Jul 2008
    Location
    Vodice, Croatia
    Posts
    13
    Quote Originally Posted by tabstop View Post
    Why would anything else be necessary? getline(input, 99999). (Read the documentation for getline in your C++ book. If you don't have a C++ book, read the documentation for getline online, such as cplusplus.com or cppreference.com.)
    Okay, I've replaced the line but this is the error I'm getting:
    error C3861: 'getline': identifier not found

    And what is the difference between getline and cin.getline? Is there any?

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I think cin.getline was what was being recommended.

    However, I'd use getline because it means you are using C++ strings which are much better than the character arrays you are using now. cin.getline only works on C style character arrays and getline (which is part of the std namespace) only works on the C++ string class. You should #include <string> for both the string class and getline.

  9. #9
    Registered User
    Join Date
    Jul 2008
    Location
    Vodice, Croatia
    Posts
    13
    I think I understand but my code still isn't working...
    Code:
    #include <stdafx.h>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    char input[99999];
    int n=0;
    do {
    	getline (input, 99999);
    	cout<<input[n];
    	n++;
    	} while (n<=99999);
    	
    cin.get();
    }
    This time it gives me 2 errors:
    Code:
    error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'char [99999]'
    1>        c:\program files\microsoft visual studio 9.0\vc\include\string(527) : see declaration of 'std::getline'
    and
    Code:
    error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : expects 3 arguments - 2 provided
    1>        c:\program files\microsoft visual studio 9.0\vc\include\string(475) : see declaration of 'std::getline'

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You picked the C++ string version of getline, but you're still using character arrays. Just put cin. in front of the getline to use the character array version and get it working.

    Then consider switching to C++ strings after your current task is accomplished.

  11. #11
    Registered User
    Join Date
    Jul 2008
    Location
    Vodice, Croatia
    Posts
    13
    Still doesn't work, I'm getting this as a result:
    Code:
    asdf
    a
    s
    d
    f
    But I have to press Enter every time for each character to appear, so its like this:
    I type "asdf", press Enter, "a" appears, I press Enter again, "s" appears, I press Enter 3rd time, "d" appears, I press it again, "f" appears, after that if I press Enter, some weird character appears in a new line...

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Don't output "input[n]", just output "input".

  13. #13
    Registered User
    Join Date
    Jul 2008
    Location
    Vodice, Croatia
    Posts
    13
    Quote Originally Posted by tabstop View Post
    Don't output "input[n]", just output "input".
    Oh it works now!!
    Many thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a problem regarding dynamic memory and arrays
    By Michty in forum C++ Programming
    Replies: 5
    Last Post: 07-26-2006, 01:26 PM
  2. assignment of arrays problem
    By HumbuckeR in forum C++ Programming
    Replies: 4
    Last Post: 04-13-2006, 04:25 PM
  3. Problem with arrays
    By dogbert234 in forum C++ Programming
    Replies: 2
    Last Post: 03-25-2006, 03:06 AM
  4. Problem with character arrays in classes
    By spoketoosoon in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2004, 03:57 AM
  5. Replies: 5
    Last Post: 12-03-2003, 05:47 PM