Newbie at programming here. Help is much appreciated

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...";

}
else {

std::cout << "invalid" << std::endl;

}
return 0;

}
You have to read the assignNumber before the if statement:
1
2
3
4
5
6
7
8
std::cout << "Press 1 to show PC stat..." << std::endl;
if (std::cin >> assignNumber && 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;
}

1) This belongs to the beginner section
2) Use code tags to show your source code properly

Is this what you want to achieve?
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>;

int main() 
{
    std::cout << "Press 1 to show PC stat..." << std::endl;
    char c;
    std::cin >> c;
    if(c == '1')
        std::cout << "PC stats\n CPU : AMD\n RAM : 8gb\n GPU: GTX 2GB\n " << std::endl;
    else
        std::cout << "invalid" << std::endl;
}
Hello,

thank you guys,

Both method works exactly what I want.

highly appreciate it..
closed account (j3Rz8vqX)
My aim is to output the specs by pressing only '1' and nothing else.
There are several, limited, ways to invoke this. The below is one example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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
#include <conio.h>//Does not work for all compilers: enables getch();


Then why are you using it? There are better ways to achieve the same thing.
closed account (j3Rz8vqX)
pressing only '1' and nothing else

My interpretation is that the OP wants to operate when 1 is pressed, without the return/enter key.

@abhishekm71:
There are better ways to achieve the same thing
Demonstrate an alternative.

There are a few possibilities, and getch() is one of the candidates.
Topic archived. No new replies allowed.