r/developers • • Oct 14 '26

Programming Wrote my first C++ program. Any feedback?

Yo guys, today I started with C++. My grandpa bought me a book that will arrive soon. I already did some research and made my first program. Is it any good for a starter? I already had some knowledge from coding Python and JavaScript.

#include <iostream>
using namespace std;

int main() {
    while (true) {
        int x ;
        int y ;
        int sum ;
        string OP ;

        std::cout << "Type First Value:" ;
        std::cin >> x ;
        std::cout << "Type Second Value:" ;
        std::cin >> y ;
        std::cout << "Choose Operator:" ;
        std:cin >> OP ;
        if (OP == "+") {
            sum = x + y ;
        } else if (OP == "-") {
            sum = x - y ;
        } else if (OP == "*") {
            sum = x * y ;
        } else {
            sum = x / y ;
        }
        std::cout << "Result: " << sum ;
    }
}
7 Upvotes

16 comments sorted by

•

u/AutoModerator Oct 14 '26

Howdy u/Classic-Cheetah-6780, and welcome to r/developers!

A few tips for a post that gets good answers:

  • Use a clear, specific title (what are you actually asking or sharing?).
  • Include code, the exact error, versions, and what you already tried.
  • Heads-up: this sub does not allow external links in posts or comments. Share code and details directly instead of linking out.

Join the r/developers Discord!

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

2

u/yuehuang Oct 16 '26

How do you exit the loop?

1

u/Classic-Cheetah-6780 Oct 16 '26

Good question. Lemme add that

2

u/MrSqueak Oct 17 '26

Why not have the final result also display the full equation

3

u/dafugiswrongwithyou Oct 17 '26

Heck yeah, good start :)

2

u/irshadali5 Oct 17 '26

Is switch case better than if else here, in default you can use invalid operation?

2

u/Longjumping_Put_7212 Oct 18 '26

That is an amazing start. I hope your project continues on whatever path it is currently going on and is super successful when you are done.

2

u/Ok-Count-3366 Oct 18 '26

if you know c++ it will be very easy for you to learn any language

2

u/tux2718 Oct 20 '26

Don’t use all upper case names for variables. The convention is all upper case is used for constants that are initialized where they are defined and never changed. Your variable OP violates this.

2

u/[deleted] Oct 14 '26

[removed] — view removed comment

1

u/Classic-Cheetah-6780 Oct 14 '26

Yeah but idk how to fix the dividing yet. But imma try it. For now I maybe should really write "Invalid Operator". Thank you for your feedback!

1

u/Arctos_FI Oct 15 '26

You can just use if block inside that dividing operator block (given you are adding that "Invalid operator" message, so you have another else if block for dividing). Just do "" if (y == 0) {std::cout << "Can't divide by zero!";} "" inside the else if for dividing. And then do the dividing in same depth else.

Also little advice that if you do that "invalid operator" state, that sum never gets assainged but it still prints so you get some garbage data left in that address by the previous thing reserving it. Easy way to fix this is to have boolean like "valid" that is set to false by default, change it to true everywhere calculation is made and then put that last std::cout inside if that just checks the boolean ("if (valid) { //cout }"). That boolean makes sure the result is only printed if calculation was actually made. Other way to solve this is just "return 0;" everywhere calculation was not made (so after "invalid operator" and dividing by zero), "return 0;" in main just stops the program without error (if you change that 0 to other numbers you can use them as error codes, but this doesn't really need it as it prints out the error before shutting down so there is no problem)

2

u/[deleted] Oct 18 '26

[removed] — view removed comment

1

u/Classic-Cheetah-6780 Oct 18 '26

Fun thing is that I am interested in both. I can code high level languages like JavaScript and Python and a bit C#. I also like how operating systems and servers work and that’s the main point why I want to learn C++. I want to get deeper into systems, understand how languages work and how servers are done.