C++ program

Re: C++ program

Post by hascon on Fri Mar 27, 2009 9:48 am
([msg=20689]see Re: C++ program[/msg])

okay i was wrong, it really works with just pressing enter or return...
sorry for the senseless discussion :lol:

bird7727 wrote:both cin.get() and system("pause") will react to ANY key being pressed, "enter" (or return) key or otherwise..

but it does not react on any key, only on enter/return, otherwise you couldnt type in more than one char
or am i doing something wrong? :?
hascon
New User
New User
 
Posts: 8
Joined: Tue Mar 24, 2009 5:01 pm
Blog: View Blog (0)


Re: C++ program

Post by bird7727 on Sat Mar 28, 2009 12:19 pm
([msg=20794]see Re: C++ program[/msg])

cin.get() and cin.getline() are different functions with different variables. With cin.get() you can just leave it like that and the program will break after any input. cin.getline() requires you to set the null character ("\0") as the end of the string, set the input length, and...something else I can't remember. But to sum it up cin.get() is the exact same as system("PAUSE") without the overhead.
bird7727
New User
New User
 
Posts: 48
Joined: Sun Feb 01, 2009 12:43 am
Blog: View Blog (0)


Re: C++ program

Post by hascon on Sun Mar 29, 2009 9:56 pm
([msg=20874]see Re: C++ program[/msg])

both cin.get() and system("pause") will react to ANY key being pressed, "enter" (or return) key or otherwise..

But to sum it up cin.get() is the exact same as system("PAUSE") without the overhead.

the matter is, if you use system("pause"), you only have to "press any key to continue".
however, with cin.get() you have to press enter or return, otherwise the program wont get any input, no matter if other keys are pressed or not.
my question was if there is any other possibility of "registrating" every key press directly, without the need of enter/return and without the system command.

i just wanted to make my question a bit clearer, i dont really need such a command now.. pressing enter instead of another key is no big problem 8-)
hascon
New User
New User
 
Posts: 8
Joined: Tue Mar 24, 2009 5:01 pm
Blog: View Blog (0)


Re: C++ program

Post by bird7727 on Tue Mar 31, 2009 2:13 am
([msg=20932]see Re: C++ program[/msg])

The thing is, you CAN press another key other than enter/return! Do you mean so the program executes different actions when you press different keys? Because it was said many times before you don't need to press enter/return.
bird7727
New User
New User
 
Posts: 48
Joined: Sun Feb 01, 2009 12:43 am
Blog: View Blog (0)


Re: C++ program

Post by V3HK15 on Tue Mar 31, 2009 2:34 am
([msg=20934]see Re: C++ program[/msg])

This should work:
Code: Select all
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <iostream>


bool Ask ()
{
   double a = rand()%12 + 1;
   double b = rand()%12 + 1;
   double c;

   std::cout<<"\n"<<a<<" x "<<b<<" = ";
   std::cin>>c;

   return (c==(a*b));   // gives back wether the answer was right or wrong
}

int main ()
{
      short int e=0;
      short int i=0;

      srand ( time(NULL) );

      std::cout << "ok well maths isn't fun but DO IT! \n copyright© Benjamin Mikhaiel(bigfish) \n";
      std::cin.get();

   for (; i<5; ++i)
   {
            if (Ask())
      {
                   std::cout<<"\ncorrect.\n";
                   ++e;
                }
            else
      {
                   std::cout<<"\nfail.\n";
                }
            std::cin.get();
      }
      std::cout<<"\n\nCongrats or not u got "<<e<<" out of "<<i<<" correct\n";

      std::cin.get();

   return 0;
}
V3HK15
New User
New User
 
Posts: 2
Joined: Sat Mar 21, 2009 8:46 am
Blog: View Blog (0)


Re: C++ program

Post by hascon on Tue Mar 31, 2009 4:32 pm
([msg=20946]see Re: C++ program[/msg])

bird7727 wrote:The thing is, you CAN press another key other than enter/return! Do you mean so the program executes different actions when you press different keys? Because it was said many times before you don't need to press enter/return.

But, if I compile
Code: Select all
...
int main() {
  cin.get();
  return 0;
}

I get a empty console. i can press any key, type in a whole text, and delete it again. nothing happens, the program doesnt care about it. i have to press enter or return if i want to go on.

if i compile system("pause") and, for example press 'a', the program will go on. i cannot type in anything, it doesn't matter which key i press first.

dont get me wrong, we could immediately stop the discussion here, i actually dont care about this little matter, but now that you all say the same i get a bit curious about that thing.

maybe i got something wrong, english is not my "mother tongue" (i think im not the only one though), but you all say system("pause") and cin.get() are practically exactly the same, and i dont understand why you say that :|

edit.. i just want to make clear that i would prefer cin.get() ,too.
hascon
New User
New User
 
Posts: 8
Joined: Tue Mar 24, 2009 5:01 pm
Blog: View Blog (0)


Re: C++ program

Post by bird7727 on Tue Mar 31, 2009 9:19 pm
([msg=20968]see Re: C++ program[/msg])

hmm.... so you can type anything but it won't continue the program unless you press enter/return? Are you putting "using namespace std;" in the beginning?

EDIT:

For example, if you compiled
Code: Select all
#include <iostream>

using namespace std;

int main()
{
cout<<"Hello\n";

cin.get();
return 0;
}


It should be the same as
Code: Select all
#include <iostream>

using namespace std;

int main()
{
cout<<"Hello\n";

system("PAUSE>null");
return 0;
}


EDIT: Actually, now that I try it again you are absolutely right..
bird7727
New User
New User
 
Posts: 48
Joined: Sun Feb 01, 2009 12:43 am
Blog: View Blog (0)


Re: C++ program

Post by YayPython on Mon Apr 13, 2009 8:43 pm
([msg=21738]see Re: C++ program[/msg])

Yes, because cin.get() just gets input until you hit the enter/return key. You could make a program that will decide what key does what, I guess.

Code: Select all
...
int main(){
  cout<<"Press a key: ";
  string key;
  cin << key;
  if (key == " //Blah blah blah...
}


I didn't compile a program like this, but what if you didn't put in a cin.get() and just the cin? Then it wouldn't ask for the enter, I think.
User avatar
YayPython
Experienced User
Experienced User
 
Posts: 50
Joined: Sat Jan 31, 2009 11:05 am
Blog: View Blog (0)


Previous

Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests

cron