Actually i am a beginner in C++. I have been trying to write a code that can divide any number using very basic operations and producing the non-decimal quotient and remainder. But when i build and run the terminal (CodeBlocks) it takes a minute or two for the terminal to run. I have tried using arrays but no use.
Please tell me any improvement, but i donnt want to use any additional header files. Thanks in advance :)
#include <iostream>
using namespace std;
int main(){
double a, b, x, y;
int n=0;
cout<<"x\n-\ny\n\nEnter x: ";
cin>>x;
cout<<"\nEnter y: ";
cin>>y;
while (n>=0){
a=x-(y*n);
b=x-(y*(n+1));
if(b<0)break;
if(b<a) n++; }
cout<<"\nQuotient: "<<n<<"\n\nRemainder: "<<a;
}
It can't be the program itself. Check for Code Completion slowness.
Some good suggestions to add:
Turn off all Plug-ins you do not use.
Run Code::Blocks as Admin
Turn off code completion to see if the problem goes away.
Use a nightly build
It all runs fine for me.
The compiling time might be the thing that's bugging you.
Maybe don't rebuild the whole project when you just want to run the program.
I believe pressing F9 is "Build and run" and Ctrl + F10 is just "Run".
Also, n will never be less than 0 so rethink the conditional in you while loop.
Related
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.!!!!
Why does my C++ output screen disappear immediately? I'm a beginner in cpp. Can anyone help me to find the problem please?
You should either launch your application inside of a terminal, or add a line of code that waits for the input in order for the window to not close. E.g. add in the end of the function main a line:
std::cin.get();
And also add at beginning of the file the include that holds that function.
#include <iostream>
This is hard to answer since there can be many things that can cause your output box to close immediately. First try having a cout statement and then a cin statement. Something like:
cout<<"Hello"<<endl;
cin>>input>>endl;
Also make sure to have the necessary include statement at the top and whatever you want to return at the bottom.
#include<iostream>
return 0;
As you have said that you are beginner in C++, you should keep in mind ,three major things while coding in C++. You've mentioned that your screen disappears , then following things you should try.
1). in C++ conventionally main returns value of type int.And the format of your program should be like...
int main()
{
-------
//body of your program
-------
return 0;
}
If the function returns 0, that means it ran successfully.
2). You have to inlcude #include<iostream> on the top of your program.
3).check whether the IDE you are using is compatibale to your operating system or not.
Hope this will help you.
I am new in programming. but i know turbo C & i can work with it. But how to work on Dev C++?
i means 1.How to Compile & 2. How to Run. I have Windows so if anyone help me with proper example than it will be great.
Dev c++ is very simple! To do the basic, Open you file, compile and run using the menu Execute!
However, if u are starting something new now, I would suggest you consider using eclipse. It is not so easy in the first days but the long term gain is giant!
I would like to improve my answer... I suggested that you should consider using eclipse. I still think that eclipse is great. Furthermore, I had had some problems with the old version of dev c++. After reading the comment of #Orwell (which provided facts that I didn't know), I checked the new version and the issues that I had had before disappeared. So, the big picture of dev c++ is that it is a program that works well and it is very simple. There is a cost to start using eclipse!
Example:
#include <iostream>
using namespace std;
int fibonacci(int n){
if(n==1)return 0;
if(n==2)return 1;
if(n>=3)return fibonacci(n-1)+fibonacci(n-2);
}
int main(){
//
int n =10;
cout << "The element of the Fibonacci series is " << fibonacci(n) << "\n";
return 0;
}
I am a beginner in C++ and I was trying to write a program that finds the average of two numbers, but when I run the program, the window disappears without allowing me to see the result. Can someone please help me?
Thanks
#include <iostream>
using namespace std;
int main()
{
int number1,number2,answer;
cout << "number1? ";
cin >> number1;
cout << "number2? ";
cin >> number2;
answer = (number1 + number2)/2;
cout << answer<<endl;
return 0;
}
Solution #0 (proper):
Run program from shell (cmd.exe, bash)
Solution #1 (proper):
Run program from orthodox file manager. Far Manager or midnight commander.
Solution #2 (alternative);
Redirect output to file. program.exe >file.txt from command line.
Solution #3 (improper):
Launch message box, use "sleep"/"Sleep" to delay program termination.
Solution #4 (improper):
Request user input at the end of program.
Solution #5 (improper):
Set breakpoint on "return 0", debug the program.
Solution #6 (windows+msvc):
Launch program from msvc by Ctrl+F5 using debug build. You'll get "press key to continue" prompt.
Put a breakpoint at your return statement. It won't stop on an uncaught exception but that can be fixed with a try/catch block at the outermost part of main.
Before you return add system("PAUSE"); and this should fix your problem.
If you are using Visual Studio, hit CTRL+F5 to run. That inserts a "Hit RETURN to continue" for a console application.
Include this header:
#include <stdio.h>
And add a call to getchar() before you return:
cout << answer<<endl;
getchar(); // wait for user input
return 0;
Solution #6 from SigTerm (above) is not guaranteed to work. In Visual Studio you should right-click your project and choose:
Properties | Configuration Properties | Linker | System | SubSystem
and in the dropdown choose Console (/SUBSYSTEM:CONSOLE).
I always hated that...I always find the easiest to be system("pause"); right before you return, but that's not a portable solution (cin.get is though). There are many other ways as well, some of which are mentioned in the link below.
http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/1555ce45-8313-4669-a31e-b95b5d28c787
Edit: system("pause"); is terrible, should never be used, and may or may not end life on this planet as we know it, even if by a beginner in a project called 'Hello'. Using system("pause") is expensive and dangerous, and if you use it, you'll never see anyone you love again. Don't do it. Use cin.get or something else.
I'm trying my hand at c++ and am using fedora eclipse (3.4.2) as my IDE.
At the moment I'm trying to enter a string of numbers into the console, get the program to sort them and spit them back out. The program is straight out of a book and works with xcode and through a normal terminal - so I know it's correct.
Basically I run the program and enter a few numbers into the eclipse console, the numbers are coloured green so I know its accepting the input correctly.
When I hit enter, the console jumps to a new line and nothing happens. When I press control+shift+D, nothing happens. When I press control+d, nothing happens.
I use eclipse for python too, and the console works properly. Just hitting enter inputs data to the program.
Am I missing something here? I've spent the last half hour or so trying to figure this out. Can anyone help me? Thanks.
What version of ecplise and what complier are you using? The following worked for me on Eclipse Ganymede with GCC version 3.4.5:
#include <iostream>
using namespace std;
int main() {
int x = 0;
cout << "Type your input here:";
cin >> x ;
cout << "You entered " << x << endl;
return 0;
}
How does your program know that input has ended? It sounds like it accepts multiple lines of input in the console window. Isn't there some magic case that pops you out of that loop so you can process the input that's been collected?
As other said, without the code there's no answer.