Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

My First Project: A Complete and Intelligent Calculator

williamdev

Active Coder
It's a pretty lame first project but here's the code:


[CODE lang="cpp" title="My Calculator"]#include <iostream>
#include <stdlib.h>
#include "func.h"
int main()
{
int first_Num; //Your First Number
int second_Num; //Your Second Number
int after_Num; //Your Other Number to apply to the previous result with a given operator ex: 2+2 = 4 + 10 <- this is the after_num
std::string operation_Setting; //the setting for which operator you want to use
std::string operation_Switch_Setting; //the setting for continuing to operate on to previous result, prompted after every operation
int check_Stop = 10; //check_Stop is used to stop the while loop if the user declines the option to add, subtract, multiply, divide, or modulus the previous result by a subsequently given number.
int past_Output; //previous output
int present_Output; //current output
bool didOneOperation = false; //to check if the program can prompt the user to operate on previous result, is set to true after you do one operation with the first_Num and second_Num


while (check_Stop > 0) //check_Stop is used to stop the while loop if the user declines the option to add, subtract, multiply, divide, or modulus the previous result by a subsequently given number.
{
std::cout << "Your First Number?" << std::endl; //prompt for first number
std::cin >> first_Num; //first number input by user
while (std::cin.fail())
{
std::cout << "Input an integer only with no spaces or characters" << std::endl;
std::cin.clear();
std::cin.ignore(256, '\n');
std::cin >> first_Num;
}
std::cout << "+ for add, - for subtract, * for multiply, / for divide, and % for modulus. Type one to choose operator" << std::endl;
//prompt for operator
std::cin >> operation_Setting; //input the operator
if (didOneOperation == false)
{
if (operation_Setting == "+")//addition // Way too many if statements, maybe could be shortened
{
std::cout << "Your Second Number?" << std::endl; //prompts second number
std::cin >> second_Num;
while (std::cin.fail())
{
std::cout << "Input an integer only with no spaces or characters" << std::endl;
std::cin.clear();
std::cin.ignore(256, '\n');
std::cin >> second_Num;
}
past_Output = add(first_Num, second_Num);
std::cout << past_Output << std::endl;
didOneOperation = true;
}

else if (operation_Setting == "-")//subtraction
{
std::cout << "Your Second Number?" << std::endl; //prompts second number // Way too many if checks, maybe could be shortened
std::cin >> second_Num;
past_Output = subtract(first_Num, second_Num);
std::cout << past_Output << std::endl;
didOneOperation = true;
}

else if (operation_Setting == "*")//multiplication // Way too many if checks, maybe could be shortened
{
std::cout << "Your Second Number?" << std::endl; //prompts second number
std::cin >> second_Num;
past_Output = multiply(first_Num, second_Num);
std::cout << past_Output << std::endl;
didOneOperation = true;
}

else if (operation_Setting == "/")//division // Way too many if checks, maybe could be shortened
{
std::cout << "Your Second Number?" << std::endl; //prompts second number
std::cin >> second_Num;
past_Output = divide(first_Num, second_Num);
std::cout << past_Output << std::endl;
didOneOperation = true;
}

else if (operation_Setting == "%")//modulus // Way too many if checks, maybe could be shortened
{
std::cout << "Your Second Number?" << std::endl; //prompts second number
std::cin >> second_Num;
past_Output = modulus(first_Num, second_Num);
std::cout << past_Output << std::endl;
didOneOperation = true;
}

else
{
check_Stop = 0;
std::cout << "Please input an operator";
}
}




//NOTE: This next section uses the same logic as before just with different variables




if(didOneOperation == true)
{
while (check_Stop > 0)
{
std::cout << "Operate onto this result? Y/N" << std::endl;
std::cin >> operation_Switch_Setting;
while (std::cin.fail())
{
std::cout << "Input Y or N" << std::endl;
std::cin.clear();
std::cin.ignore(256, '\n');
std::cin >> operation_Switch_Setting;
}
if (operation_Switch_Setting == "Y" || operation_Switch_Setting == "y") //checks for yes option
{
std::cout << "+ for add, - for subtract, * for multiply, / for divide, and % for modulus. Type one to choose operator" << std::endl;
std::cin >> operation_Setting;
if (operation_Setting == "+") // Way too many if checks, maybe could be shortened
{
std::cout << "Give another number to add to your previous result, " << past_Output << ":" << std::endl;
std::cin >> after_Num;
present_Output = add(past_Output, after_Num);
std::cout << present_Output << std::endl;
didOneOperation = true;
past_Output = present_Output;
present_Output = 0;
}

else if (operation_Setting == "-") // Way too many if checks, maybe could be shortened
{
std::cout << "Give another number to subtract from your previous result, " << past_Output << ":" << std::endl;
std::cin >> after_Num;
present_Output = subtract(past_Output, after_Num);
std::cout << present_Output << std::endl;
didOneOperation = true;
past_Output = present_Output;
present_Output = 0;
}

else if (operation_Setting == "*") // Way too many if checks, maybe could be shortened
{
std::cout << "Give another number to multiply your previous result, " << past_Output << ", by:" << std::endl;
std::cin >> after_Num;
present_Output = multiply(past_Output, after_Num);
std::cout << present_Output << std::endl;
didOneOperation = true;
past_Output = present_Output;
present_Output = 0;
}

else if (operation_Setting == "/") // Way too many if checks, maybe could be shortened
{
std::cout << "Give another number to divide your previous result," << past_Output << ", by:" << std::endl;
std::cin >> after_Num;
present_Output = divide(past_Output, after_Num);
std::cout << present_Output << std::endl;
didOneOperation = true;
past_Output = present_Output;
present_Output = 0;
}

else if (operation_Setting == "%") // Way too many if checks, maybe could be shortened
{
std::cout << "Give another number to modulus your previous result," << past_Output << ", by:" << std::endl;
std::cin >> after_Num;
present_Output = modulus(past_Output, after_Num);
std::cout << present_Output << std::endl;
didOneOperation = true;
past_Output = present_Output;
present_Output = 0;
}
else
{
check_Stop = 0;
std::cout << "Please input an operator";
}

}
else if (operation_Switch_Setting == "N" || operation_Switch_Setting == "n")
{
std::cout << "Ok, shutting down...";
abort;
exit;
return 0;

}
}
}


}
}[/CODE]
 
