You are viewing our Forum Archives. To view or take place in current topics click here.
C++ Terminating to quickly
Posted:

C++ Terminating to quicklyPosted:

ip
  • Summer 2018
Status: Offline
Joined: Dec 30, 201211Year Member
Posts: 3,778
Reputation Power: 3016
Status: Offline
Joined: Dec 30, 201211Year Member
Posts: 3,778
Reputation Power: 3016
So lets say I build a simple program.

#include <iostream>
using namespace std;
int main()
{
string name;
cout << "Please enter your name!" << endl;
cin >> name;
cout << "Thank you for inputting your name. We will help you shortly." << endl;
return 0;
}

It always blows right past the code simply showing it for milliseconds and then it closes the application. I need to know a simple code that will allow the user to press "Q" to quit the program. I have been looking and looking but I have not found it.
I have never made an actual C++ program before so bare with me.

Thanks guys!
#2. Posted:
7en
  • V5 Launch
Status: Offline
Joined: Aug 16, 201212Year Member
Posts: 598
Reputation Power: 29
Status: Offline
Joined: Aug 16, 201212Year Member
Posts: 598
Reputation Power: 29

char x = cin.getch();
if(x == 'q')
    return 0;


Sorry for previous answer.


Last edited by 7en ; edited 2 times in total
#3. Posted:
Botch
  • TTG Senior
Status: Offline
Joined: Aug 31, 201212Year Member
Posts: 1,553
Reputation Power: 65
Status: Offline
Joined: Aug 31, 201212Year Member
Posts: 1,553
Reputation Power: 65
You need to have some form of user input at the end of the program, or else it will do just that. That way, the user can press a key and it will finish running the program.
#4. Posted:
ip
  • V5 Launch
Status: Offline
Joined: Dec 30, 201211Year Member
Posts: 3,778
Reputation Power: 3016
Status: Offline
Joined: Dec 30, 201211Year Member
Posts: 3,778
Reputation Power: 3016
Abbreviate wrote

char x = cin.getch();
if(x == 'q')
    return 0;


Sorry for previous answer.
Thank you sir. +Rep for you!
#5. Posted:
ip
  • Summer 2018
Status: Offline
Joined: Dec 30, 201211Year Member
Posts: 3,778
Reputation Power: 3016
Status: Offline
Joined: Dec 30, 201211Year Member
Posts: 3,778
Reputation Power: 3016
Decisively wrote
Abbreviate wrote

char x = cin.getch();
if(x == 'q')
    return 0;


Sorry for previous answer.


That's what you need AWL! I programme.

It's difficult at first, but as soon as you

put years of practise into it, you won't need our help.

Good luck on your programme, have a brilliant day!
Thanks man! I have a lot of great ideas coming up for my geometry class. Maybe a little cheating program? This is going to be fun!
#6. Posted:
cwc123
  • Challenger
Status: Offline
Joined: Apr 10, 201014Year Member
Posts: 124
Reputation Power: 4
Status: Offline
Joined: Apr 10, 201014Year Member
Posts: 124
Reputation Power: 4

#include <iostream>
#include <string>
int main()
{
   string a;
   getInput(cin, a); // places input in a
   cout << a;
   return 0;
}
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.