1

How do I read in 1000 characters from the console using C++?

UPDATE from comment on answer: "What i want is that the user can input a paragraph ( say 500 or 300 characters)" - i.e. not always 1000 characters

With the following code, I am only able to input up to a limit( around two lines). What am I doing wrong?

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
void main()
{
    char cptr[1000];
    cout<<"Enter :" ;
    gets(cptr);
    getch();
}
2
  • <iostream.h> is for ancient versions of C++ only. You should not use it. Include <iostream> instead. Commented Nov 13, 2012 at 5:50
  • actually,i am using a pretty ancient version of c++. i have borland c++ 3.0 the problem is all my school computers have this version & they dont update it so i have to code in this way. i have a school project to do for my board exams & it has to run in my school computer so.... Commented Nov 13, 2012 at 5:54

4 Answers 4

3

Hope this helps:

#include<iostream>

using namespace std;

int main()
{
    const int size = 1000;
    char str[size];

    cout << "Enter: " ;

    cin.read(str, size);

    cout << str << endl;
}
Sign up to request clarification or add additional context in comments.

2 Comments

@user1819921 using namespace std; is pretty much standard in c++.
Alternative would be to prefix cout, cin and endl with std:: (i.e. std::cout, std::cin, std::endl).
2

Use getchar to read one character at a time in for loop as below:

            int i;
            for (i = 0; i < 1000; i++){
              cptr[i] = getchar(); 
            }

EDIT: If you want to break the loop early e.g. on new line char then:

            int i;
            for (i = 0; i < 1000; i++){
                char c  = getChar();
                if(c == '\n'){
                  break;//break the loop if new line char is entered
                }
                cptr[i] = c; 
            }

14 Comments

,but if i use getchar ,the user will have to input 1000 characters. What i want is that the user can input a paragraph ( say 500 or 300 characters)
@user1819921: That fine. You can add a type check on the character and break the loop if type matched. Updated the answer with new line check.
hey,,in d code u gave,i used cin instead of getchar cause i was having some error with getchar. but even then the max i am able to input is 2 lines..dont know why i am having this problem.
@user1819921: Lets do some troubleshooting. Add statements within the loop to cout the value of i once it goes inside if. This will tell us, if it entered in the if block? Also, after the for loop, again add the cout to print value of i to know, how many times it ran?
void main() { char c,cptr[1000]; int i; for (i = 0; i < 1000; i++){ cout<<i; cin>>c; if(c == '\n'){ break;//break the loop if new line char is entered } cptr[i] = c; } cout<<i; getch(); }
|
1

This is likely due to the fact that you're reading a new line. gets(char* ptr) stops reading when you encounter a new line, and appends terminating character to the string.

4 Comments

so, how do i solve this ? like say,user wants to input a paragraph of around 300-500 characters
@user1819921 Call gets in a loop until all the input is consumed. On each subsequent call set the pointer to the strings end plus one.
"set the pointer to the strings end plus one" did not get this part, can you please write the code for this ?
@user1819921 Actually, I would recommend just trying the answer using cin.read. Assuming you can use iostream it is probably much simpler.
0

Here is a solution to user can input a paragraph ( 1000, 500 or 300 characters).

code:

#include <iostream>
using namespace std;

int main()
{

  char ch;
  int count = 0;
  int maxCharacters=0;
  char words[1024]={' '};

  cout<<"Enter maxCharacters 300,500,1000 >";
  cin>>maxCharacters;

  cout << "\nProceed to write chars, # to quit: \n";

  cin.get(ch);
  while( (ch != '#')  )
  {
    cin.get(ch);    // read next char on line
    ++count;        // increment count

    words[count]=ch;
    cout <<words[count];     // print input

    if (count>= maxCharacters) break;

  }
  cout << "\n\n---------------------------------\n";
  cout << endl << count << " characters read\n";
  cout << "\n---------------------------------\n";
  for(int i=0;i<count;i++) cout <<words[i];
  cout << "\n"<< count << " characters \n";

  cout<<" \nPress any key to continue\n";
  cin.ignore();
  cin.get();

   return 0;
}

Output:

Enter maxCharacters 300,500,1000 >10

Proceed to write chars, # to quit:
The pearl is in the river
The pearl

---------------------------------

10 characters read

---------------------------------
 The pearl
10 characters

Press any key to continue

2 Comments

hey ,thnx fr d code.im having a problem, while inputting characters,as soon as i input around 2 lines,further input stops & execution gets hanged. eg : after cout << "\nProceed to write chars, # to quit: \n"; part when say i keep writing & complete around 1 nd 1/2 lines, further input does nt take place . Why am i facing this problem ?
in 1 go the max i am being able to input is 126 characters, after which further input does nt take place, but if i input multiple times with less that 126 characters,input proceeds normally.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.