Hey there! C++ right? And also can you provide some images of your creation :)
 
Yep, C++! I am trying to go forward in my programming career this Christmas and the following year. I am starting early (before college), and I feel that I should start early so I can learn something useful in the long run. This program has no GUI, but I can provide images of each step of the calculator. Might implement this in Qt, who knows.
 

Attachments

  • Annotation 2019-12-21 224450.png
    Annotation 2019-12-21 224450.png
    33 KB · Views: 14
Whoops forgot to include the header file func.h
[CODE lang="cpp" title="func.h"]#pragma once
int add(int x, int y)
{
int result_Add = x + y;
return result_Add;
}
int subtract(int x, int y)
{
int result_Subtract = x - y;
return result_Subtract;
}
int multiply(int x, int y)
{
int result_Multiply = x * y; /*
pretty self explanatory as these functions are quite short.
Just made these to try out parameters, and I don't think they are very useful
in the ways of performance/functionality, but they help tidy up the code for people
reading it
*/
return result_Multiply;
}
int divide(int x, int y)
{
int result_Divide = x / y;
return result_Divide;
}
int modulus(int x, int y)
{
int result_Modulus = x % y;
return result_Modulus;
}[/CODE]
 
Oh wow! Very cool! I recognized the <iostream> in your code. I've been trying to teach myself before college as well but often get stuck on little things - like why does this do this, what's this etc. lol. Hopefully you can teach us!
 
FreeCodeCamp has lots of tutorials on frameworks so I could make a Qt or other GUI with this. Just need visual Numpad buttons and output with fonts and such.
 
FreeCodeCamp has lots of tutorials on frameworks so I could make a Qt or other GUI with this. Just need visual Numpad buttons and output with fonts and such.
Interesting! I'll try them out thank you! So how long did it take you to make this?
 
Yeah, I'm kind of tinkering as I go and finding out new things. I am trying to avoid StackOverflow since I can get dependent on that. It will cause me to not learn anything by doing (projects and such and finding out stuff as I go) but just by copy-pasting.
 
Yeah, I'm kind of tinkering as I go and finding out new things. I am trying to avoid StackOverflow since I can get dependent on that. It will cause me to not learn anything by doing (projects and such and finding out stuff as I go) but just by copy-pasting.
Yeah, honestly wasn't a fan of Stack Overflow, CF is supposed to be a learning experience. I hope the community will make you work for your answers! :laugh:
 
Working for the answers is the best option anyways. Doing projects is the best way to learn a programming language and expand your knowledge on how to break down a problem into little bits, find a way to solve those little individual issues, and then build up all of those tiny solutions into one big solution. I have done other projects, but they were very simple Python programs. This project marks my leap into C++, and it feels more like home believe it or not. Python is supposed to be easier, but C++ seems easier to me for some reason.
 
