-
cin.get() aint working.
I have no idea what im doing wrong, cin.get() used to stop my programs but for somereason they nolonger do. Here is an example of when the cin.get() hasnt been stopping my code :
#include <iostream>
using namespace std;
int main() {
int x;
int y;
int box[2][2];
cout<<"Please enter a number between 1 and 2\n";
cin>>x;
cout<<"Please enter another number between 1 and 2\n";
cin>>y;
box[0][0] = x*y;
box[0][1] = x/y;
box[1][1] = x+y;
cout<<box[0][0]<<" "<<box[0][1]<<" "<<box[1][1];
cin.get();
}
i know its simple but after it runs it just quits by its self.
-
try adding return 0; after cin.get( ) and see if that works.
-
-
i added return 0; afterwards and it still closes right away. Its wierd that it used to work....
im using microsoft visual studio .net as my compiler if it matters.
-
try flushing your buffer with an << endl; before using cin.get( )
-
still doesnt work...
so now the code looks like this:
#include <iostream>
using namespace std;
int main() {
int x;
int y;
int box[2][2];
cout<<"Please enter a number between 1 and 2\n";
cin>>x;
cout<<"Please enter another number between 1 and 2\n";
cin>>y;
box[0][0] = x*y;
box[0][1] = x/y;
box[1][1] = x+y;
cout<<box[0][0]<<" "<<box[0][1]<<" "<<box[1][1]<<endl;
cin.get();
return 0;
}
and what is wierd is that the cin.get() in this code works fine :
#include <iostream>
using namespace std;
int main() {
cout<<"hello";
cin.get();
}
-
-
-
box[0][1] = x/y; might cause a problem when y = 0.
-
doesnt work:
Code:
#include <iostream>
using namespace std;
int main() {
int x;
int y;
int box[2][2];
cout<<"Please enter a number between 1 and 2\n";
cin>>x;
cout<<"Please enter another number between 1 and 2\n";
cin>>y;
box[0][0] = x*y;
box[0][1] = x/y;
box[1][1] = x+y;
cout<<box[0][0]<<" "<<box[0][1]<<" "<<box[1][1]<<endl;
cin.get();
return 0;
}
does work:
Code:
#include <iostream>
using namespace std;
int main() {
cout<<"hello";
cin.get();
}
-
it doesnt matter if i set y to 0 or not , the same thing keeps happening.....
-
Ok ill be happy with this for now :) , but next time try to indent code, its much easier to read.
Now on to your problem; the problem is that cin leaves the trailing newline character in the buffer. This means that the program basicly skips the cin.get call because cin.get() gets the newline already in the buffer from previous calls to cin >> [whatever variable you have here]. to fix this add this code after every cin >> [whatever]; : cin.ignore();
-
Ah, thank you that did it. I had forgoten all about the cin.ignore() function :D
now back to the tutorials.
-
The problem here is cin.ignore() with no arguments simply ignores the next character in the input stream. While usually the only item left is the newline from the previous input, sometimes there can be more than one character in the input stream. Usually when cin is used to input a variable, but the user input was delimited with whitespaces and extra chars.
Using while ( cin.get() != '\n' ); will flush all characters up to the newline out of the input buffer.
-
im not sure the header file but where your cin.get() is try a getch().
it will pause the screen until a user hits a button.