I just want to output a simple practice program.
My goal is to output a pc specs when a user input '1'.
Although this can run and output the specs, any character entered can output the specs. My aim is to output the specs by pressing only '1' and nothing else.
if I enter other character it will say something like "invalid".
Code:
// Output PC specs //
#include <iostream>;
int main() {
int assignNumber{ 1 };
std::cout << "Press 1 to show PC stat..." << std::endl;
if (assignNumber == 1) {
std::cin >> assignNumber;
std::cout << "PC stats\n CPU : AMD\n RAM : 8gb\n GPU: GTX 2GB\n " << std::endl;
std::cout << "Press enter to exit...";
#include <iostream>
#include <conio.h>//Does not work for all compilers: enables getch();
int main()
{
int assignNumber;
std::cout << "Press 1 to show PC stat...";
assignNumber = getch();
assignNumber = assignNumber-'0';//Convert characters to number:
//Subtracting '0' and starting 0 base at '0'.
//'1' to value 1; normally '1' == 49.
if (assignNumber == 1)
{
std::cout << "PC stats\n CPU : AMD\n RAM : 8gb\n GPU: GTX 2GB\n " << std::endl;
std::cout << "Press enter to exit...";
}
else
{
std::cout << "invalid" << std::endl;
}
return 0;
}
I believe that was your intentions.
Conio.h is not a c++ library, it is a MS-DOS library.
http://en.wikipedia.org/wiki/Conio.h