Skip to search.

Breaking News Visit Yahoo! News for the latest.

×Close this window

  1. Home >
  2. All Categories >
  3. Computers & Internet >
  4. Programming & Design >
  5. Resolved Question
aaron s aaron s
Member since:
April 18, 2008
Total points:
117 (Level 1)

Resolved Question

Show me another »

C++ help? how to make this wait until key press.?

im writing a basic C++ code and it has no errors, but at the end i wrote std::endl; to make it pause for a key stroke, but it wont, is there an answer to this? Im using visual studio 2005, and writing in its C++, so i think its the same as visual C++ 2005.

here is the code:

// tutorial.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}

// this is a comment
// this program will accept numbers and display their sum

#include <iostream>

void main()
{
//define variables
float num1;
float num2;
float total;

// Enter data for variables

std::cout << "Enter a value for the first variable: ";
std::cin >> num1;
std::cout << "Enter a value for the second variable: ";
std::cin >> num2;

// Add the two numbers together
total = num1 + num2;

// Display the result
std::cout << "The sum of the numbers = " << total << "\n";
std::cin.get(); //wait for keypress
}

it should in theory wait till i press a key, but i entered 5, thenhit enter, entered 5 again, and hit enter and i coul;d barely see a flash of 10 before it closed.
Chip Shot by Chip Shot
Member since:
December 31, 2008
Total points:
430 (Level 2)

Best Answer - Chosen by Voters

Where you define variables write
char key;

Then where you have std::cin.get(); change this to:

std::cin >> key;

I believe std::cin.get() is just trying to get the last of the display and not waiting for another keystroke. But by doing this it will wait and display what you have.
100% 1 Vote

There are currently no comments for this question.

Other Answers (0)

No other answers.

Answers International

Yahoo! does not evaluate or guarantee the accuracy of any Yahoo! Answers content. Click here for the Full Disclaimer.

Help us improve Yahoo! Answers. Send Feedback