Working for the answers is the best option anyways. Doing projects is the best way to learn a programming language and expand your knowledge on how to break down a problem into little bits, find a way to solve those little individual issues, and then build up all of those tiny solutions into one big solution. I have done other projects, but they were very simple Python programs. This project marks my leap into C++, and it feels more like home believe it or not. Python is supposed to be easier, but C++ seems easier to me for some reason.
Very true, from what I hear, Python is pretty much a stepping stone into other programming languages.
 
Still wrapping my head around classes and such, I don't understand their use cases. I think I missed something when I watched that FreeCodeCamp C++ video, so I need to rewatch the second half of it and I'll be up and running to program Qt for this calculator, and I'm excited for that since I'll be getting away from ugly console programs.
 
Still wrapping my head around classes and such, I don't understand their use cases. I think I missed something when I watched that FreeCodeCamp C++ video, so I need to rewatch the second half of it and I'll be up and running to program Qt for this calculator, and I'm excited for that since I'll be getting away from ugly console programs.
Hmm I might use FreeCodeCamp, I'm using Udemy not really any issues just cost $$
 
I started with Python, and it helped me wrap my head around programming as a whole, so it was a stepping stone for me. But, I didn't wanna get stuck with Python since my end goal is Unreal Engine and high-performance programs, so I ran straight to C++. I have actually been learning programming since 2017, but until now I haven't gotten into it with projects since I had no time. But now it is Christimas so I have enough time to at least learn the basics of Unreal Engine by the end of Christmas.
 
I tried Udemy, but I really couldn't concentrate since I knew I had 48 hours of course lectures for C++ ahead of me. FreeCodeCamp taught me the basics, and now I'm learning more by doing projects. I've heard making projects is the best approach since you can make mistakes and learn from them rather than being held by the hand every step of the way by course instructors but then when they let go you have no application/experience, just theory of how the language works.
 
I tried Udemy, but I really couldn't concentrate since I knew I had 48 hours of course lectures for C++ ahead of me. FreeCodeCamp taught me the basics, and now I'm learning more by doing projects. I've heard making projects is the best approach since you can make mistakes and learn from them rather than being held by the hand every step of the way by course instructors but then when they let go you have no application/experience, just theory of how the language works.
Interesting, last question! I don't want to hijack your thread. Would you recommend that people learn Python first or dive right into C++? That's the one thing I wasn't a fan of, should honestly be a mix of everything.
 
Sorry for the very late response. I recommend learning Python first since it is similar to English and easy to read for someone new to coding. Since I went into coding a while ago, I have tinkered with Python, so now I am confident to move to C++. But I advise you learn Python and do some projects with it unlike me since doing projects with Python can get you even more prepared in the long run to learn complex languages like C++ (Not to say that I am even close to learning C++ thoroughly, barely know the basics.) Also, if you are interested in neural networks, deep learning, machine learning, OpenCV, and all that computer intelligence jazz, then stick to Python since it is more supported for those use cases (Tensorflow and such). Once you learn Python, you can learn the uniqueness of C++ quite quickly and then learn as you do projects. For example, you can make Unreal Engine 4 games with C++ only, so if you want to make games learn C++ since that is a high-performance task. Another language to learn after you learn Python is C#, which can be used for the Unity Game Engine. But so far, I haven't learned much of C++, and I can tell that in comparison Python is more readable since it uses English words with simple grammar. For example:
if("sample" in str): #do stuff

checks for a string in a variable and seems like an English sentence in a way, very easy to understand for newcomers.
Look at the equivalent in C++:
if (s1.find(s2) != std::string::npos) { std::cout << "found!" << '\n'; //found on google }

The reason I prefer C++ for anything is that it really makes me solve problems and think since it is such a difficult language. I like programming because it is like a challenge to me, so I prefer C++. I might hate this language choice in the long run and switch to Python, but in the end, I will have more knowledge on how to solve programming problems and a slight headache :laugh:

In the end, you should always keep an open mind for other languages. You can go with Java, or even dive into web development with JavaScript, TypeScript, HTML, CSS, all of it. But most of the time, it is better to start with Python if you are a complete newcomer who's maximum knowledge of computers is how to do essential functions on it. I have learned that you cannot take giant steps, only baby steps, to your final goal (as I have said, I have not reached my own goal, but I have learned this insofar). I tried going from "Hello World" to Unreal Engine, and that did not work at all; it was too difficult with my mindset and knowledge. I felt like I knew enough, but I didn't. So Python is the best first language. If you are aiming at creating programs that are best created with a specific language for reasons like high performance (C++) frameworks made specifically for that language, still go with Python first to learn the basics (aka all of the things that a language can do outlined by any programming tutorial in X hours video
To view this content we will need your consent to set third party cookies.
For more detailed information, see our cookies page.
) then go to your preferred language after you get used to programming as a whole with Python.
Merry Christmas,
William Boycher
:sleep:
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom