I want to get a very large number from the user and put the each individual digits of that number in rows of an array, respectively.
That's why I wrote this code in c++.
But when I running code and copy that big number and paste in windows Cmd it only receives 4094 digits and does not allow to write more numbers.
How to fix this?
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int size=0;
int *a=NULL;
int *b=NULL;
int count=0;
char x='0';
a=new int[size];
x=cin.get(); //input by user
while(isdigit(x)!=0)
{
if(count>=size)
{
b=new int[size+1];
for(int i=0;i<size;i++)
{
b[i]=a[i];
}
delete []a;
a=b;
size++;
}
a[count++]=x-'0';
x=cin.get(); //input by user
}
cout<<size;
}
Experimentation has shown me that the Windows cmd.exe has a maximum command line length of approximately 4094 * 2. On my Windows 10 64bit machine I am able to enter a maximum of 8189 characters before it stops allowing me to enter more. This means when I enter a sequence of digits separated by spaces, the most I can possibly enter in a single prompt is 4095 individual digits.
Here's the official Microsoft documentation on the subject:
Command prompt (Cmd. exe) command-line string limitation
Which states:
On computers running Microsoft Windows XP or later, the maximum length of the string that you can use at the command prompt is 8191 characters. On computers running Microsoft Windows 2000 or Windows NT 4.0, the maximum length of the string that you can use at the command prompt is 2047 characters.
This limitation applies to the command line, individual environment variables (such as the PATH variable) that are inherited by other processes, and all environment variable expansions. If you use Command Prompt to run batch files, this limitation also applies to batch file processing.
Microsoft even offers some guidance on how to work around this.
Modify programs that require long command lines so that they use a file that contains the parameter information, and then include the name of the file in the command line.
In your case you're using cin, but the same limitation seems to hold.
What this indicates is your problem lies in the method of entry to the particular prompt. There's a limit to how much can be entered at once.
Related question: Maximum Length of Command Line String
Related
int a[10000];
for(int i=0;i<10000;i++)
{
a[i]=i; cout<<a[i]<<endl;
}
Suppose this is the code and on the terminal screen I need all outputs (0-9999) But it only displays (9704-9999) at the end
I want to see all the numbers on the terminal window but it removes the upper part of the data. I guess I have to change some settings.
Increase the console buffering. Depending on which terminal you're using it'll be different. For example on Windows conhost.exe is the default console used by cmd and PowerShell. Just click the icon on the top left > Properties > Layout and set Screen Buffer Size to a large enough number
But the better solution would be redirecting to file, because no one wants to read 10000 lines on the console, and there's no guarantee that the console will have a buffer of infinite length or length of more than 10000 lines. conhost for example only supports maximum 9999 lines so you'll miss at least the command you typed and the first output line. Besides that'll often remove other commands' output from the history which is undesirable
Either do that from the command line with the redirection operator >
yourapp.exe >output.txt
or save to file directly from your code
I have 2 questions:
Question 1= i search in the internet and i found that everyone told don't use getch() or getche() in c++. so how can i get a character from user ?? for example i wrote this code so how can i replace getch with another statement ?
Question 2= i wrote this code in visual studio 2013 and it works fine but when i wrote in code block and compile whit GNU GCC compiler getchar() statement doesn't work . why ??
#include <iostream>
#include "conio.h"
using namespace std;
int main()
{
int word_counter=0,char_counter=0;
char ch;
cout<<"Enter your paragraph and press ENTER for end :\n";
cin>>ch;
while((ch=getchar())!=13)
char_counter++;
cout<<"Number of characters ="<<char_counter<<endl;
return 0;
}
First Question:
Use cin to get input from the user through the console.
Second Question:
getchar() is not supported on GNU gcc compiler as conio is not a part of gcc.`
EDIT:
Use getline() instead of cin as I earlier mentioned since you want the loop to break only when the input is '\n'.
Reponse 1 : getch() is used to ask(read) only one character form the user(or file).
Reponse 2: I suppose that you want to count the number of char.
int main()
{
char c;
long long n;
do
{
cin.get(c);
cout<<c;
n++;
}while(c!='.');
cout<<n<<endl;
return 0;
}
Question 1= i search in the internet and i found that everyone told don't use getch() or getche() in c++. so how can i get a character from user ??
Most environments buffer the text before it gets to your program. You would have to type the text, then press Enter in order to transfer the buffer to your program.
There are no functions nor facilities in standard C++ to get a character from the User as they type it.
You will need to use an OS specific function to fetch a character as the User types it. You may want to also search the internet for the word "keypress".
Question 2= i wrote this code in visual studio 2013 and it works fine but when i wrote in code block and compile whit GNU GCC compiler getchar() statement doesn't work . why ??
As others have stated, the conio.h file is not standard.
According to this reference on getchar, you will need to include stdio for the function.
Also note that the C language streams, such as stdin and printf, may not be compatible with the C++ language streams such as cout and cin. They may have different buffers. Pick one or the other and don't cross the streams.
so i am new to C++ and I was hoping the community could help me with my homework. Now im not asking for someone to do it for me because i am very capable of doing it on my own, i am just asking for help on a particular part. So, my assignment involved making a program that could be able to find and print all of the prime numbers between 2 and 100. I have to use a double loop (its what my professor said), so i have set up an if statement to run through all of the numbers from 2 to 100 and have a second loop inside the first one to determine if the current number is a prime number and then print it. Here is where my issue comes into play, when i run it, it opens the console window and closes it so fast i cannot see if anything did print to it. So i added a break point to see if it did. When i press F5 to step into each next step, it runs through the loop once and then it starts jumping over to different windows reading the lines of different source files (i think they are source files). Eventually the console window closes with nothing printed to it, and it doesn't start the loop over again like it should. My question is this, like in Visual Basic where you can put console.readline() so a button has to be pressed from the keyboard in order to continue, how can you do the same in C++ so that after the loop to see if the number is prime ran and printed the number, the program will wait for a key to be pressed right after it was printed?
Here is my current code as follows, Thanks again for any help, I really appreciate it.
#include "stdafx.h"
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int counter=2;
int primeCounter=counter;
//two variables, one to keep track of the number we are one, the other to check to see if its prime.
if(counter<101)
{//I want the counter to increment by 1 everytime the loops runs until it gets to 100.
if(!(counter%(primeCounter-1)==0))//if the counter has a remainer, then it is prime
{//each time the main loop run i want this loop to run too so it can check if it is a prime number and then print it.
cout<<counter<<endl;
//if this was VB, here is where i would want to have my console.Readline()
}
else
{
}
counter+=1;
}
else
{
}
Since you're using Visual Studio you can just use Ctrl+F5 to run your program without debugging. That way the console window stays after the program's finished.
Alternatively you can run the program from the command line.
Or you can put a breakpoint on the last } of main and run it in a debugger.
It's not a good idea to add a “stop here” at the end.
If you want see each line of output as it's produced, just place a breakpoint after the output statement and run the program in the debugger, in Visual Studio keypress F5.
In passing, <stdafx.h> is not a standard header. It's in support of Visual C++ precompiled headers, which is a feature that yields quite non-standard preprocessor behavior. Better turn that off in the project settings, and remove the >stdafx.h> include.
Also
int _tmain(int argc, _TCHAR* argv[])
is a silly non-standard Microsoft-ism, at one time in support of Windows 9x, but no longer with any purpose other than vendor lock-in.
Just write standard
int main()
or with trailing return type syntax,
auto main() -> int
Finally, instead of
!(counter%(primeCounter-1)==0)
just write
counter%(primeCounter-1) != 0
cin.get() will do what you want.
In Visual Studio you can actually call system("pause"); in a place you need to suspend your app.
so i figured out my issue. First off the loop wasnt running because the first if statement didnt loop. I changes this to a while and now the output works like a charm. take a look
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
int counter=2;
int primeCounter=counter;
//two variables, one to keep track of the number we are one, the other to check to see if its prime.
while(counter<101)
{//I want the counter to increment by 1 everytime the loops runs until it gets to 100.
if((counter%(primeCounter-1)==0))//if the counter has a remainer, then it is prime
{//each time the main loop run i want this loop to run too so it can check if it is a prime number and then print it.
}
else
{
cout<<counter<<endl;
}
counter+=1;
primeCounter=counter;
}
}
now i just need to polish the condition to actually figure out the prime numbers. thanks again for your help.!!!!
This is my first question on stackoverflow. Pardon me if I haven't searched properly but I do not seem to find an explanation for this. Was just attempting an example from Bjourne Stroustroup's papers. Added my bits to see the array get re-sized as I type the text.
But it doesn't seem to work that way! getchar() simply waits till I am done with entering all the characters and then it will execute the loop. As per the logic, it doesn't actually go into the loop, get a character, perform its actions and then iterate. I am wondering if this is implementation specific, or intended to be like this?
I am on Ubuntu 14.04 LTS using Codeblocks with gcc 4.8.2. The source was in cpp files if that matters.
while(true)
{
int c = getchar();
if(c=='\n' || c==EOF)
{
text[i] = 0;
break;
}
text[i] = c;
if(i == maxsize-1)
{
maxsize = maxsize+maxsize;
text = (char*)realloc(text,maxsize);
if(text == 0) exit(1);
cout << "\n Increasing array size to " << maxsize << endl;
}
i++;
}
The output is as follows:
Array Size is now: 10
Please enter some text: this is some sample text. I would have liked to see the memory being realloced right here, but apparently that's not how it works!
Increasing array size to 20
Increasing array size to 40
Increasing array size to 80
Increasing array size to 160
You have entered: this is some sample text. I would have liked to see the memory being realloced right here, but apparently that's not how it works!
Array Size is now: 160
This has nothing to do with getchar directly. The "problem" is the underlying terminal, which will buffer your Input. The Input is sent to the program after you press enter. In Linux (dunno if there is a way in Windows) you can workaround this by calling
/bin/stty raw
in terminal or by calling
system ("/bin/stty raw");
in your program. This will cause getchar to immediately return the input character to you.
Dont forget to reset the tty behaviour by calling
/bin/stty cooked
when done!
Here is an example (for Linux):
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main() {
system ("/bin/stty raw");
char c = getchar();
system ("/bin/stty cooked");
return 0;
}
Also have a look at this SO Post: How to avoid press enter with any getchar()
Also, as suggested in the comments, have a look here: http://linux.die.net/man/3/termios especially on the command tcsetattr, which should work cross-platform.
Actually, tcsetattr does not apply to Windows (which is what is commonly referred to in this site as "cross-platform"). However, the question is tagged for Linux, so "cross-platform" is a moot point.
By default the standard input, output and error streams are set to
line-buffered (input)
block-buffered (output)
line-buffered (error)
You can change that using setbuf, but of course will not solve the problem (the answer calls for single-character input). POSIX terminal I/O (termios) lets you change via a system call any of the flags shown using stty. As a rule, you might call stty directly from a script, rarely from a C program.
Reading a single character is a frequently asked question, e.g.,
How can I read single characters from the terminal? (unix-faq)
How can I read a single character from the keyboard without waiting for the RETURN key? How can I stop characters from being echoed on the screen as they're typed? (comp.lang.c FAQ)
You could also use ncurses: the filter function is useful for programs that process a command-line (rather than a full-screen application). There is a sample program in ncurses-examples (filter.c) which does this.
In C++, when writing to and taking information from the console using cout / cin, is it possible to do something like
ENTER YOUR DATA HERE --> __ <-- ENTER YOUR DATA HERE
With the user input cursor where the underscores are located, and output located on either side of the cursor, and then the user's typed input appearing between those two bits of output before being returned to cin?
If so how would I got about doing that?
I hope that between my title and explanation here that it's clear what I'm asking, if not I can try to explain further.
Ideally I'd like to do this using iostream / cin & cout, because those are what I've used in the past. If the solution is to use... printf or similar I'll do that but may need a bit of additional explanation since I'm not really experienced in using that for output.
NOTE: I tried to find an answer for this problem and I can't say for sure that there wasn't one, it was mostly just a matter of finding a huge amount of other input/output-related questions.
EDIT: This is using the DOS shell on Windows 7, compiling from the Windows Visual Studio 2012 command-line compiler.
If Windows, use the Console API. http://msdn.microsoft.com/en-us/library/windows/desktop/ms682073(v=vs.85).aspx
If Linux, use the curses library: http://en.wikipedia.org/wiki/Ncurses
If you are using windows you have the option to use the conio.h.
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
gotoxy(5,3);
cout<<"HELLO WORLD";
gotoxy(60,10);
cout<<"HELLO WORLD";
gotoxy(35,20);
cout<<"HELLO WORLD";
getch();
return 0;
}
If you wish to use ANSI libraries only, you can try to take a look at this discussion:
http://www.cplusplus.com/forum/lounge/78225/
In both cases all you have to do is print the first text, then use gotoxy, then you print seconde text, gotoxy again where you want the input to happens, and read the user input.