simple code error

hello guys..
i'm so new and dumb, and i can't figure out why this is wrong. i did it exactly the way i saw it in the video and it not working. all i get is it pops up, asks me to type a number, but after typing number it goes away rlly quick. but i need it to sqrt that number first and stay there. i thought cin.get() make it stay but it wont let me


#include "stdafx.h"
#include <iostream>
#include <cmath>

int main()
{
	using namespace std;

double num1;
cout << "Pick your number bro..." << endl;
cin >> num1; 
double num2;
num2 = sqrt (num1);
cout << "The square root of " << num1 << " is " << num2 << endl;

	cin.get();
	return 0; 

}
Last edited on
I don't remember exactly how this works, but I thought it was just get(). If that doesn't work, and don't count on it working, then try #include <cstdlib> and replace cin.get(); with system("pause");. This is considered by some as bad practice, but you will probably be able to find better ways as you proceed.

EDIT: I believe that the function you are looking for is getch(). I haven't tested it because I use other methods, but that probably works.
Last edited on
oh so i just replace cin.get(); by getch();?
i never seen that before..what does it do?
I can't remember. Try it and find out.
add another cin.get() to your code.

what compiler are you using btw? If you are using visual studio you can set a flag (in linker settings?) to let the compiler know that you would like the console to remain open after the return 0; statement.
Last edited on
I think the problem is the cin.get() is getting your '\n' character when you hit enter. Maybe use another cin.get() (or, more ideally in this case, cin.ignore())?
Last edited on
1
2
cin.clear();
cin.ignore(80, '\n');


That should do it.

And "cin.clear" is to clear the stream, so all eventual \n's will be cleared (and any character that might be in the stream).
Topic archived. No new replies allowed.